Skip to content

Commit d4f754c

Browse files
authored
feat: add resourceQuery option for excluding by query (#34)
1 parent 6929416 commit d4f754c

File tree

6 files changed

+243
-185
lines changed

6 files changed

+243
-185
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Compared to the previous approach, this method decouples the React Fast Refresh
108108
- Type: [Rspack.RuleSetCondition](https://rspack.dev/config/module#condition)
109109
- Default: `/\.([cm]js|[jt]sx?|flow)$/i`
110110

111-
Include files to be processed by the plugin. The value is the same as the `rule.test` option in Rspack.
111+
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.
112112

113113
```js
114114
new ReactRefreshPlugin({
@@ -121,14 +121,29 @@ new ReactRefreshPlugin({
121121
- Type: [Rspack.RuleSetCondition](https://rspack.dev/config/module#condition)
122122
- Default: `/node_modules/`
123123

124-
Exclude files from being processed by the plugin. The value is the same as the `rule.exclude` option in Rspack.
124+
Exclude files from being processed by the plugin. The value is the same as the [rule.exclude](https://rspack.dev/config/module#ruleexclude) option in Rspack.
125125

126126
```js
127127
new ReactRefreshPlugin({
128128
exclude: [/node_modules/, /some-other-module/],
129129
});
130130
```
131131

132+
### resourceQuery
133+
134+
- Type: [Rspack.RuleSetCondition](https://rspack.dev/config/module#condition)
135+
- Default: `undefined`
136+
137+
Can be used to exclude certain resources from being processed by the plugin by the resource query. The value is the same as the [rule.resourceQuery](https://rspack.dev/config/module#ruleresourcequery) option in Rspack.
138+
139+
For example, to exclude all resources with the `raw` query, such as `import rawTs from './ReactComponent.ts?raw';`, use the following:
140+
141+
```js
142+
new ReactRefreshPlugin({
143+
resourceQuery: { not: /raw/ },
144+
});
145+
```
146+
132147
### forceEnable
133148

134149
- Type: `boolean`

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class ReactRefreshRspackPlugin {
8686
// biome-ignore lint: exists
8787
or: [this.options.exclude!, [...runtimePaths]].filter(Boolean),
8888
},
89+
resourceQuery: this.options.resourceQuery,
8990
use: ReactRefreshRspackPlugin.loader,
9091
});
9192
}

src/options.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,29 @@ export type PluginOptions = {
1616
* Include files to be processed by the plugin.
1717
* The value is the same as the `rule.test` option in Rspack.
1818
* @default /\.([cm]js|[jt]sx?|flow)$/i
19+
* @see https://rspack.dev/config/module#ruletest
1920
*/
2021
include?: RuleSetCondition | null;
2122
/**
2223
* Exclude files from being processed by the plugin.
2324
* The value is the same as the `rule.exclude` option in Rspack.
2425
* @default /node_modules/
26+
* @see https://rspack.dev/config/module#ruleexclude
2527
*/
2628
exclude?: RuleSetCondition | null;
29+
/**
30+
* Can be used to exclude certain resources from being processed by
31+
* the plugin by the resource query.
32+
* @see https://rspack.dev/config/module#ruleresourcequery
33+
*
34+
* @example
35+
* To exclude all resources with the `raw` query, such as
36+
* `import rawTs from './ReactComponent.ts?raw';`, use the following:
37+
* ```ts
38+
* { resourceQuery: { not: /raw/ } }
39+
* ```
40+
*/
41+
resourceQuery?: RuleSetCondition;
2742
/**
2843
* Sets a namespace for the React Refresh runtime.
2944
* It is most useful when multiple instances of React Refresh is running

test/fixtures/query/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'foo';

test/fixtures/query/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./foo?raw');

0 commit comments

Comments
 (0)