EsifyCSS

Generates modules from CSS.
EsifyCSS consists of a PostCSS plugin and a Runner.
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
.
Example
Assume you have following sample.css
:
@keyframes foo {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(720deg);
}
}
#bar1 {
animation: 1s foo;
}
#bar2 {
animation: 2s foo;
}
.baz1 {
animation: 3s foo;
}
.baz2 {
animation: 4s bar;
}
Then, run the command:
$ esifycss sample.css
You'll get the following file:
import {addStyle} from './helper.js';
addStyle([
"qBsBKAMCSUCWGYaSceIEuBUCWGYawBceIEE",
"gBASCOGiBQKAMIE",
"gBAiBCOGkBQKAMIE",
"mBAkBCOGoBQKAMIE",
"mBAoBCOGMQKyBIE"
]);
export const className = {
"baz1": "_2",
"baz2": "_3"
};
export const id = {
"bar1": "_0",
"bar2": "_1"
};
export const keyframes = {
"foo": "_4"
};
Each string given to addStyle
represents a CSS string.
They are inserted to the document's stylesheet by the helper script.
Classnames, identifiers, and keyframes are shortened uniquely and it makes
styles modular. This means you don't have to concern about naming somethings.
Installation
npm install --save-dev esifycss
Usage
Usage: esifycss [options] <include ...>
Options:
-V, --version output the version number
--output <path> A path to the helper script.
--config <path> A path to configuration files.
--exclude <path ...> Paths or patterns to be excluded.
--watch Watch files and update the modules automatically.
-h, --help output usage information
@import
Syntax
You can use @import
syntax when the style declarations requires names in external files.
Example: Assume you have the following a.css
and b.css
.
.container {...}
.container {...}
The container
class names will be shortened to unique names like
_0
and _1
.
You can import the shortened names with the @import
syntax.
@import './a.css' modA-;
@import './b.css' BBB;
.wrapper>.modA-container {...}
.wrapper>.BBBcontainer {...}
JavaScript API for Runner
import {Session} from 'esifycss';
new Session(options).start()
.then(() => console.log('Done'))
.catch((error) => console.error(error));
Options
export interface ISessionOptions {
include?: string | Array<string>,
exclude?: anymatch.Matcher,
helper?: string,
watch?: boolean,
chokidar?: chokidar.WatchOptions,
postcssPlugins?: Array<postcss.AcceptedPlugin>,
esifycssPluginParameter?: IPluginOptions,
stdout?: stream.Writable,
stderr?: stream.Writable,
}
Source: src/runner/types.ts
JavaScript API for Plugin
const postcss = require('postcss');
const esifycss = require('esifycss');
postcss([
esifycss.plugin({}),
])
.process(css, {from: '/foo/bar.css'})
.then((result) => {
const pluginResult = esifycss.extractPluginResult(result);
console.log(pluginResult);
});
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 IPluginOptions {
mangle?: boolean,
identifier?: IIdentifier,
rawPrefix?: string,
mangler?: IPluginMangler,
}
Source: src/postcssPlugin/types.ts
LICENSE
The esifycss project is licensed under the terms of the Apache 2.0 License.