swc/crates/swc_plugin
2022-07-27 04:18:47 +00:00
..
scripts refactor: Flatten crates (#2697) 2021-11-09 20:42:49 +09:00
src feat(plugin): Make metadata api lazy (#5310) 2022-07-27 13:16:44 +09:00
Cargo.toml chore: Publish crates 2022-07-27 04:18:47 +00:00
README.md doc(plugin): Fix typo (#3547) 2022-02-13 01:16:52 +09:00

SWC Plugin

This crate provides necessary types and macros for creating a custom plugin for SWC (https://swc.rs/), which are WebAssembly modules that may be used to modify behavior of SWC. Currently only transform (https://swc.rs/docs/usage/core#transform) is supported.

Disclaimer: currently SWC plugin support is experimental, there may be possible breaking changes or unexpected behaviors. Please provide issues, feedbacks to https://github.com/swc-project/swc/discussions .

Writing a plugin

All plugin require adding crate-type = ['cdylib'] to the Cargo.toml. For a quick setup, SWC provides a simple commandline to create project for the plugin.

// if you haven't, add build targets for webassembly
rustup target add wasm32-wasi wasm32-unknown-unknown

cargo install swc_cli

swc plugin new ${plugin_name} --target-type wasm32-wasi

When create a new project cli require to specify target type between wasm32-wasi or wasm32-unknown-unknown. wasm32-unknown-unknown will generate slighly smaller binary, but it doesn't support system interfaces as well as macros like printn!().

Generated project will contain a fn with macro #[plugin_transform] which internally does necessary interop for communication between host to the plugin.

There are few references like SWC's jest transform for writing actual VisitMut.