builtin:swc-loader
is the Rust version of swc-loader
, aiming to deliver better performance. The Loader's configuration is aligned with the JS version of swc-loader
.
If you need to use builtin:swc-loader
in your project, configure it as follows:
To transpile .ts
files:
To transpile React's .jsx
files:
SWC provides jsc.target and env.targets to specify the target of JavaScript syntax lowering.
jsc.target is used to specify the ECMA version, such as es5
, es2015
, es2016
, etc.
env.targets uses the browserslist syntax to specify browser range, for example:
jsc.target
and env.targets
cannot be configured at the same time, choose one according to your needs.
When using higher versions of JavaScript syntax and APIs in your project, to ensure that the compiled code can run in lower version browsers, you will typically need to perform two parts of the downgrade: syntax downgrading and polyfill injection.
SWC supports injecting core-js as an API polyfill, which can be configured using env.mode and env.coreJs:
You can enable type hints using the SwcLoaderOptions
type exported by @rspack/core
:
rspack.config.js
:rspack.config.ts
:The following is an introduction to some SWC configurations and Rspack specific configurations. Please refer to the SWC Configurations for the complete options.
The Wasm plugin is deeply coupled with the version of SWC, you need to choose a Wasm plugin that is compatible with the corresponding version of SWC in order to function normally.selecting-swc-core.
you can see more compatible info about how to choose right Wasm plugin version in
Rspack supports load Wasm plugin in builtin:swc-loader
, you can specify the plugin name like
this is an example of Wasm plugin usage.
When you use SWC's Wasm plugin, SWC will generate cache files in the .swc
directory of the current project by default. If you want to adjust this directory, you can modify the cacheRoot
configuration, such as:
Experimental features provided by rspack.
Ported from babel-plugin-import, configurations are basically the same.
Function can't be used in configurations, such as customName
, customStyleName
, they will cause some performance overhead as these functions must be called from Rust
, inspired by modularize_imports, some simple function can be replaced by template string instead. Therefore, the function type configuration such as customName
, customStyleName
can be passed in strings as templates to replace functions and improve performance.
For example:
Apply following configurations:
{{ member }}
will be replaced by the imported specifier:
Template customName: 'foo/es/{{ member }}'
is the same as customName: (member) => `foo/es/${member}`
, but template string has no performance overhead of Node-API.
The template used here is handlebars. There are some useful builtin helpers, Take the above import statement as an example:
Transformed to:
In addition to kebabCase
, there are camelCase
, snakeCase
, upperCase
, lowerCase
and legacyKebabCase
/legacySnakeCase
can be used as well.
The legacyKebabCase
/legacySnakeCase
works as babel-plugin-import versions before 1.13.7.
You can check the document of babel-plugin-import for other configurations.
Taking the classic 4.x version of ant-design as an example, we only need to configure it as follows:
The above configuration will transform import { Button } from 'antd'
; to:
Then you can see the style file is automatically imported and applied on the page.
Of course, if you have already configured support for less
, you can simply use the following configuration:
The above configuration will transform import { Button } from 'antd';
to: