Skip to content

Commit cf140f0

Browse files
authored
feat(plugin-preact): add include and exclude options (#3835)
1 parent 1199b02 commit cf140f0

File tree

7 files changed

+92
-12
lines changed

7 files changed

+92
-12
lines changed
+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { pluginPreact } from '@rsbuild/plugin-preact';
22

33
export default {
4-
plugins: [pluginPreact()],
4+
plugins: [
5+
pluginPreact({
6+
exclude: [
7+
/node_modules/,
8+
// exclude Rsbuild internal HMR client
9+
/packages\/core\/dist/,
10+
],
11+
}),
12+
],
513
};
+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { pluginPreact } from '@rsbuild/plugin-preact';
22

33
export default {
4-
plugins: [pluginPreact()],
4+
plugins: [
5+
pluginPreact({
6+
exclude: [
7+
/node_modules/,
8+
// exclude Rsbuild internal HMR client
9+
/packages\/core\/dist/,
10+
],
11+
}),
12+
],
513
};

packages/plugin-preact/src/index.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ export type PluginPreactOptions = {
1414
* @default true
1515
*/
1616
prefreshEnabled?: boolean;
17+
/**
18+
* Include files to be processed by the `@rspack/plugin-preact-refresh` plugin.
19+
* The value is the same as the `rule.test` option in Rspack.
20+
* @default /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/
21+
*/
22+
include?: Rspack.RuleSetCondition;
23+
/**
24+
* Exclude files from being processed by the `@rspack/plugin-preact-refresh` plugin.
25+
* The value is the same as the `rule.exclude` option in Rspack.
26+
* @default /[\\/]node_modules[\\/]/
27+
*/
28+
exclude?: Rspack.RuleSetCondition;
1729
};
1830

1931
export const PLUGIN_PREACT_NAME = 'rsbuild:preact';
@@ -25,6 +37,8 @@ export const pluginPreact = (
2537

2638
setup(api) {
2739
const options = {
40+
include: /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
41+
exclude: /[\\/]node_modules[\\/]/,
2842
prefreshEnabled: true,
2943
reactAliasesEnabled: true,
3044
...userOptions,
@@ -101,20 +115,14 @@ export const pluginPreact = (
101115
'@rspack/plugin-preact-refresh'
102116
);
103117

104-
const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
105118
const preactPath = require.resolve('preact', {
106119
paths: [api.context.rootPath],
107120
});
108121

109122
chain.plugin('preact-refresh').use(PreactRefreshPlugin, [
110123
{
111-
include: [SCRIPT_REGEX],
112-
exclude: [
113-
/node_modules/,
114-
// exclude Rsbuild internal HMR client
115-
// TODO: use better way to exclude
116-
/packages\/core\/dist/,
117-
],
124+
include: options.include,
125+
exclude: options.exclude,
118126
preactPath,
119127
},
120128
]);

website/docs/en/plugins/list/plugin-preact.mdx

+28
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,31 @@ pluginPreact({
6060
prefreshEnabled: false,
6161
});
6262
```
63+
64+
### include
65+
66+
Include files to be processed by the [@rspack/plugin-preact-refresh](https://github.com/rspack-contrib/rspack-plugin-preact-refresh) plugin. The value is the same as the `rule.test` option in Rspack.
67+
68+
- **Type:** [RuleSetCondition](https://rspack.dev/zh/config/module#condition)
69+
- **Default:** `/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/`
70+
- **Version:** `>= v1.1.0`
71+
72+
```ts
73+
pluginPreact({
74+
include: [/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/, /some-other-module/],
75+
});
76+
```
77+
78+
### exclude
79+
80+
Exclude files from being processed by the [@rspack/plugin-preact-refresh](https://github.com/rspack-contrib/rspack-plugin-preact-refresh) plugin. The value is the same as the `rule.exclude` option in Rspack.
81+
82+
- **Type:** [RuleSetCondition](https://rspack.dev/zh/config/module#condition)
83+
- **Default:** `/[\\/]node_modules[\\/]/`
84+
- **Version:** `>= v1.1.0`
85+
86+
```ts
87+
pluginPreact({
88+
exclude: [/[\\/]node_modules[\\/]/, /some-other-module/],
89+
});
90+
```

website/docs/zh/config/source/exclude.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# source.exclude
22

3-
- **类型:** [RuleSetCondition[]](https://rspack.dev/zh/config/module#condition)
3+
- **类型:** [RuleSetCondition](https://rspack.dev/zh/config/module#condition)
44
- **默认值:** `[]`
55

66
指定不需要编译的 JavaScript/TypeScript 文件。用法与 Rspack 中的 [Rule.exclude](https://rspack.dev/zh/config/module#ruleexclude) 一致,支持传入字符串或正则表达式来匹配模块的路径。

website/docs/zh/config/source/include.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# source.include
22

3-
- **类型:** [RuleSetCondition[]](https://rspack.dev/zh/config/module#condition)
3+
- **类型:** [RuleSetCondition](https://rspack.dev/zh/config/module#condition)
44
- **默认值:**
55

66
```ts

website/docs/zh/plugins/list/plugin-preact.mdx

+28
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,31 @@ pluginPreact({
6060
prefreshEnabled: false,
6161
});
6262
```
63+
64+
### include
65+
66+
指定要由 [@rspack/plugin-preact-refresh](https://github.com/rspack-contrib/rspack-plugin-preact-refresh) 插件处理的文件。这个值与 Rspack 中的 `rule.test` 选项相同。
67+
68+
- **类型:** [RuleSetCondition](https://rspack.dev/zh/config/module#condition)
69+
- **默认值:** `/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/`
70+
- **版本:** `>= v1.1.0`
71+
72+
```ts
73+
pluginPreact({
74+
include: [/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/, /some-other-module/],
75+
});
76+
```
77+
78+
### exclude
79+
80+
排除 [@rspack/plugin-preact-refresh](https://github.com/rspack-contrib/rspack-plugin-preact-refresh) 插件处理的文件。这个值与 Rspack 中的 `rule.exclude` 选项相同。
81+
82+
- **类型:** [RuleSetCondition](https://rspack.dev/zh/config/module#condition)
83+
- **默认值:** `/[\\/]node_modules[\\/]/`
84+
- **版本:** `>= v1.1.0`
85+
86+
```ts
87+
pluginPreact({
88+
exclude: [/[\\/]node_modules[\\/]/, /some-other-module/],
89+
});
90+
```

0 commit comments

Comments
 (0)