Code Monkey home page Code Monkey logo

css-minifier-tests's Introduction

css-minifier-tests

A comparison of CSS minification engines.

Usage

Run the tests with:

node cli

Optionally, you can localise to an engine, or engines, of your choice by passing the --engines flag, with a comma separated list of engines:

node cli --engines cssnano,csso

And you can also run specific tests by passing the --suites flag.

node cli --suites 19,84

About

This project has been heavily inspired from GoalSmashers' benchmark, covering Node.js minifiers on a variety of CSS frameworks. But rather than comparing minification sizes at a macro-level, it intends to provide a suite of test cases on what the optimiser actually compresses. The aim of this project is to improve the quality of Node.js minifiers (and provide thorough test cases for new projects) - so that it isn't necessary to run several minifiers on a single CSS file.

See https://github.com/ben-eb/css-minifiers for details on which engines are covered.

The test cases do not resemble a standard unit test style of pass/fail, because each minifier may yield slightly different output to the others, even when the ending CSS size is the same. So they are graded on these criteria instead:

outstanding

The minification path chosen is considered 'above and beyond' what one would expect from a minifier; it either improved the structure of the CSS without losing its semantics, or it did some other transform which was beneficial. Note that this criteria does not appear on the majority of test cases.

optimal

The minifier performed a safe optimisation which reduced the size of the CSS.

sub-optimal

The size of the minified code is greater than an optimal optimisation, but it is still fine to use in production.

broken/crashed

The optimisation either breaks the author's original intentions, or a browser's capability to render the CSS generated by the minifier. Or worse, the minifier crashed when trying to parse the test case.

Limitations

Some of these tests may appear deliberately contrived to try and break the minifiers. Indeed, there are some tests that contain CSS that no sane person would write. However, this is intentional; it is entirely possible that such code can end up in minified stylesheets, through aggressive optimisations such as selector and declaration merging. By testing every circumstance, we can ensure that any code we throw at the minifier will not break our stylesheets.

Note that if an optimisation is classified as sub-optimal, it is still fine to use in production code. But, we can always do better - so that if only one minifier supports a safe optimisation which yields a smaller result than the rest, it will be the recommended (optimal) solution.

This test suite should not be a replacement for thorough unit testing of a CSS minifier - it should be used alongside one.

Why?

When searching for different CSS optimisation tools as part of my build process, it was found that running more than one tool on a single CSS file resulted in greater compression than using a single tool. So why is this? Well, many CSS compressors that exist out there today perform optimisations other than simple whitespace compression. The more advanced ones will apply structural minimisation to CSS; code that optimises the structure of a CSS file beyond the author's original intent. A simple example:

h1 {
    color: red;
}

h1 {
    font-weight: bold;
}

Structurally optimised, this can look like:

h1{color:red;font-weight:bold}

Whereas a traditional minifier will just collapse the spacing around the ruleset:

h1{color:red}h1{font-weight:bold}

This kind of optimisation is important when considering that many developers use CSS frameworks today; it means that authors can write additional styles on top of the framework, and have the minifier compress those rules together automatically.

Contributing

Thinking of contributing? Great! Fork away, do your work on a separate branch and send a pull request with your feature or fix. The aim of this project is to produce a robust suite of test cases that are small in scope; please do not contribute test cases that contain a full site's CSS. If you think that this project is missing test cases that you feel are relevant, then please:

  • Fork the project and create a branch for your test case.
  • Add a camelCased test directory in tests.
  • Create a fixture.css with your input. As much as possible, make your input test a limited case, so that tests can be small and meaningful.
  • Create an optimalX.css with the expected minified CSS - where X is any number of cases.
  • If some minifiers produce broken output, log it into brokenX.css - where X is any number of cases.
  • If some minifiers produce better than optimal output, log it into outstandingX.css - where X is any number of cases.
  • Optionally, add a README.md in the folder for further test case description.
  • Commit your changes and send the pull request.
  • You're awesome. :)

License

MIT © Ben Briggs

css-minifier-tests's People

Contributors

ben-eb avatar mattbasta avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

mattbasta

css-minifier-tests's Issues

CSS namespace tests use invalid CSS

The tests in suite 079 and 080 use namespaces incorrectly:

@namespace html "http://www.w3.org/1999/xhtml";
@namespace islands "http://bar.yandex.ru/ui/islands";

*|.test {
    color: red;
    margin: 0px;
}

*|.test is not a valid selector. The spec only allows element selectors to be placed immediately after the | character:

https://www.w3.org/TR/css3-selectors/#typenmsp

In these tests, a classname selector is used, which is invalid. The browser would conceivably drop the ruleset, or ignore the *| (or other namespace), meaning the "optimal" output would be to remove the ruleset entirely.

Perhaps replacing .test with a custom element name, like custom-elem would be better? It would be syntactically correct and be a good "real world" use case for namespaces.

HTML report

Need a way to show these results in-browser. CLI option works but is fiddly and requires people to clone this repo, npm install and then run the tests. It should be more readily available.

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.