-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathwebpack.host-config.js
59 lines (58 loc) · 1.8 KB
/
webpack.host-config.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
55
56
57
58
59
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { container } = require('webpack');
module.exports = {
mode: 'development',
entry: path.resolve(__dirname, 'src/index.js'),
output: {
path: path.resolve(__dirname, 'build'),
filename: 'host-bundle-[name].js',
chunkFilename: 'host-chunk-[name].js',
},
plugins: [
new container.ModuleFederationPlugin({
remotes: {
myModule: `promise new Promise(resolve => {
// This part depends on how you plan on hosting and versioning your federated modules
if (globalThis.SharedWorkerGlobalScope) {
const remoteUrlWithVersion = 'http://localhost:3001/worker/remoteEntry.js'
importScripts(remoteUrlWithVersion)
resolve(globalThis.myModule);
return;
}
const remoteUrlWithVersion = 'http://localhost:3001/remoteEntry.js'
const script = document.createElement('script')
script.src = remoteUrlWithVersion
script.onload = () => {
// the injected script has loaded and is available on window
// we can now resolve this Promise
const proxy = {
get: (request) => window.myModule.get(request),
init: (...arg) => {
try {
return window.myModule.init(...arg)
} catch(e) {
console.log('remote container already initialized')
}
}
}
resolve(proxy)
}
// inject this script with the src set to the versioned remoteEntry.js
document.head.appendChild(script);
})
`,
},
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'index.html'),
}),
],
devServer: {
static: {
directory: path.join(__dirname, 'build'),
},
compress: true,
port: 3000,
}
};