Lightning CSS with Rust
Hystrelius January 26, 2025 [Software] #rust #webLightning CSS ⚡ is a way to minify CSS files and ensure backwards compatibility with a range of browsers. This blog post will demonstrate how to use Lightning CSS in Rust to manipulate CSS files.
This blog post will describe how to read a CSS file from disk then bundle, minify and compile it for a range of different CSS versions.
Installing Lightning CSS
We need to first install lightningcss into our Rust project, as it is still beta at the time of writing - the installing process is a bit different.
[]
= "^1.0.0-alpha.65"
Make sure to check the latest version before installing
Then the main items we need to install are:
use ;
All these options all us to interact with our CSS file and modify it correctly.
Reading a CSS file
Make sure that fs
is installed first:
use fs;
Then we can read the CSS file from disk:
By loading it into the bundler, we are able to combine a range of disparate CSS files into a single file.
To minify the CSS we can adjust the MinifyOptions
through the minify
command:
stylesheet
.minify
.expect;
This will try to minify the CSS to reduce the size of the CSS file reducing the load on your user’s browsers.
Minification is a two stage process, now we need to “print” the CS file which will “crystallise” the minification:
let res = stylesheet
.to_css
.expect;
Then we can write the CSS to a file:
.expect;
write
This will
Results
This can turn CSS like this:
}
into this:
}
* code taken from rust-lang.org
It may not look like much, but it is cutting down on a character here or there, and when you have a large chunk of CSS, it can save a lot.
Or you can even minify it into this:
}
Working example
A working example of this can be found here on my GitHub: https://github.com/Hysterelius/lightningcss_rust, it’s under the Unlicense license, so feel free to do whatever you want with it.
p.s. Targetting different browsers
One of the biggest features of using LightningCSS is being able to target different browsers and versions, and transpile the CSS to work for them, so that your site is more accessible to a wider audience.
This is done by specifying browsers by their major.minor.patch version, so for example:
Browsers
This selects browsers by their major versions, but these are a pain to write - so we can use from_browserslist()
to convert a browserslist string into a Browsers
object:
Ensure that you have the browserslist
feature enabled in your Cargo.toml
with cargo add lightningcss --features browserslist
, and then you can use it like this:
let browsers: Targets = from_browserslist
.expect
.unwrap
.into;
We convert it into a Targets
object which is used in both the minification and printing stages. We can put the browserslist strings in the vec
, which is the strings from the https://browsersl.ist/ site.
- By targetting
>0.1%
we are targetting all browsers with a market share of over 0.1%, which is a good starting point and covers over 93.3% of users.
Then we adjust our code to be like this:
let browsers: Targets = from_browserslist
.expect
.unwrap
.into;
stylesheet
.minify
.expect;
let res = stylesheet
.to_css
.expect;
This will then minify the CSS to work for the browsers specified in the browserslist
string.
Conclusion
Through LightningCSS we can ensure that we create CSS code and file which are accessible to a wider audience, and