A CSS parser, transformer, and minifier written in Rust.
@parcel/css
parses all values using the grammar from the CSS specification, and exposes a specific value type for each property.@parcel/css
is built on the cssparser and selectors crates created by Mozilla and used by Firefox and Servo. These provide a solid general purpose CSS-parsing foundation on top of which @parcel/css
implements support for all specific CSS rules and properties.@parcel/css
is to minify CSS to make it smaller. This includes many optimizations including:
calc()
expressions where possible.@parcel/css
accepts a list of browser targets, and automatically adds (and removes) vendor prefixes.@parcel/css
parses modern CSS syntax, and generates more compatible output where needed, based on browser targets.
rgb
and hsl
functionshwb()
color syntaxred 40% 80%
)clamp()
functionplace-items
)overflow
shorthand@media (width <= 100px)
or @media (100px < width < 500px)
)display
property (e.g. inline flex
)@parcel/css
supports compiling a subset of CSS modules features.
@keyframes
names, grid lines/areas, @counter-style
names, etc.:local()
and :global()
selectorscomposes
property@parcel/css
can be used from Parcel, as a standalone library from JavaScript or Rust, or wrapped as a plugin within any other tool.
More docs to come, but here is a simple example:
```js const css = require('@parcel/css');
let {code, map} = css.transform({ filename: 'style.css', code: Buffer.from('.foo { color: red }'), minify: true, sourceMap: true, targets: { // Semver versions are represented using a single 24-bit number, with one component per byte. // e.g. to represent 13.2.0, the following could be used. safari: (13 << 16) | (2 << 8) } }); ```
See the Rust API docs on docs.rs.
Add the following to your .parcelrc
:
json
{
"extends": "@parcel/config-default",
"optimizers": {
"*.css": ["@parcel/optimizer-css"]
}
}
``` $ node bench.js bootstrap-4.css cssnano: 542.956ms 159636 bytes
esbuild: 17.411ms 160332 bytes
parcel-css: 4.74ms 143985 bytes
$ node bench.js animate.css cssnano: 283.105ms 71723 bytes
esbuild: 11.858ms 72183 bytes
parcel-css: 1.989ms 23666 bytes
$ node bench.js tailwind.css cssnano: 2.198s 1925626 bytes
esbuild: 107.668ms 1961642 bytes
parcel-css: 45.701ms 1799209 bytes ```