Skip to content

Commit

Permalink
fix: allow exclusion of dynamically generated modules (#769)
Browse files Browse the repository at this point in the history
Co-authored-by: =?UTF-8?q?N=C3=ADcholas=20Oliveira?= <nicholas.andre@10up.com>
  • Loading branch information
pmmmwh and nicholasio committed Aug 15, 2023
1 parent 4dc633d commit 02b2592
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/utils/injectRefreshLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,33 @@ const refreshUtilsPath = path.join(__dirname, '../runtime/RefreshUtils');
function injectRefreshLoader(moduleData, injectOptions) {
const { match, options } = injectOptions;

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

// As we inject runtime code for each module,
// it is important to run the injected loader after everything.
// This way we can ensure that all code-processing have been done,
// and we won't risk breaking tools like Flow or ESLint.
moduleData.loaders.unshift({
loader: resolvedLoader,
options,
});

return moduleData;
}

Expand Down

0 comments on commit 02b2592

Please sign in to comment.