CSS minifier based on existing NPM package (added NDCODE hooks)
An NDCODE project.
Overview
This is a minor modification to the NPM clean-css
package version 4.2.1.
For the complete usage information, please see the original documentation at:
https://www.npmjs.com/package/clean-css.
We gratefully acknowledge the authors for their MIT-licensing the package.
Modifications
Suppose this package is imported as
let CleanCSS = require('@ndcode/clean-css')
and then instantiated by
let clean_css = new CleanCSS(...)
,
then usually minification is done by calling
clean_css.minify(input, maybeSourceMap, maybeCallback)
.
We define a similar function for minifying CSS embedded in other source code,
clean_css.minifyEmbedded(
input,
embeddedStart,
maybeSourceMap,
maybeCallback
)
This makes parsing start at the specified character position embeddedStart
in
the input
string, which should be just after an opening {
character. CSS is
parsed while counting the nesting level of braces, and parsing stops when an
unmatched }
is encountered. The embedded CSS is then minified as per usual.
Suppose the minifyEmbedded()
function returns the object result
, then the
minified CSS is normally accessed as result.styles
. We define a new field
result.embeddedEnd
which contains the character position of the }
that
stopped the parsing. If no unmatched }
is found, -1
is returned instead.
Note that passing embeddedStart == -1
makes minifyEmbedded()
behave the
same as minify()
, and that embeddedEnd == -1
is returned by both minify()
and minifyEmbedded()
in the case that no final }
was found and/or sought.
The embedded-CSS interface is used by the jst
(JavaScript Templates) package.
To be implemented
In the future we may also add separate APIs for parsing and rendering the CSS.
This would have been handy for the current usage, since the jst
package works
by parsing, transforming, and then rendering the source code tree, but for now
it was simpler just to minify the CSS completely to a string during parsing.
GIT repository
The development version can be cloned, downloaded, or browsed with gitweb
at:
https://git.ndcode.org/public/clean-css.git
License
All of our NPM packages are MIT licensed, please see LICENSE in the repository.
Contributions
Feedback on NDCODE packages and/or code contributions are gratefully welcomed.
Contact: Nick Downing nick@ndcode.org