GithubHelp home page GithubHelp logo

css-shortener's Introduction

css-shortener

CI npm npm GitHub issues

Preview

/* BEFORE */
p.this-class-is-extremely-long-and-definitely-needs-to-be-shortened,
p.why-is-this-so-long-if-it-just-makes-white-text {
  color: white;
}

/* AFTER */
p.a,
p.b {
  color: white;
}

Table of contents

  1. Quick Start
  2. API Documentation
    1. Constructor
    2. #importMap(map, override)
    3. #map
  3. Examples
    1. CSS filter for nunjucks and express

Quick Start

import { CssShortener } from 'css-shortener';

const shortener = new CssShortener();

console.log(shortener.shortenClassName('long-class'));
// Output: a

console.log(shortener.map);
// Output: {
//   "long-class": "a"
// }

API Documentation

Constructor

const options = {
  alphabet: 'abcdef',
};
const shortener = new CssShortener(options);

The options parameter can be omitted.

Options

Option Type Optionality Description Default value
alphabet string optional The alphabet is used to generate the new class names. 'abcefghijklmnopqrstuvwxyz0123456789_-'

Note that there is no d in the default alphabet to avoid generation of the combination ad.

#importMap(map, override)

Imports mappings into the shortener

cssShortener.importMap(
  {
    'my-extremely-long-class-name': 'a',
  },
  false
);

If override is true, class names that are already mapped will be overridden.
The override parameter can be omitted.

#map

Returns the mapped class names

var map = cssShortener.getMap();
{
  "my-extremely-long-class-name": "a"
}

Examples

CSS filter for nunjucks and express

const express = require('express');
const nunjucks = require('nunjucks');

const cssMap = JSON.parse(fs.readFileSync('./cssMap.json'));

const app = express();
const env = nunjucks.configure('your-views-folder', { express: app });

env.addFilter('css', function(str) {
  return str
    .split(' ')
    .map(c => (cssMap[c] != null ? cssMap[c] : c))
    .join(' ');
});
<!-- BEFORE -->
<div class="{{'my-extremely-long-class-name'|css}}"></div>
<!-- RENDERED -->
<div class="a"></div>

css-shortener's People

Contributors

augustdg avatar dependabot[bot] avatar mbrandau avatar renovate-bot avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

awen-hub

css-shortener's Issues

Cannot find module '../index'

Hey!

I was trying out this plugin, but couldn't use it as this error always came up:

Error: Cannot find module '../index'
Require stack:
- C:\Users\augus\AppData\Roaming\npm\node_modules\css-shortener\bin\cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\Users\augus\AppData\Roaming\npm\node_modules\css-shortener\bin\cli.js:3:22)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\augus\\AppData\\Roaming\\npm\\node_modules\\css-shortener\\bin\\cli.js'
  ]
}

When I went to check the file, it indeed was requiring an '../index', but I couldn't find it either...

const CssShortener = require('../index');
const fs = require('fs');

Exclude comments from CSS RegExp

The current RegExp (/\.[a-z][a-z0-9-_]*/g) also replaces things in comments. That specifically is a problem with source map file extensions.

/*# sourceMappingURL=bootstrap.min.css.map */
/*# sourceMappingURL=bootstrap.aij.aik.ail */

Usage with Webpack/NuxtJS

Hello @mbrandau
Thank you vey much for your amazing work!
How to use it with Webpack/NuxtJS to replace the classes in Vue template & style tags?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update actions/checkout action to v4 (main)
  • chore(deps): update actions/setup-node action to v4 (main)
  • chore(deps): update dependency @size-limit/preset-small-lib to v11 (main)
  • chore(deps): update dependency conventional-changelog-conventionalcommits to v8 (main)
  • chore(deps): update dependency husky to v9 (main)
  • chore(deps): update dependency size-limit to v11 (main)
  • chore(deps): update dependency typescript to v5 (main)
  • chore(deps): update dependency @semantic-release/changelog to v6.0.3 (next)
  • chore(deps): update actions/checkout action to v4 (next)
  • chore(deps): update actions/setup-node action to v4 (next)
  • chore(deps): update dependency @size-limit/preset-small-lib to v11 (next)
  • chore(deps): update dependency conventional-changelog-conventionalcommits to v8 (next)
  • chore(deps): update dependency husky to v9 (next)
  • chore(deps): update dependency size-limit to v11 (next)
  • chore(deps): update dependency typescript to v5 (next)
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

Branch main
github-actions
.github/workflows/main.yml
  • actions/checkout v3
  • actions/setup-node v3
  • bahmutov/npm-install v1
.github/workflows/release.yml
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/size.yml
  • actions/checkout v3
  • andresz1/size-limit-action v1
npm
package.json
  • @semantic-release/changelog 6.0.1
  • @semantic-release/git 10.0.1
  • @size-limit/preset-small-lib 7.0.8
  • conventional-changelog-conventionalcommits 4.6.3
  • husky 8.0.1
  • semantic-release 19.0.2
  • size-limit 7.0.8
  • tsdx 0.14.1
  • tslib 2.4.0
  • typedoc 0.22.15
  • typescript 4.6.4
  • node >=14
Branch next
github-actions
.github/workflows/main.yml
  • actions/checkout v3
  • actions/setup-node v3
  • bahmutov/npm-install v1
.github/workflows/release.yml
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/size.yml
  • actions/checkout v3
  • andresz1/size-limit-action v1
npm
package.json
  • @semantic-release/changelog 6.0.1
  • @semantic-release/git 10.0.1
  • @size-limit/preset-small-lib 7.0.8
  • conventional-changelog-conventionalcommits 4.6.3
  • husky 7.0.4
  • semantic-release 19.0.2
  • size-limit 7.0.8
  • tsdx 0.14.1
  • tslib 2.3.1
  • typedoc 0.22.13
  • typescript 4.6.3
  • node >=14

  • Check this box to trigger a request for Renovate to run again on this repository

@-webkit-keyframes percentages detected as classnames

INPUT:

@-webkit-keyframes primary-indeterminate-scale {
0% {
transform: scaleX(0.08); }
36.65% {
-webkit-animation-timing-function: cubic-bezier(0.33473, 0.12482, 0.78584, 1);
animation-timing-function: cubic-bezier(0.33473, 0.12482, 0.78584, 1);
transform: scaleX(0.08); }
69.15% {
-webkit-animation-timing-function: cubic-bezier(0.06, 0.11, 0.6, 1);
animation-timing-function: cubic-bezier(0.06, 0.11, 0.6, 1);
transform: scaleX(0.66148); }
100% {
transform: scaleX(0.08); } }

OUTPUT:

@-webkit-keyframes primary-indeterminate-scale {
0% {
transform: scaleX(0.08); }
36.qJ {
-webkit-animation-timing-function: cubic-bezier(0.33473, 0.12482, 0.78584, 1);
animation-timing-function: cubic-bezier(0.33473, 0.12482, 0.78584, 1);
transform: scaleX(0.08); }
69.qH {
-webkit-animation-timing-function: cubic-bezier(0.06, 0.11, 0.6, 1);
animation-timing-function: cubic-bezier(0.06, 0.11, 0.6, 1);
transform: scaleX(0.66148); }
100% {
transform: scaleX(0.08); } }

Add function generate source maps

I'd like to map the shortened classes to the original classes. Although this is not really necessary since I could just disable the shortening in development, I would like to implement this feature just for the sake of being able to do so.
I currently don't know how source maps are generated exactly and if it is even possible to map the classes although the names changed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.