-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
NodeFederationPlugin.ts
183 lines (160 loc) · 6.06 KB
/
NodeFederationPlugin.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
import type { Compiler, container } from 'webpack';
import type { ModuleFederationPluginOptions } from '../types';
import {extractUrlAndGlobal} from "@module-federation/utilities";
interface NodeFederationOptions extends ModuleFederationPluginOptions {
experiments?: Record<string, unknown>;
verbose?: boolean;
}
interface Context {
ModuleFederationPlugin?: typeof container.ModuleFederationPlugin;
}
// possible remote evaluators
// this depends on the chunk format selected.
// commonjs2 - it think, is lazily evaluated - beware
// const remote = eval(scriptContent + '\n try{' + moduleName + '}catch(e) { null; };');
// commonjs - fine to use but exports marker doesnt exist
// const remote = eval('let exports = {};' + scriptContent + 'exports');
// commonjs-module, ideal since it returns a commonjs module format
// const remote = eval(scriptContent + 'module.exports')
export const parseRemotes = (remotes: Record<string, any>) => Object.entries(remotes).reduce((acc, remote) => {
if (!remote[1].startsWith('promise ') && remote[1].includes('@')) {
acc[remote[0]] = `promise ${parseRemoteSyntax(remote[1])}`;
return acc;
}
acc[remote[0]] = remote[1];
return acc;
}, {} as Record<string, string>);
// server template to convert remote into promise new promise and use require.loadChunk to load the chunk
export const generateRemoteTemplate = (url: string, global: any) => `new Promise(function (resolve, reject) {
if(!global.__remote_scope__) {
// create a global scope for container, similar to how remotes are set on window in the browser
global.__remote_scope__ = {
_config: {},
}
}
if (typeof global.__remote_scope__[${JSON.stringify(global)}] !== 'undefined') return resolve(global.__remote_scope__[${JSON.stringify(global)}]);
global.__remote_scope__._config[${JSON.stringify(global)}] = ${JSON.stringify(url)};
var __webpack_error__ = new Error();
__webpack_require__.l(
${JSON.stringify(url)},
function (event) {
if (typeof global.__remote_scope__[${JSON.stringify(global)}] !== 'undefined') return resolve(global.__remote_scope__[${JSON.stringify(global)}]);
var realSrc = event && event.target && event.target.src;
__webpack_error__.message = 'Loading script failed.\\n(' + event.message + ': ' + realSrc + ')';
__webpack_error__.name = 'ScriptExternalLoadError';
__webpack_error__.stack = event.stack;
reject(__webpack_error__);
},
${JSON.stringify(global)},
);
}).catch((e)=> {
console.error(${JSON.stringify(global)}, 'is offline, returning fake remote');
console.error(e);
return {
fake: true,
get: (arg) => {
console.log('faking', arg, 'module on', ${JSON.stringify(global)});
return Promise.resolve(() => {
return () => null
});
},
init: () => {
}
}
}).then(function (remote) {
if(remote.fake) {
return remote;
}
const proxy = {
get: (arg)=>{
// if(!global.__remote_scope__[${JSON.stringify(global)}].__initialized) {
// try {
// global.__remote_scope__[${JSON.stringify(global)}].__initialized = true;
// proxy.init(__webpack_require__.S.default);
// } catch(e) {}
// }
return remote.get(arg).then((f)=>{
const m = f();
return ()=>new Proxy(m, {
get: (target, prop)=>{
if(global.usedChunks) global.usedChunks.add(${JSON.stringify(global)} + "->" + arg);
return target[prop];
}
})
})
},
init: function(shareScope) {
const handler = {
get(target, prop) {
if (target[prop]) {
Object.values(target[prop]).forEach(function(o) {
if(o.from === '_N_E') {
o.loaded = 1
}
})
}
return target[prop]
},
set(target, property, value) {
if(global.usedChunks) global.usedChunks.add(${JSON.stringify(global)} + "->" + property);
if (target[property]) {
return target[property]
}
target[property] = value
return true
}
}
try {
global.__remote_scope__[${JSON.stringify(global)}].init(new Proxy(shareScope, handler))
} catch (e) {
}
global.__remote_scope__[${JSON.stringify(global)}].__initialized = true
}
}
if (!global.__remote_scope__[${JSON.stringify(global)}].__initialized) {
proxy.init(__webpack_require__.S.default)
}
return proxy
})`;
export const parseRemoteSyntax = (remote: any) => {
if (typeof remote === 'string' && remote.includes('@')) {
const [url, global] = extractUrlAndGlobal(remote);
return generateRemoteTemplate(url, global);
}
return remote;
};
class NodeFederationPlugin {
private _options: ModuleFederationPluginOptions;
private context: Context;
private experiments: NodeFederationOptions['experiments'];
constructor(
{ experiments, verbose, ...options }: NodeFederationOptions,
context: Context
) {
this._options = options || ({} as ModuleFederationPluginOptions);
this.context = context || ({} as Context);
this.experiments = experiments || {};
}
apply(compiler: Compiler) {
// When used with Next.js, context is needed to use Next.js webpack
const { webpack } = compiler;
// const defs = {
// 'process.env.REMOTES': runtime,
// 'process.env.REMOTE_CONFIG': hot,
// };
// new ((webpack && webpack.DefinePlugin) || require("webpack").DefinePlugin)(
// defs
// ).apply(compiler);
const pluginOptions = {
...this._options,
remotes: parseRemotes(this._options.remotes || {}) as ModuleFederationPluginOptions['remotes'],
};
new (this.context.ModuleFederationPlugin ||
(webpack && webpack.container.ModuleFederationPlugin) ||
require('webpack/lib/container/ModuleFederationPlugin'))(
pluginOptions
).apply(compiler);
}
}
export default NodeFederationPlugin;