website/docs/en/guide/tech/preact.mdx
import { PackageManagerTabs } from '@theme'; import { Stability } from '@components/ApiMeta.tsx';
Rspack provides two solutions to support Preact:
Rspack leverages SWC transformer for JSX/TSX.
Add builtin:swc-loader loader to support jsx and tsx:
export default {
module: {
rules: [
{
test: /\.(?:js|mjs|jsx|ts|tsx)$/,
use: {
loader: 'builtin:swc-loader',
options: {
detectSyntax: 'auto',
},
},
type: 'javascript/auto',
},
],
},
};
Refer to Builtin swc-loader for detailed configurations.
Refer to examples/preact for the full example.
To enable Preact Refresh, the following steps are required:
@rspack/plugin-preact-refresh plugin to inject runtime codeFirst you need to install the dependencies:
<PackageManagerTabs command="add @rspack/plugin-preact-refresh @prefresh/core @prefresh/utils -D" />The enabling of the Preact Refresh is divided into two parts: code injection and code transformation
@prefresh/core and @prefresh/utils, which has been integrated in the @rspack/plugin-preact-refresh pluginbuiltin:swc-loader or swc-loader
jsc.transform.react.refresh to support common react transformation@swc/plugin-prefresh into jsc.experimental.plugins to support the specific transformation of preactbabel-loader and add official babel plugin of prefresh.:::warning
In versions below 1.0.0, Rspack did not support preact refresh with swc-loader.
Please use builtin:swc-loader and enable preact specific transformation with rspackExperiments.preact: {}
:::
import PreactRefreshPlugin from '@rspack/plugin-preact-refresh';
const isDev = process.env.NODE_ENV === 'development';
export default {
mode: isDev ? 'development' : 'production',
module: {
rules: [
{
test: /\.(?:js|mjs|jsx|ts|tsx)$/,
use: {
loader: 'builtin:swc-loader',
options: {
detectSyntax: 'auto',
jsc: {
experimental: {
plugins: [
[
'@swc/plugin-prefresh', // enable prefresh specific transformation
{
library: ['preact-like-framework'], // the customizable preact name, default is `["preact", "preact/compat", "react"]`
},
],
],
},
transform: {
react: {
development: isDev,
refresh: isDev, // enable common react transformation
},
},
},
},
},
},
],
},
plugins: [
isDev && new PreactRefreshPlugin(),
isDev && new rspack.HotModuleReplacementPlugin(),
],
};
Refer to examples/preact-refresh for the full example.