-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.js
48 lines (41 loc) · 1.42 KB
/
Plugin.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
'use strict'
class ExtractFilePlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap('ExtractFilePlugin', (compilation) => {
compilation.hooks.normalModuleLoader.tap('ExtractFilePlugin', (loaderContext, module) => {
loaderContext[__dirname] = () => {
module.buildMeta[__dirname] = true;
};
});
compilation.hooks.additionalAssets.tap('ExtractFilePlugin', () => {
for (const chunk of compilation.chunks) {
for (const module of chunk.modulesIterable) {
processModule(chunk, module);
}
}
});
});
}
}
function processModule(chunk, ourModule) {
if (!ourModule.buildMeta || !ourModule.buildMeta[__dirname]) {
return;
}
// let's find module, which was issued by ours (proxied module)
for (const module of chunk.modulesIterable) {
if (module.reasons.some(reason => reason.module === ourModule)) {
addAssets(chunk, module);
break;
}
}
}
function addAssets(chunk, module) {
if (!module.buildInfo || !module.buildInfo.assets) {
return;
}
// add any emitted assets via proxied module to this chunk
for (const file of Object.keys(module.buildInfo.assets)) {
chunk.files.push(file);
}
}
module.exports = ExtractFilePlugin;