Skip to content

Commit 2604eb0

Browse files
committed
Work on plugins to remove unnecessary JS chunks
1 parent 451e9c9 commit 2604eb0

File tree

6 files changed

+96
-3
lines changed

6 files changed

+96
-3
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch via NPM",
11+
"runtimeExecutable": "npm",
12+
"runtimeArgs": ["run-script", "debug"],
13+
"port": 9229
14+
}
15+
]
16+
}

build/Array.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Array.prototype.rejectInPlace = function(callback) {
2+
return this.filterInPlace((...args) => {
3+
return !callback(...args);
4+
});
5+
};
6+
7+
Array.prototype.filterInPlace = function(callback) {
8+
let i = 0,
9+
j = 0;
10+
11+
while (i < this.length) {
12+
const val = this[i];
13+
if (callback(val, i, this)) this[j++] = val;
14+
i++;
15+
}
16+
17+
this.length = j;
18+
return this;
19+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require("./Array");
2+
3+
const pluginName = "RemoveStyleScriptChunksPlugin";
4+
5+
module.exports = class RemoveStyleScriptChunksPlugin {
6+
hasCSSChunks(results) {
7+
return results.some(r => {
8+
return r.identifier.includes("mini-css-extract-plugin");
9+
});
10+
}
11+
12+
isJavascriptChunk(result) {
13+
return result.pathOptions.contentHashType === "javascript";
14+
}
15+
16+
removeExtraChunksInPlace(results) {
17+
if (!this.hasCSSChunks(results)) {
18+
return;
19+
}
20+
21+
results.rejectInPlace(this.isJavascriptChunk);
22+
}
23+
24+
listenForAssets(template) {
25+
template.hooks.renderManifest.tap(
26+
pluginName,
27+
this.removeExtraChunksInPlace.bind(this)
28+
);
29+
}
30+
31+
listenForEntires(template) {
32+
template.hooks.startup.tap(pluginName, (source, chunk, hash) => {
33+
if (chunk.hasEntryModule()) {
34+
for (const chunkGroup of chunk.groupsIterable) {
35+
if (chunkGroup.chunks.length > 1) {
36+
console.log(chunkGroup, chunk);
37+
}
38+
}
39+
}
40+
});
41+
}
42+
43+
apply(compiler) {
44+
compiler.hooks.thisCompilation.tap(pluginName, compilation => {
45+
// We can't register the tap until beforeChunkAssets
46+
// If we register it before then it won't be last
47+
compilation.hooks.beforeChunkAssets.tap(pluginName, () => {
48+
[compilation.mainTemplate].forEach(this.listenForEntires.bind(this));
49+
[compilation.chunkTemplate].forEach(this.listenForAssets.bind(this));
50+
});
51+
});
52+
}
53+
};

build/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.RemoveStyleScriptChunksPlugin = require("./RemoveStyleScriptChunksPlugin");

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"main": "index.js",
55
"license": "MIT",
66
"scripts": {
7-
"test": "node ./node_modules/.bin/webpack --progress --display verbose"
7+
"test": "node ./node_modules/.bin/webpack --progress --display verbose",
8+
"debug": "node --inspect-brk ./node_modules/.bin/webpack --progress --display verbose"
89
},
910
"dependencies": {
1011
"css-loader": "^0.28.11",

webpack.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require("path");
22
const webpack = require("webpack");
33
const { VueLoaderPlugin } = require("vue-loader");
4+
const { RemoveStyleScriptChunksPlugin } = require("./build");
45

56
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
67
module.exports = {
@@ -54,6 +55,8 @@ module.exports = {
5455
filename: "[name].css",
5556
chunkFilename: "[name].css"
5657
}),
58+
59+
new RemoveStyleScriptChunksPlugin()
5760
],
5861

5962
optimization: {
@@ -69,7 +72,7 @@ module.exports = {
6972
// enforce: false
7073
// results in no vue-styles chunk
7174
// Only a bundle.css file
72-
enforce: false
75+
enforce: true
7376
},
7477

7578
extractOtherStyles: {
@@ -82,7 +85,7 @@ module.exports = {
8285
// Only a bundle.css file
8386
name: "other-styles",
8487
chunks: "all",
85-
enforce: false
88+
enforce: true
8689
}
8790
}
8891
}

0 commit comments

Comments
 (0)