Skip to content

Commit fabd518

Browse files
authored
feat: add new test option (#45)
1 parent cb616c9 commit fabd518

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Import the plugin in your code:
3636
```js
3737
// Named import (recommended)
3838
import { ReactRefreshRspackPlugin } from "@rspack/plugin-react-refresh";
39-
4039
```
4140

4241
- CommonJS:
@@ -101,12 +100,31 @@ Compared to the previous approach, this method decouples the React Fast Refresh
101100

102101
## Options
103102

103+
### test
104+
105+
- Type: [Rspack.RuleSetCondition](https://rspack.dev/config/module#condition)
106+
- Default: `undefined`
107+
108+
Specifies which files should be processed by the React Refresh loader. This option is passed to the `builtin:react-refresh-loader` as the `rule.test` condition.
109+
110+
Works identically to Rspack's [rule.test](https://rspack.dev/config/module#ruletest) option.
111+
112+
```js
113+
new ReactRefreshPlugin({
114+
test: [/\.jsx$/, /\.tsx$/],
115+
});
116+
```
117+
104118
### include
105119

106120
- Type: [Rspack.RuleSetCondition](https://rspack.dev/config/module#condition)
107121
- Default: `/\.([cm]js|[jt]sx?|flow)$/i`
108122

109-
Include files to be processed by the plugin. The value is the same as the [rule.test](https://rspack.dev/config/module#ruletest) option in Rspack.
123+
Explicitly includes files to be processed by the React Refresh loader. This option is passed to the `builtin:react-refresh-loader` as the `rule.include` condition.
124+
125+
Use this to limit processing to specific directories or file patterns.
126+
127+
Works identically to Rspack's [rule.include](https://rspack.dev/config/module#ruleinclude) option.
110128

111129
```js
112130
new ReactRefreshPlugin({

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class ReactRefreshRspackPlugin {
8080

8181
if (this.options.injectLoader) {
8282
compiler.options.module.rules.unshift({
83+
test: this.options.test,
8384
// biome-ignore lint: exists
8485
include: this.options.include!,
8586
exclude: {

src/options.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ interface OverlayOptions {
1313

1414
export type PluginOptions = {
1515
/**
16-
* Include files to be processed by the plugin.
17-
* The value is the same as the `rule.test` option in Rspack.
18-
* @default /\.([cm]js|[jt]sx?|flow)$/i
16+
* Specifies which files should be processed by the React Refresh loader.
17+
* This option is passed to the `builtin:react-refresh-loader` as the `rule.test` condition.
18+
* Works identically to Rspack's `rule.test` option.
1919
* @see https://rspack.dev/config/module#ruletest
2020
*/
21+
test?: RuleSetCondition;
22+
/**
23+
* Explicitly includes files to be processed by the React Refresh loader.
24+
* This option is passed to the `builtin:react-refresh-loader` as the `rule.include` condition.
25+
* Use this to limit processing to specific directories or file patterns.
26+
* Works identically to Rspack's `rule.include` option.
27+
* @default /\.([cm]js|[jt]sx?|flow)$/i
28+
* @see https://rspack.dev/config/module#ruleinclude
29+
*/
2130
include?: RuleSetCondition | null;
2231
/**
2332
* Exclude files from being processed by the plugin.

test/test.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ describe('react-refresh-rspack-plugin', () => {
128128
);
129129
});
130130

131+
it('should test selected file when compiling', (done) => {
132+
compileWithReactRefresh(
133+
path.join(__dirname, 'fixtures/custom'),
134+
{
135+
exclude: null,
136+
test: path.join(__dirname, 'fixtures/node_modules/foo'),
137+
include: null,
138+
},
139+
(_, __, { vendor }) => {
140+
expect(vendor).toContain('function $RefreshReg$');
141+
done();
142+
},
143+
);
144+
});
145+
131146
it('should include selected file when compiling', (done) => {
132147
compileWithReactRefresh(
133148
path.join(__dirname, 'fixtures/custom'),

0 commit comments

Comments
 (0)