Skip to content

Commit 3802d58

Browse files
authored
fix!: preserve default extension filter when include is customized (#100)
1 parent 1f5a44a commit 3802d58

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Compared to the previous approach, this method decouples the React Fast Refresh
8989
### test
9090

9191
- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
92-
- Default: `undefined`
92+
- Default: `/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/`
9393

9494
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.
9595

@@ -104,7 +104,7 @@ new ReactRefreshPlugin({
104104
### include
105105

106106
- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
107-
- Default: `/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/`
107+
- Default: `undefined`
108108

109109
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.
110110

src/options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type PluginOptions = {
55
* Specifies which files should be processed by the React Refresh loader.
66
* This option is passed to the `builtin:react-refresh-loader` as the `rule.test` condition.
77
* Works identically to Rspack's `rule.test` option.
8+
* @default /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/
89
* @see https://rspack.rs/config/module-rules#rulestest
910
*/
1011
test?: RuleSetCondition;
@@ -13,7 +14,7 @@ export type PluginOptions = {
1314
* This option is passed to the `builtin:react-refresh-loader` as the `rule.include` condition.
1415
* Use this to limit processing to specific directories or file patterns.
1516
* Works identically to Rspack's `rule.include` option.
16-
* @default /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/
17+
* @default undefined
1718
* @see https://rspack.rs/config/module-rules#rulesinclude
1819
*/
1920
include?: RuleSetCondition | null;
@@ -93,8 +94,8 @@ const d = <K extends keyof PluginOptions>(
9394
export function normalizeOptions(
9495
options: PluginOptions,
9596
): NormalizedPluginOptions {
97+
d(options, 'test', /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/);
9698
d(options, 'exclude', /node_modules/i);
97-
d(options, 'include', /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/);
9899
d(options, 'library');
99100
d(options, 'forceEnable', false);
100101
d(options, 'injectLoader', true);

test/fixtures/include/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './style.css';
2+
3+
export default 'include';

test/fixtures/include/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.include {
2+
color: red;
3+
}

test/test.spec.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Outputs = {
1212
fixture: string;
1313
runtime: string;
1414
vendor: string;
15+
css?: string;
1516
};
1617

1718
type CompilationResult = {
@@ -22,6 +23,10 @@ type CompilationResult = {
2223
};
2324

2425
const uniqueName = 'ReactRefreshLibrary';
26+
const readOutput = (fixturePath: string, file: string) =>
27+
fs.existsSync(path.join(fixturePath, 'dist', file))
28+
? fs.readFileSync(path.join(fixturePath, 'dist', file), 'utf-8')
29+
: '';
2530

2631
const compileWithReactRefresh = (
2732
fixturePath: string,
@@ -32,7 +37,9 @@ const compileWithReactRefresh = (
3237
const cjsEntry = path.join(fixturePath, 'index.js');
3338
const ctsEntry = path.join(fixturePath, 'index.cjs');
3439
const mjsEntry = path.join(fixturePath, 'index.mjs');
35-
const customLoader = path.join(fixturePath, 'loader.cjs');
40+
const customLoader = fs.existsSync(path.join(fixturePath, 'loader.cjs'))
41+
? path.join(fixturePath, 'loader.cjs')
42+
: path.join(import.meta.dirname, 'fixtures/loader/loader.cjs');
3643
const entry = fs.existsSync(cjsEntry)
3744
? cjsEntry
3845
: fs.existsSync(ctsEntry)
@@ -52,6 +59,14 @@ const compileWithReactRefresh = (
5259
uniqueName,
5360
assetModuleFilename: '[name][ext]',
5461
},
62+
module: {
63+
rules: [
64+
{
65+
test: /\.css$/,
66+
type: 'css/auto',
67+
},
68+
],
69+
},
5570
resolveLoader: {
5671
alias: {
5772
'custom-react-refresh-loader': customLoader,
@@ -112,22 +127,11 @@ const compileWithReactRefresh = (
112127
error,
113128
stats,
114129
outputs: {
115-
reactRefresh: fs.readFileSync(
116-
path.join(fixturePath, 'dist', 'react-refresh.js'),
117-
'utf-8',
118-
),
119-
fixture: fs.readFileSync(
120-
path.join(fixturePath, 'dist', 'fixture.js'),
121-
'utf-8',
122-
),
123-
runtime: fs.readFileSync(
124-
path.join(fixturePath, 'dist', 'runtime.js'),
125-
'utf-8',
126-
),
127-
vendor: fs.readFileSync(
128-
path.join(fixturePath, 'dist', 'vendor.js'),
129-
'utf-8',
130-
),
130+
reactRefresh: readOutput(fixturePath, 'react-refresh.js'),
131+
fixture: readOutput(fixturePath, 'fixture.js'),
132+
runtime: readOutput(fixturePath, 'runtime.js'),
133+
vendor: readOutput(fixturePath, 'vendor.js'),
134+
css: readOutput(fixturePath, 'fixture.css') || undefined,
131135
},
132136
plugin,
133137
});
@@ -301,4 +305,19 @@ describe('react-refresh-rspack-plugin', () => {
301305
expect(fixture).toContain('TEST_LOADER');
302306
expect(fixture).not.toContain('function $RefreshReg$');
303307
});
308+
309+
it('should keep the default extension filter when include targets a directory', async () => {
310+
const {
311+
outputs: { fixture, css },
312+
} = await compileWithReactRefresh(
313+
path.join(import.meta.dirname, 'fixtures/include'),
314+
{
315+
include: path.join(import.meta.dirname, 'fixtures/include'),
316+
reactRefreshLoader: 'custom-react-refresh-loader',
317+
},
318+
);
319+
expect(fixture).toContain('TEST_LOADER');
320+
expect(css).toBeDefined();
321+
expect(css).not.toContain('TEST_LOADER');
322+
});
304323
});

0 commit comments

Comments
 (0)