Skip to content

Commit 02b2592

Browse files
pmmmwhnicholasio
andauthored
fix: allow exclusion of dynamically generated modules (#769)
Co-authored-by: =?UTF-8?q?N=C3=ADcholas=20Oliveira?= <[email protected]>
1 parent 4dc633d commit 02b2592

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

lib/utils/injectRefreshLoader.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,33 @@ const refreshUtilsPath = path.join(__dirname, '../runtime/RefreshUtils');
2525
function injectRefreshLoader(moduleData, injectOptions) {
2626
const { match, options } = injectOptions;
2727

28+
// Include and exclude user-specified files
29+
if (!match(moduleData.matchResource || moduleData.resource)) return moduleData;
30+
// Include and exclude dynamically generated modules from other loaders
31+
if (moduleData.matchResource && !match(moduleData.request)) return moduleData;
32+
// Exclude files referenced as assets
33+
if (moduleData.type.includes('asset')) return moduleData;
34+
// Check to prevent double injection
35+
if (moduleData.loaders.find(({ loader }) => loader === resolvedLoader)) return moduleData;
36+
// Skip react-refresh and the plugin's runtime utils to prevent self-referencing -
37+
// this is useful when using the plugin as a direct dependency,
38+
// or when node_modules are specified to be processed.
2839
if (
29-
// Include and exclude user-specified files
30-
match(moduleData.matchResource || moduleData.resource) &&
31-
// Exclude files referenced as assets
32-
!moduleData.type.includes('asset') &&
33-
// Skip react-refresh and the plugin's runtime utils to prevent self-referencing -
34-
// this is useful when using the plugin as a direct dependency,
35-
// or when node_modules are specified to be processed.
36-
!moduleData.resource.includes(reactRefreshPath) &&
37-
!moduleData.resource.includes(refreshUtilsPath) &&
38-
// Check to prevent double injection
39-
!moduleData.loaders.find(({ loader }) => loader === resolvedLoader)
40+
moduleData.resource.includes(reactRefreshPath) ||
41+
moduleData.resource.includes(refreshUtilsPath)
4042
) {
41-
// As we inject runtime code for each module,
42-
// it is important to run the injected loader after everything.
43-
// This way we can ensure that all code-processing have been done,
44-
// and we won't risk breaking tools like Flow or ESLint.
45-
moduleData.loaders.unshift({
46-
loader: resolvedLoader,
47-
options,
48-
});
43+
return moduleData;
4944
}
5045

46+
// As we inject runtime code for each module,
47+
// it is important to run the injected loader after everything.
48+
// This way we can ensure that all code-processing have been done,
49+
// and we won't risk breaking tools like Flow or ESLint.
50+
moduleData.loaders.unshift({
51+
loader: resolvedLoader,
52+
options,
53+
});
54+
5155
return moduleData;
5256
}
5357

0 commit comments

Comments
 (0)