How did you shave off 0.00 seconds?
This assumes it's a TypeScript library. I've pushed the Plumeria CSS compilation speed to the limit, so I'd like to explain what I did to achieve it.
Use fast Rust binaries
Due to the nature of the CSS library, I tried to avoid framework lock-in as much as possible.
I use glob to collect js, jsx, ts, tsx, vue, svelte.
I created the library @rust-gear/glob when fs.glob was still unstable.
At the time, Node.js's fs.glob was not yet stable, so I had to create a stable version myself.
By directly calling the Rust globset crate with napi-rs, I was able to achieve speeds two to three times faster than the usual fs.globSync.
The dependencies are as follows:
napi = { version = "2", features = ["tokio_rt"] }
napi-derive = "2.12.2"
globset = "0.4"
ignore = "0.4"
tokio = { version = "1", features = ["rt-multi-thread"] }
Using the JS V8 Engine Directly
The code collected above needs to be executed directly in memory. To do this, we used the following method to directly evaluate it on the optimized JS V8 engine.
1. Evaluate in Node.js V8 using await import
2. Use Node module loader to have Node.js V8 interpret the code
3. Create a sandbox using Node.js's VM and evaluate in the VM's V8
4. Evaluate directly in the JS engine's V8 using eval (security concerns have been raised)
5. Create a script using new Function and evaluate it directly in the JS engine's V8
Of these, the last method, evaluating using new Function, was ultimately selected.
The reasons for this were: it is significantly faster than 1 and 2; it can be evaluated more quickly than 3; and it can be evaluated more safely than 4.
Since everything needed to be converted to commonJS, @swc/core was used for AST parsing.
This library is also implemented in Rust and is extremely fast and powerful.
Simply put, "rscute is a wrapper around new Function."
rscute is a lightweight execution environment based on new Function.
It instantly converts TypeScript/ESM to CJS and instantly evaluates it on V8.
CSS Compiler Dependencies
The compiler only collects and executes commands.
@rust-gear/glob: Collects files
@swc/core: For AST analysis of files
rscute: For execution
lightningcss: Responsible for final CSS minification
postcss: For final media query adjustments
Thanks to the fast runtimes of three Rust-based libraries, Plumeria CSS compilation reaches a speed that can be described as "effectively almost 1/4 second."