-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
merge-runtime.js
54 lines (52 loc) · 1.53 KB
/
merge-runtime.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
const fs = require("fs");
const path = require("path");
module.exports = class MergeRemoteChunksPlugin {
constructor(options) {
this._options = Object.assign(
{},
{
filename: "remoteEntry",
},
options
);
}
// Define `apply` as its prototype method which is supplied with compiler as its argument
apply(compiler) {
if (!this._options) return null;
const options = this._options;
// Specify the event hook to attach to
compiler.hooks.afterEmit.tap("MergeRemoteChunksPlugin", (output) => {
const affectedAssets = Array.from(output.emittedAssets);
const assetsWeNeed = [
"static/chunks/webpack",
"static/chunks/commons",
"static/runtime/remoteEntry",
];
if (
assetsWeNeed.some((neededAsset) =>
affectedAssets.some((affectedAsset) =>
affectedAsset.includes(neededAsset)
)
)
) {
const allAssets = Object.keys(output.assets);
const filesToMerge = assetsWeNeed
.map((assetWeNeed) =>
allAssets.find((asset) => asset.includes(assetWeNeed))
)
.filter(Boolean)
.map((file) => path.join(compiler.options.output.path, file));
fs.writeFile(
path.join(
compiler.options.output.path,
"static/runtime/remoteEntryMerged.js"
),
filesToMerge
.map((filePath) => fs.readFileSync(filePath, "utf-8"))
.join("\n"),
() => {}
);
}
});
}
};