forked from webpack-contrib/mini-css-extract-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
80 lines (63 loc) · 1.88 KB
/
utils.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import NativeModule from 'module';
const MODULE_TYPE = 'css/mini-extract';
function findModuleById(compilation, id) {
const { modules, chunkGraph } = compilation;
for (const module of modules) {
const moduleId =
typeof chunkGraph !== 'undefined'
? chunkGraph.getModuleId(module)
: module.id;
if (moduleId === id) {
return module;
}
}
return null;
}
function evalModuleCode(loaderContext, code, filename) {
const module = new NativeModule(filename, loaderContext);
module.paths = NativeModule._nodeModulePaths(loaderContext.context); // eslint-disable-line no-underscore-dangle
module.filename = filename;
module._compile(code, filename); // eslint-disable-line no-underscore-dangle
return module.exports;
}
function compareIds(a, b) {
if (typeof a !== typeof b) {
return typeof a < typeof b ? -1 : 1;
}
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
function compareModulesByIdentifier(a, b) {
return compareIds(a.identifier(), b.identifier());
}
function provideLoaderContext(compiler, name, handler, thisCompilation = true) {
const NormalModule =
compiler.webpack && compiler.webpack.NormalModule
? compiler.webpack.NormalModule
: // eslint-disable-next-line global-require
require('webpack/lib/NormalModule');
compiler.hooks[thisCompilation ? 'thisCompilation' : 'compilation'].tap(
name,
(compilation) => {
const normalModuleHook =
typeof NormalModule.getCompilationHooks !== 'undefined'
? NormalModule.getCompilationHooks(compilation).loader
: compilation.hooks.normalModuleLoader;
normalModuleHook.tap(name, (loaderContext, module) =>
handler(loaderContext, module, compilation)
);
}
);
}
export {
provideLoaderContext,
MODULE_TYPE,
findModuleById,
evalModuleCode,
compareModulesByIdentifier,
};