injectRefreshLoader.js
1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const path = require('path');
/**
* @callback MatchObject
* @param {string} [str]
* @returns {boolean}
*/
const resolvedLoader = require.resolve('../../loader');
/**
* Injects refresh loader to all JavaScript-like and user-specified files.
* @param {*} data Module factory creation data.
* @param {MatchObject} matchObject A function to include/exclude files to be processed.
* @returns {*} The injected module factory creation data.
*/
function injectRefreshLoader(data, matchObject) {
if (
// Include and exclude user-specified files
matchObject(data.resource) &&
// Skip plugin's runtime utils to prevent self-referencing -
// this is useful when using the plugin as a direct dependency.
!data.resource.includes(path.join(__dirname, '../runtime/RefreshUtils')) &&
// Check to prevent double injection
!data.loaders.find(({ loader }) => loader === resolvedLoader)
) {
// 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.
data.loaders.unshift({
loader: resolvedLoader,
options: undefined,
});
}
return data;
}
module.exports = injectRefreshLoader;