swc/crates/swc_plugin
2022-02-12 14:25:32 +09:00
..
scripts refactor: Flatten crates (#2697) 2021-11-09 20:42:49 +09:00
src feat(cli): Add a command for scaffolding a new plugin (#3536) 2022-02-12 13:48:40 +09:00
tests fix(es/minifier): Fix bugs (#3238) 2022-01-12 16:57:54 +09:00
Cargo.toml chore: Publish crates 2022-02-12 14:25:32 +09:00
README.md feat(cli): Add a command for scaffolding a new plugin (#3536) 2022-02-12 13:48:40 +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 trasnform (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

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.