Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

esifycss

gjbkz5.1kApache-2.01.4.40TypeScript support: included

Generates .js or .ts exports class names and custom properties

readme

EsifyCSS

.github/workflows/test.yml BrowserStack Status codecov

Introduction

EsifyCSS finds CSS files in your project and generates ES modules for each of them.

Assume that you have src/style1.css and src/style2.css. They have the same contents:

/* src/style1.css, src/style2.css */
@keyframes FadeIn {
    0%: {opacity: 0}
  100%: {opacity: 0}
}
@keyframes Rotate {
    0%: {transform: rotate(  0deg)}
  100%: {transform: rotate(360deg)}
}
#container {
  animation: 0.2s linear FadeIn;
}
.icon {
  animation-duration: 1s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
.icon.rotate {
  animation-name: Rotate;
}

Then, run esifycss --helper src/helper.js src. --helper src/helper.js is where the helper script is written. The last src specifies the directory that contains the file to be processed by EsifyCSS.

The process finds CSS files, parses them, extracts identifiers, replaces them with values.

After the process, you'll get src/style1.css.js and src/style2.css.js:

// src/style1.css.js
import {addStyle} from './helper.js';
addStyle(["WYIGqCCQSCaAQEcSCaAUEE","WYIGsCCQSCeAgBiBIIQkBmBEcSCeAgBiByBkBmBEE","0BGQC2BA4BKOA6BoBIqBIGqCKE","sBGUCOM8BAUoBKOM+BMgCAiCKOMkCMmCAqBKE","sBG2CG4CCOMoCAGsCKE"]);
export const className = {
    "icon": "_1",
    "rotate": "_2"
};
export const id = {
    "container": "_0"
};
export const keyframes = {
    "FadeIn": "_3",
    "Rotate": "_4"
};
// src/style2.css.js
import {addStyle} from './helper.js';
addStyle(["WYIGuBCQSCaAQEcSCaAUEE","WYIGwBCQSCeAgBiBIIQkBmBEcSCeAgBiByBkBmBEE","0BGuCC2BA4BKOA6BoBIqBIGuBKE","sBGwCCOM8BAUoBKOM+BMgCAiCKOMkCMmCAqBKE","sBGyCG0CCOMoCAGwBKE"]);
export const className = {
    "icon": "_6",
    "rotate": "_7"
};
export const id = {
    "container": "_5"
};
export const keyframes = {
    "FadeIn": "_8",
    "Rotate": "_9"
};

The two modules are almost the same, but the exported objects are different. And there will be src/helper.js which exports the addStyle function which applies the style to documents. You can see the code at sample/01-mangle/helper.js.

The exported objects are mappings of identifiers of className, id, and keyframes that were replaced in the process. You should import them and use the replaced identifiers instead of original in the code:

import style from './style1.css.js';
const element = document.createElement('div');
element.classList.add(style.className.icon);

Tools

EsifyCSS consists of PostCSS plugin, Runner and CLI.

PostCSS plugin

The plugin converts the identifiers in CSS and minifies them. It outputs the result of minifications using Root.warn().

Runner

A runner process .css files in your project with PostCSS and output the results to .css.js or .css.ts.

CLI

Usage: esifycss [options] <include ...>

Options:
  -V, --version         output the version number
  --helper <path>       A path where the helper script will be output. You can't use --helper with --css.
  --css <path>          A path where the css will be output. You can't use --css with --helper.
  --ext <ext>           An extension of scripts generated from css.
  --config <path>       A path to configuration files.
  --exclude <path ...>  Paths or patterns to be excluded.
  --noMangle            Keep the original name for debugging.
  --watch               Watch files and update the modules automatically.
  -h, --help            output usage information

example: generate .css.ts and css-helper.ts

esifycss --helper=css-helper.ts --ext=.ts <source-directory>

example: TypeScript based Next.js project

Assume that you have following files:

src/
  styles/
    global.css
  components/
    Button/
      index.ts
      style.module.css
  pages/
    _app.tsx

Then, run the following command:

esifycss --css=src/pages/all.css --ext=.ts src

You'll get src/pages/all.css. src/pages/_app.tsx should import it:

// src/pages/_app.tsx
import './all.css';

Installation

npm install --save-dev esifycss

@import Syntax

You can use @import syntax if the style declarations requires identifiers declared in other files.

For example, Assume you have the following a.css and b.css.

/* a.css */
.container {...} /* → ._0 */
/* b.css */
.container {...} /* → ._1 */

The container class names will be shortened to unique names like _0 and _1. You can import the shortened names with the @import syntax.

/* "modA-" is prefix for a.css */
@import './a.css' modA-;
/* "bbbb" is prefix for b.css */
@import './b.css' BBB;
.wrapper>.modA-container {...} /* → ._2>._0 */
.wrapper>.BBBcontainer {...}   /* → ._2>._1 */

JavaScript API for Runner

import {Session} from 'esifycss';
new Session(options).start()
.then(() => console.log('Done'))
.catch((error) => console.error(error));

Options

export interface SessionOptions {
  /**
   * Pattern(s) to be included
   * @default "*"
   */
  include?: string | Array<string>,
  /**
   * Pattern(s) to be excluded.
   * @default ['node_modules']
   */
  exclude?: anymatch.Matcher,
  /**
   * File extension(s) to be included.
   * @default ['.css']
   */
  extensions?: Array<string>,
  /**
   * Where this plugin outputs the helper script.
   * If you use TypeScript, set a  value like '*.ts'.
   * You can't use this option with the css option.
   * The {hash} in the default value is calculated from the include option.
   * @default "helper.{hash}.css.js"
   */
  helper?: string,
  /**
   * File extension of generated script.
   * @default options.helper ? path.extname(options.helper) : '.js'
   */
  ext?: string,
  /**
   * Where this plugin outputs the css.
   * You can't use this option with the helper option.
   * @default undefined
   */
  css?: string,
  /**
   * It it is true, a watcher is enabled.
   * @default false
   */
  watch?: boolean,
  /**
   * Options passed to chokidar.
   * You can't set ignoreInitial to true.
   * @default {
   *   ignore: exclude,
   *   ignoreInitial: false,
   *   useFsEvents: false,
   * }
   */
  chokidar?: chokidar.WatchOptions,
  /**
   * An array of postcss plugins.
   * esifycss.plugin is appended to this array.
   * @default []
   */
  postcssPlugins?: Array<postcss.AcceptedPlugin>,
  /**
   * https://github.com/postcss/postcss#options
   * @default undefined
   */
  postcssOptions?: postcss.ProcessOptions,
  /**
   * Parameters for esifycss.plugin.
   */
  esifycssPluginParameter?: PluginOptions,
  /**
   * A stream where the runner outputs logs.
   * @default process.stdout
   */
  stdout?: stream.Writable,
  /**
   * A stream where the runner outputs errorlogs.
   * @default process.stderr
   */
  stderr?: stream.Writable,
}

Source: src/runner/types.ts

JavaScript API for Plugin

const postcss = require('postcss');
const esifycss = require('esifycss');
postcss([
  esifycss.plugin({/* Plugin Options */}),
])
.process(css, {from: '/foo/bar.css'})
.then((result) => {
  const pluginResult = esifycss.extractPluginResult(result);
  console.log(pluginResult);
  // → {
  //   className: {bar: '_1'},
  //   id: {foo: '_0'},
  //   keyframes: {aaa: '_2'},
  // }
});

The code is at sample/plugin.js. You can run it by node sample/plugin.js after cloning this repository and running npm run build.

Options

export interface PluginOptions {
  /**
   * When it is true, this plugin minifies classnames.
   * @default true
   */
  mangle?: boolean,
  /**
   * A function returns an unique number from a given file id. If you process
   * CSS files in multiple postcss processes, you should create an identifier
   * outside the processes and pass it as this value to keep the uniqueness
   * of mangled outputs.
   * @default esifycss.createIdGenerator()
   */
  identifier?: IdGenerator,
  /**
   * Names starts with this value are not passed to mangler but replaced with
   * unprefixed names.
   * @default "raw-"
   */
  rawPrefix?: string,
  /**
   * A custom mangler: (*id*, *type*, *name*) => string.
   * - *id*: string. A filepath to the CSS.
   * - *type*: 'id' | 'class' | 'keyframes'. The type of *name*
   * - *name*: string. An identifier in the style.
   *
   * If mangler is set, `mangle` and `identifier` options are ignored.
   *
   * For example, If the plugin processes `.foo{color:green}` in `/a.css`,
   * The mangler is called with `("/a.css", "class", "foo")`. A mangler should
   * return an unique string for each input pattern or the styles will be
   * overwritten unexpectedly.
   * @default undefined
   */
  mangler?: PluginMangler,
}

Source: src/postcssPlugin/types.ts

LICENSE

The esifycss project is licensed under the terms of the Apache 2.0 License.

changelog

Changelog

v1.4.40 (2023-01-07)

Dependency Upgrades

  • reinstall packages (21e69ab)
  • @hookun/parse-animation-shorthand:0.1.4→0.1.5 (8c4990d)
  • @nlib/eslint-config:3.19.5→3.19.6 @typescript-eslint/eslint-plugin:5.47.1→5.48.0 (1fda220)
  • postcss:8.4.20→8.4.21 (a33d953)
  • rollup:2.79.1→3.9.1 (1cf9d99)
  • ava:4.3.3→5.1.0 (86cb8e3)

v1.4.39 (2022-08-21)

Bug Fixes

Tests

Continuous Integration

Dependency Upgrades

  • add @nlib/changelog (4f0025c)
  • npm update (62ea1f1)
  • typescript:4.5.5→4.7.4 (7626a2a)
  • ts-node:10.4.0→10.9.1 (44c4818)
  • rollup:2.68.0→2.78.1 (d1ac037)
  • postcss-selector-parser:6.0.7→6.0.10 (924b7ec)
  • postcss-scss:4.0.3→4.0.4 (8d9b11d)
  • postcss:8.4.5→8.4.16 (2cdbd96)
  • lint-staged:12.3.4→13.0.3 (26db908)
  • commander:8.3.0→9.4.0 (f53d7d5)
  • chokidar:3.5.2→3.5.3 (663888d)
  • browserstack-local:1.4.8→1.5.1 (157b6ae)
  • ava:3.15.0→4.3.1 (7b8d589)
  • acorn:8.7.0→8.8.0 (0368d76)
  • selenium-webdriver:4.1.1→4.4.0 (d5456df)
  • @types/selenium-webdriver:4.0.18→4.1.2 (2101c8a)
  • @types/node:17.0.17→18.7.6 (4dda217)
  • @types/jsdom:16.2.14→20.0.0 (f356a91)
  • @nlib/lint-commit:0.1.8→0.2.0 (417b78f)
  • @typescript-eslint/eslint-plugin:5.12.0→5.33.1 @typescript-eslint/parser:5.12.1→5.33.1 eslint:8.10.0→8.22.0 (69b8884)
  • @nlib/indexen:0.1.2→0.2.5 (58de373)
  • @nlib/eslint-config:3.17.30→3.19.4 (a6a8687)
  • @nlib/eslint-config:3.17.29→3.17.30 @types/node:16.11.12→17.0.0 @typescript-eslint/eslint-plugin:5.4.0→5.7.0 @typescript-eslint/parser:5.4.0→5.7.0 eslint:8.3.0→8.5.0 lint-staged:12.1.2→12.1.3 postcss-selector-parser:6.0.6→6.0.7 typescript:4.5.2→4.5.4 (d5bd860)
  • add @nlib/lint-commit (291e58c)
  • install @nlib/lint-commit @nlib/indexen (baba31b)

v1.4.37 (2021-11-25)

Documentation

Dependency Upgrades

  • @nlib/eslint-config:3.17.28→3.17.29 @types/node:16.11.6→16.11.10 @types/node-fetch:2.5.12→3.0.3 @typescript-eslint/eslint-plugin:5.3.0→5.4.0 @typescript-eslint/parser:5.3.0→5.4.0 lint-staged:11.2.6→12.1.2 postcss:8.3.11→8.4.1 rollup:2.60.0→2.60.1 selenium-webdriver:4.0.0→4.1.0 (0e09342)

v1.4.36 (2021-11-05)

Bug Fixes

  • transform only once for a root (c5c27cf)

Build System

Dependency Upgrades

  • @nlib/eslint-config:3.17.25→3.17.28 @types/node:16.10.3→16.11.6 @types/node-fetch:2.5.12→3.0.3 @types/selenium-webdriver:4.0.15→4.0.16 @typescript-eslint/eslint-plugin:4.32.0→5.3.0 @typescript-eslint/parser:4.31.2→5.3.0 commander:8.2.0→8.3.0 eslint:7.32.0→8.1.0 lint-staged:11.2.3→11.2.6 postcss:8.3.9→8.3.11 typescript:4.4.3→4.4.4 vlq:1.0.1→2.0.4 (d085782)

v1.4.35 (2021-09-23)

Bug Fixes

v1.4.34 (2021-09-23)

Bug Fixes

  • parseScripts caches the results to keep css after minification (76edcb4)

Tests

  • output css should have all styles (c141495)

Dependency Upgrades

  • @types/node:16.9.1→16.9.6 @typescript-eslint/eslint-plugin:4.31.1→4.31.2 @typescript-eslint/parser:4.31.0→4.31.2 postcss:8.3.6→8.3.7 rollup:2.56.3→2.57.0 (4e35a84)

v1.4.33 (2021-09-11)

Documentation

  • update the Next.js example (fd7370a)

v1.4.32 (2021-09-11)

Features

  • suppress worthless overwrites (f204e0e)

Bug Fixes

  • update output css on change (1c59e7b)

v1.4.31 (2021-09-11)

Features

  • add preamble to generated files (2af96b3)

Build System

Dependency Upgrades

  • @types/node:16.7.13→16.9.1 commander:8.1.0→8.2.0 typescript:4.4.2→4.4.3 (764e7ee)

v1.4.29 (2021-09-11)

Features

  • minifyScriptForCSS removes import declarations (1e73fd1)

Bug Fixes

  • temporary scripts must have addStyle (5f24765)
  • omit addStyle() from generated code if the output is css (2f1bd46)
  • automatically exclude the path of css output (d7f3eda)
  • ignore parse animation error (#403) (df4896a)

Tests

  • run twice to reproduce the error (86ef1bc)

Documentation

v1.4.28 (2021-09-09)

Bug Fixes

Tests

Documentation

Continuous Integration

Dependency Upgrades

  • downgrade @types/node-fetch (502e6a5)
  • downgrade node-fetch (3c2aee6)
  • @nlib/eslint-config:3.17.24→3.17.25 @nlib/githooks:0.0.5→0.1.0 @types/anymatch:1.3.1→3.0.0 @types/node:15.14.9→16.7.13 @types/node-fetch:2.5.12→3.0.3 @typescript-eslint/eslint-plugin:4.30.0→4.31.0 @typescript-eslint/parser:4.30.0→4.31.0 (760378f)
  • uninstall @nlib/changelog @nlib/lint-commit (172c4b2)
  • acorn:8.4.1→8.5.0 acorn-walk:8.1.1→8.2.0 commander:7.2.0→8.1.0 node-fetch:2.6.1→3.0.0 postcss-scss:3.0.5→4.0.0 ts-node:9.1.1→10.2.1 (5fcd8db)
  • setup githooks (#274) (c3db89d)

v1.4.27 (2021-03-14)

Dependency Upgrades

  • upgrade dependencies (#235) (8074d39)

v1.4.26 (2020-12-20)

Dependency Upgrades

  • upgrade dependencies (#165) (2c1b842)

v1.4.25 (2020-10-30)

Continuous Integration

  • update TestClient condition (78feb22)
  • update TestClient condition (af18829)
  • update TestClient condition (25cabb6)

Dependency Upgrades

  • upgrade dependencies (#107) (d5bc422)

v1.4.24 (2020-10-04)

Continuous Integration

  • fix scripts before publish (bbadd29)
  • remove the prepack scripts (c1ad02c)

Dependency Upgrades

v1.4.23 (2020-10-01)

Code Refactoring

Styles

Dependency Upgrades

v1.4.22 (2020-07-12)

Bug Fixes

Tests

  • parse multiple animations (cafbd8b)

Code Refactoring

v1.4.21 (2020-07-11)

Code Refactoring

  • use parse-animation-shorthand instead of nbnf (a984029)

Build System

  • setup github actions (#83) (f9a1892)

v1.4.20 (2020-06-25)

Bug Fixes

v1.4.19 (2020-06-24)

Bug Fixes

Tests

v1.4.18 (2020-06-24)

Bug Fixes

  • remove acorn-dynamic-import (#79) (ee1c7f3)

v1.4.17 (2020-06-19)

Bug Fixes

  • ignore invalid arguments (495f034)

v1.4.16 (2020-06-17)

Bug Fixes

  • leave import statements (c1b6610)

v1.4.15 (2020-06-17)

Bug Fixes

  • ignore AssignmentExpression (b991b2c)

v1.4.14 (2020-06-15)

Bug Fixes

  • support locally declared addStyle functions (d59b3a9)

v1.4.13 (2020-06-02)

Bug Fixes

  • remove the uninstalled package (99548ce)

Tests

Code Refactoring

v1.4.12 (2020-01-23)

v1.4.11 (2020-01-22)

Bug Fixes

v1.4.10 (2020-01-21)

Bug Fixes

v1.4.9 (2020-01-21)

Bug Fixes

v1.4.8 (2020-01-21)

Bug Fixes

v1.4.7 (2020-01-21)

Bug Fixes

  • remove .ts extension (9e169f2)
  • build script (69cd62c)
  • determine helper import from source string (8ea1e48)

Code Refactoring

v1.4.6 (2020-01-21)

Tests

v1.4.5 (2020-01-21)

Bug Fixes

  • update minifier not to use RegExp (6702084)
  • addStyle accepts an array of css objects (38faafc)

Tests

Code Refactoring

v1.4.4 (2020-01-20)

Bug Fixes

  • use acorn to remove import declarations (#59) (279ccc2)

v1.4.3 (2020-01-19)

Bug Fixes

v1.4.2 (2020-01-18)

Bug Fixes

v1.4.1 (2020-01-18)

Features

v1.4.0 (2020-01-18)

Features

Tests

Documentation

v1.3.14 (2020-01-06)

v1.3.13 (2019-12-09)

v1.3.12 (2019-12-09)

Documentation

v1.3.11 (2019-12-07)

Bug Fixes

v1.3.9 (2019-12-06)

Features

  • support postcss.ProcessOptions (026c237)

Tests

v1.3.8 (2019-11-14)

Code Refactoring

v1.3.7 (2019-10-30)

Bug Fixes

Tests

Code Refactoring

Documentation

v1.3.6 (2019-09-20)

Code Refactoring

v1.3.5 (2019-09-04)

Bug Fixes

  • minifyCSSInScript is not asnyc (240f045)

v1.3.4 (2019-09-04)

Bug Fixes

v1.3.3 (2019-07-15)

v1.3.2 (2019-07-08)

Features

  • add css.d.ts for typeRoots (81bcdde)

v1.3.1 (2019-07-05)

Features

  • use acorn to extract CSS in script (7585e32)

v1.3.0 (2019-07-05)

Features

  • add getHelperScript and update processCSS (696045a)

Tests

  • fix the message to be wait (db75c1e)

Code Refactoring

  • change the encoding format (4d310ce)

v1.2.11 (2019-07-05)

Tests

  • add tests for serializeCSSAnimationShorthand (5f9eff2)

Code Refactoring

  • split serializeTimingFunction (21fdf2c)

Documentation

v1.2.10 (2019-07-03)

Bug Fixes

  • normalize path for Windows (c5d013f)
  • allow raw css to work in watch-mode (c6e9fb3)

Tests

  • test the generated code (b9d65d9)

Code Refactoring

v1.2.9 (2019-07-02)

Features

  • update mangler function (de49888)
  • add the extensions option (7c81df8)

Code Refactoring

Styles

Documentation

v1.2.8 (2019-06-22)

Bug Fixes

v1.2.7 (2019-06-21)

Features

  • export tokenizeString and delete countTokens (2883942)

v1.2.6 (2019-06-21)

Features

  • export createOptimizedIdentifier (64d3ed8)

v1.2.5 (2019-06-21)

Bug Fixes

v1.2.4 (2019-06-21)

Features

  • export some modules for minification (372197d)

v1.2.3 (2019-06-21)

Bug Fixes

  • add safe-base64 encoding and make it default (37fde07)

v1.2.2 (2019-06-21)

Bug Fixes

v1.2.1 (2019-06-21)

Features

  • delete the minifyScript option (b0b1a24)

Documentation

  • rename IXXXParameters and update README (30a29fa)
  • add some comments (6893026)

v1.2.0 (2019-06-20)

Features

Bug Fixes

Code Refactoring

  • remove a debug code (0f0e885)
  • merge wordToString to decode (e871d54)

Documentation

v1.1.2 (2019-06-19)

Features

  • add processCSS and hide the public handlers (8425ef2)

v1.1.1 (2019-06-19)

Features

  • expose the event handlers (2a51bef)

v1.1.0 (2019-06-19)

Features

  • the 1st arg of the constructor is optional (b3bbb86)
  • rename output to helper (b26dce5)
  • all options are optional (4375f02)

Tests

Documentation

v1.0.1 (2019-06-18)

Features

  • parse @import and transform identifiers (71805e8)
  • add noMangle option (c5377fa)
  • rewrite and reconfigure Renovate (#19) (914f7ff)

Code Refactoring

Documentation

v0.1.16 (2019-01-24)

Code Refactoring

v0.1.15 (2018-10-15)

v0.1.14 (2018-10-15)

v0.1.13 (2018-10-12)

v0.1.12 (2018-10-11)

v0.1.11 (2018-10-11)

v0.1.10 (2018-10-05)

v0.1.9 (2018-10-05)

v0.1.8 (2018-09-28)

v0.1.7 (2018-09-27)

v0.1.6 (2018-09-27)

v0.1.5 (2018-09-26)

v0.1.4 (2018-09-26)

v0.1.3 (2018-09-21)

v0.1.1 (2018-09-21)

v0.1.0 (2018-09-21)