Using SWC provides the same ability and configuration as babel-plugin-import
.
Since @modern-js/module-tools
version >= 2.16.0, this plugin functionality is built into Modern.js Module and is provided by transformImport
.
In Modern.js Module, you can register plugins in the following way:
This way we can use the ability of automatic import in Modern.js Module.
object[]
The elements of the array are configuration objects for babel-plugin-import
, which can be referred to options。
Example:
SWC (Speedy Web Compiler) is written in Rust, and this plugin is based on SWC and ported from babel-plugin-import. The configuration options remain consistent.
Some configurations can be passed in as functions, such as customName
, customStyleName
, etc., but in Modern.js Module, we do not recommend using functions in these configuration items.
Because we will call SWC in the esbuild plugin, and then when Rust calls these configuration functions through Node-API, a deadlock will occur.
Simple function logic can actually be replaced by template language. Below is an example of using a template with customName
:
Add the following configuration on the right-hand side:
The {{ member }}
in it will be replaced with the corresponding import member. After transformation:
Template customName: 'foo/es/{{ member }}'
is the same as customName: (member) => `foo/es/${member}`
, but template value has no performance overhead of Node-API.
The template used here is handlebars. There are some useful builtin tools, Take the above import statement as an example:
Transformed to:
In addition to kebabCase, there are cameraCase, snakeCase, upperCase and lowerCase can be used as well.