Skip to content

2.0.0 Drop legacy API and new AST format

Compare
Choose a tag to compare
@lahmatiy lahmatiy released this 06 Apr 02:35
· 251 commits to master since this release

It has been almost six months since CSSO came back to active development. During this time, it has become much faster, reduce memory consumption and has got new cool features. It is time to discard the legacy API to keep moving forward.
If you have used the CLI nothing has changed for you. Basically, the changes affect the internal API users and plugin authors. But the changes are not dramatic and aimed to provide a simple and consistent API.
For example earlier minify() method returns a string or an object, depending on whether the source map is required or not. Now the method is less confusing and always returns an object.

var csso = require('csso');

// before
console.log(csso.minify(css));
console.log(csso.minify(css, { sourceMap: true }).css);

// now it's consistent
console.log(csso.minify(css).css);
console.log(csso.minify(css, { sourceMap: true }).css);

Most significant changes is about AST format. For long time, CSSO used gonzales parser and its AST format for CSS transformation. That format is good enough, but not so convenient to maintain and extend. Therefore in 1.5.0 internal AST format was introduced. Despite the fact that the internal format is better in many ways, it led to unnecessary overhead in time and memory, as required converting gonzales<->internal. In addition, it has led to the need for dual-API – one for gonzales AST and another for internal.
Parser was reworked to build AST in "internal" format, that become single format is using by CSSO now. Gonzales format and related code was totally removed. The huge diff but it's worth it! This, as well as the introduction of a brand new dynamic scanner, made the parser faster by ~30%. And together with no extra converting compression accelerated at least by ~15% and reduced memory consumption by almost half.

These are important changes for the upcoming features!
See current state of API in readme.

Changes

  • No more gonzales AST format and related code
  • minify() and minifyBlock() is always return an object as result now (i.e. { css: String, map: SourceMapGenerator or null })
  • parse()
    • Returns AST in new format (so called internal)
    • Dynamic scanner implemented
    • New AST format + dynamic scanner = performance boost and less memory consumption
    • No more context argument, context should be specified via options
    • Supported contexts now: stylesheet, atrule, atruleExpression, ruleset, selector, simpleSelector, block, declaration and value
    • Drop needPositions option, positions option should be used instead
    • Drop needInfo option, info object is attaching to nodes when some information is requested by options
    • options should be an object, otherwise it treats as empty object
  • compress()
    • No more AST converting (performance boost and less memory consumption)
    • Drop outputAst option
    • Returns an object as result instead of AST (i.e. { ast: Object })
  • Drop methods: justDoIt(), stringify(), cleanInfo()