Preact refresh plugin for Rspack
npm install @rspack/plugin-preact-refreshPreact refresh plugin for Rspack.
First you need to install the dependencies:
``bash`
npm add @rspack/plugin-preact-refresh @prefresh/core @prefresh/utils -Dor
yarn add @rspack/plugin-preact-refresh @prefresh/core @prefresh/utils -Dor
pnpm add @rspack/plugin-preact-refresh @prefresh/core @prefresh/utils -Dor
bun 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
- Code injection: injects code that interacts with @prefresh/core and @prefresh/utils, this is what this plugin does.builtin:swc-loader
- Code transformation requires a loader
- Use or swc-loaderjsc.transform.react.refresh
- Enable to support common react transformation@swc/plugin-prefresh
- Add into jsc.experimental.plugins to support the specific transformation of preactbabel-loader
- Use and add official babel plugin of prefresh.
> In versions below 1.0.0, Rspack did not support preact refresh with swc-loader.
`js
const PreactRefreshPlugin = require("@rspack/plugin-preact-refresh");
const isDev = process.env.NODE_ENV === "development";
module.exports = {
// ...
mode: isDev ? "development" : "production",
module: {
rules: [
{
test: /\.jsx$/,
// exclude node_modules to avoid dependencies transformed by @swc/plugin-prefresh["preact", "preact/compat", "react"]
exclude: /node_modules/,
use: {
loader: "builtin:swc-loader",
options: {
jsc: {
experimental: {
plugins: [
[
// enable prefresh specific transformation
require.resolve("@swc/plugin-prefresh"),
{
library: ["preact-like-framework"], // the customizable preact name, default is `
},
],
],
},
parser: {
syntax: "ecmascript",
jsx: true,
},
transform: {
react: {
development: isDev,
refresh: isDev, // enable common react transformation
},
},
},
},
},
},
],
},
plugins: [isDev && new PreactRefreshPlugin()].filter(Boolean),
};
- Type: Rspack.RuleSetCondition
- Default: /\.([jt]sx?)$/
Include files to be processed by the plugin. The value is the same as the rule.test option in Rspack.
`js`
new PreactRefreshPlugin({
include: [/\.jsx$/, /\.tsx$/],
});
- Type: Rspack.RuleSetCondition
- Default: /node_modules/
Exclude files from being processed by the plugin. The value is the same as the rule.exclude option in Rspack.
`js`
new PreactRefreshPlugin({
exclude: [/node_modules/, /some-other-module/],
});
- Type: stringpath.dirname(require.resolve('preact/package.json'))
- Default:
Path to the preact package.
`js
const path = require("node:path");
new PreactRefreshPlugin({
preactPath: path.dirname(require.resolve("preact/package.json")),
});
``
See examples/preact-refresh for the full example.
Thanks to the prefresh created by @Jovi De Croock, which inspires implement this plugin.
Rspack is MIT licensed.