Using Plugins
There are 4 kinds of plugins supported in Farm:
Farm Compilation Plugins
: Support both Rust Plugins and Js Plugins, which adopt a rollup-style hooks.Vite/Rollup/Unplugin Plugin
: Vite/Rollup/Unplugin plugins are supported in Farm out of Box.Farm Runtime Plugin
: Adding abilities for Farm's runtime system.Swc Plugins
: Swc plugins are supported in Farm out of Box.
Farm adopt Vite/Rollup ecosystem, Vite/Rollup Plugins can be used directly in Farm.
For how to write your own plugins, refer to Writing Plugins
Farm Compilation Plugins
First, install the plugins your need, for example:
Using plugins
to configure Farm compilation plugins:
There are 2 kinds of Farm compilation plugins:
Rust Plugins
: which is written in Rust and has best performance.Js Plugins
: which is written in JS/TS, and it's used for compatibility with current JS ecosystem
Using Rust Plugins
Using package name
to configure a Rust Plugin, for example:
For above example, Farm will resolve package @farmfe/plugin-sass
and treat it as a Farm Rust Plugin.
If you want to configure options for rust plugins, you can use array syntax
like [packageName, optionsObject]
, for example:
Currently Farm supports 2 rust plugins officially:
@farmfe/plugin-react
: Farm rust plugin for react jsx compilation and react-refresh injection.@farmfe/plugin-sass
: Farm rust plugin for scss files compilation, usessass-embedded
internally.
To learn more about rust plugins, see Rust Plugins
Using Js Plugins
Farm JS plugin is a JS object with methods as hooks, for example:
farmPostcssPlugin()
returns a plugin object, and you can pass any postcss options by its arguments.
You can use priority
to control the order of your plugins, for example:
priority of internal plugin are 100, if you want the plugin execute first, set it larger than 100, otherwise set it smaller than 100.
If you want to add a Farm JS plugin quickly, you can just configure a plugin object:
filters
is required in Farm for js plugins. Because Js Plugin is really slow and we should avoid executing it as much as possible. For those modules that don't match the filters, Farm won't trigger js plugin hook for them at all! Which means Farm can handle them only on Rust side safely and concurrently.
To learn more about Farm Js Plugins, refer to JS Plugin
Using Vite/Rollup/Unplugin Plugins In Farm
Farm supports Vite plugins out of Box. First you need to install vite plugins,for example:
Then you can use vite plugins directly by vitePlugins
in farm.config.ts
.
To improve performance of vite plugins, you can use function syntax
that returns a filters
, for example:
Using unplugin:
configuring unplugin in vitePlugins
via unplugin/vite
or unplugin/rollup
:
Currently you can use unplugin/vite
or unplugin/rollup
. unplugin/farm
will be available as soon as this unplugin PR merged.
Farm Runtime Plugin
Farm has a runtime module system to control how to load and execute modules. Configuring compilation.runtime.plugins
to add more runtime plugin, for example:
you have to configure a path that point to your runtime plugin's entry. Recommend to a absolute path to avoid path issue.
To learn more about runtime plugin refer to Runtime Plugin
Using SWC Plugins
Swc Plugin can also be used directly in Farm, Configuring compilation.script.plugins
to add SWC plugins, for example:
Each plugin item of the array contains three fields:
- name: the package name of the swc plugin
- options: Configuration items passed to swc plugin
- filters: Which modules to execute the plug-in, must be configured, support
resolvedPaths
andmoduleTypes
these two filter items, if both are specified at the same time, take the union.
SWC plugin
may not be compatible with the SWC version
(rust crate swc_core v0.90
) that Farm uses. If a error occurred, try upgrade the plugin version.