forked from v-anton/nextjs-mf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-runtime.js
52 lines (47 loc) · 1.51 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
const ConcatSource = require("webpack-sources/lib/ConcatSource");
const Compilation = require("webpack/lib/Compilation");
const PLUGIN_NAME = "EnableSingleRunTimeForFederationPlugin";
/**
* Merge the webpack runtime with your remote entry.
*/
module.exports = class EnableSingleRunTimeForFederationPlugin {
/**
* @param {object} options
* @param {string} [options.filename] The file name to concat the runtime with
*/
constructor(options) {
this._options = options;
}
// Define `apply` as its prototype method which is supplied with compiler as its argument
apply(compiler) {
const { filename } = this._options;
if(!filename){
throw new Error(`${PLUGIN_NAME}: options.filename is required.`);
}
// Specify the event hook to attach to
compiler.hooks.thisCompilation.tap(
PLUGIN_NAME,
(compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets) => {
Object.keys(assets).forEach((asset) => {
if (asset.includes("static/chunks/webpack")) {
compilation.updateAsset(
filename,
new ConcatSource(
compilation.getAsset(asset).source.buffer().toString(),
compilation.getAsset(filename).source.buffer().toString()
)
);
}
});
}
);
}
);
}
};