Skip to content

Commit 50043af

Browse files
authored
feat: allow to override the default react-refresh-loader (#70)
1 parent 80bde84 commit 50043af

File tree

8 files changed

+278
-165
lines changed

8 files changed

+278
-165
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ new ReactRefreshPlugin({
227227
});
228228
```
229229

230+
### reactRefreshLoader
231+
232+
- Type: `string`
233+
- Default: `builtin:react-refresh-loader`
234+
235+
Be default, the plugin uses `builtin:react-refresh-loader` loader implementation [from Rspack](https://github.com/web-infra-dev/rspack/tree/main/crates/rspack_loader_react_refresh) in order ot inject
236+
the React Refresh utilities into each module. `reactRefreshLoader` option allows to specify the loader, that implements
237+
custom React Refresh instrumentation if needed.
238+
239+
```js
240+
new ReactRefreshPlugin({
241+
reactRefreshLoader: 'my-advanced-react-refresh-loader',
242+
});
243+
```
244+
230245
## Credits
231246

232247
Thanks to the [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) created by [@pmmmwh](https://github.com/pmmmwh), which inspires implement this plugin.

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class ReactRefreshRspackPlugin {
4242
return getRefreshRuntimePaths();
4343
}
4444

45-
static loader = 'builtin:react-refresh-loader';
46-
4745
constructor(options: PluginOptions = {}) {
4846
this.options = normalizeOptions(options);
4947
}
@@ -104,7 +102,7 @@ class ReactRefreshRspackPlugin {
104102
// React Refresh should not be injected for asset modules as they are static resources
105103
not: ['url'],
106104
},
107-
use: ReactRefreshRspackPlugin.loader,
105+
use: this.options.reactRefreshLoader,
108106
});
109107
}
110108

src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export type PluginOptions = {
8585
* @default false
8686
*/
8787
reloadOnRuntimeErrors?: boolean;
88+
/**
89+
* Allows to specify custom react-refresh loader
90+
* @default "builtin:react-refresh-loader"
91+
*/
92+
reactRefreshLoader?: string;
8893
};
8994

9095
export interface NormalizedPluginOptions extends Required<PluginOptions> {
@@ -135,6 +140,7 @@ export function normalizeOptions(
135140
d(options, 'injectLoader', true);
136141
d(options, 'injectEntry', true);
137142
d(options, 'reloadOnRuntimeErrors', false);
143+
d(options, 'reactRefreshLoader', 'builtin:react-refresh-loader');
138144
options.overlay = normalizeOverlay(options.overlay);
139145
return options as NormalizedPluginOptions;
140146
}

test/exports.spec.mts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import DefaultExport, { ReactRefreshRspackPlugin } from '../exports/index.mjs';
22

33
test('should allow to import from the package', () => {
4-
expect(DefaultExport.loader).toBeTruthy();
4+
const instance = new ReactRefreshRspackPlugin();
5+
expect(instance.options.reactRefreshLoader).toBeTruthy();
6+
});
7+
8+
test('should allow the default import from the package', () => {
9+
const instance = new DefaultExport();
10+
expect(instance.options.reactRefreshLoader).toBeTruthy();
11+
});
12+
13+
test('default import picks the same plugin class', () => {
514
expect(DefaultExport).toEqual(ReactRefreshRspackPlugin);
615
});

test/exports.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const DefaultExport = require('../exports/index.cjs');
22

33
test('should allow to require from the package', () => {
4-
expect(DefaultExport.loader).toBeTruthy();
4+
const instance = new DefaultExport();
5+
expect(instance.options.reactRefreshLoader).toBeTruthy();
56
});

test/fixtures/loader/index.js

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

test/fixtures/loader/loader.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function customLoader(source, sourceMap) {
2+
const callback = this.async();
3+
4+
const injected = `/** TEST_LOADER */`;
5+
const result = `${source}\n${injected}`;
6+
7+
callback(null, result, sourceMap);
8+
};

0 commit comments

Comments
 (0)