-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveStyleScriptChunksPlugin.js
53 lines (45 loc) · 1.4 KB
/
RemoveStyleScriptChunksPlugin.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
require("./Array");
const pluginName = "RemoveStyleScriptChunksPlugin";
module.exports = class RemoveStyleScriptChunksPlugin {
hasCSSChunks(results) {
return results.some(r => {
return r.identifier.includes("mini-css-extract-plugin");
});
}
isJavascriptChunk(result) {
return result.pathOptions.contentHashType === "javascript";
}
removeExtraChunksInPlace(results) {
if (!this.hasCSSChunks(results)) {
return;
}
results.rejectInPlace(this.isJavascriptChunk);
}
listenForAssets(template) {
template.hooks.renderManifest.tap(
pluginName,
this.removeExtraChunksInPlace.bind(this)
);
}
listenForEntires(template) {
template.hooks.startup.tap(pluginName, (source, chunk, hash) => {
if (chunk.hasEntryModule()) {
for (const chunkGroup of chunk.groupsIterable) {
if (chunkGroup.chunks.length > 1) {
console.log(chunkGroup, chunk);
}
}
}
});
}
apply(compiler) {
compiler.hooks.thisCompilation.tap(pluginName, compilation => {
// We can't register the tap until beforeChunkAssets
// If we register it before then it won't be last
compilation.hooks.beforeChunkAssets.tap(pluginName, () => {
[compilation.mainTemplate].forEach(this.listenForEntires.bind(this));
[compilation.chunkTemplate].forEach(this.listenForAssets.bind(this));
});
});
}
};