* Injects refresh loader to all JavaScript-like and user-specified files. * @param {*} moduleData Module factory creation data. * @param {InjectLoaderOptions} injectOptions Options to alter how the loader is injected. * @returns {*} The injected module factory creation data.
(moduleData, injectOptions)
| 23 | * @returns {*} The injected module factory creation data. |
| 24 | */ |
| 25 | function injectRefreshLoader(moduleData, injectOptions) { |
| 26 | const { match, options } = injectOptions; |
| 27 | |
| 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. |
| 39 | if ( |
| 40 | moduleData.resource.includes(reactRefreshPath) || |
| 41 | moduleData.resource.includes(refreshUtilsPath) |
| 42 | ) { |
| 43 | return moduleData; |
| 44 | } |
| 45 | |
| 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 | |
| 55 | return moduleData; |
| 56 | } |
| 57 | |
| 58 | module.exports = injectRefreshLoader; |
no outgoing calls
no test coverage detected
searching dependent graphs…