|
| 1 | +# swc-plugin-css-variable |
| 2 | + |
| 3 | +This is the SWC counterpart to the babel plugin of this library. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +This is a workspace with two crates: |
| 8 | + |
| 9 | +- `swc-plugin-css-variable` is the outer shell and only contains what's |
| 10 | + necessary to interface with SWC, which is essentially the main function that |
| 11 | + is decorated with `#[plugin_transform]` |
| 12 | +- `transform` has everything else, most importantly the visitor implementation |
| 13 | + |
| 14 | +The reason for the split is to enable benchmarking inside Wasm runtimes. If the |
| 15 | +code under test (the visitor) were in the same crate as `#[plugin_transform]`, |
| 16 | +code would be generated that requires functions to be linked in for accessing |
| 17 | +the environment and emitting diagnostics. Normally these are provided at |
| 18 | +runtime by SWC, but for only running benchmarks we don't want to have to |
| 19 | +fake them. |
| 20 | + |
| 21 | +## Benchmarking |
| 22 | + |
| 23 | +The benchmarks can be run easily with `cargo bench`. Note that this builds and |
| 24 | +runs native code on your machine, which may not exhibit the exact same |
| 25 | +performance characteristics as a Wasm module in a runtime, as is the case for |
| 26 | +SWC transformations. |
| 27 | + |
| 28 | +To account for this, it's also possible to build a benchmark executable in Wasm |
| 29 | +format and execute it in your runtime of choice. Since SWC internally uses |
| 30 | +[Wasmer](https://wasmer.io), it makes sense to install this one. Then, it's |
| 31 | +just a matter of |
| 32 | + |
| 33 | +```console |
| 34 | +$ cargo build --bench bench_main --release --target wasm32-wasi |
| 35 | +# Get the name of the Wasm module we just built |
| 36 | +$ ls -t target/wasm32-wasi/release/deps/*.wasm | head -n 1 |
| 37 | +target/wasm32-wasi/release/deps/bench_main-9530540cc15e2e67.wasm |
| 38 | +# Execute the benchmark |
| 39 | +$ wasmer target/wasm32-wasi/release/deps/bench_main-9530540cc15e2e67.wasm -- --bench |
| 40 | +``` |
| 41 | + |
| 42 | +With this you get the most accurate runtime behavior and can observe the |
| 43 | +difference to native code. However, you'll most likely find it negligible, with |
| 44 | +Wasm being just slightly slower and everything else scaling mostly linearly. A |
| 45 | +typical example: |
| 46 | + |
| 47 | +- `wasmer: visitor styledPage time: [6.6177 µs 6.6316 µs 6.6495 µs]` |
| 48 | +- `native: visitor styledPage time: [5.4191 µs 5.4736 µs 5.5150 µs]` |
| 49 | + |
| 50 | +For most changes that aren't Wasm-specific, it's therefore often good enough to |
| 51 | +just run the benchmarks normally with `cargo bench`. |
0 commit comments