forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
150 lines (144 loc) · 3.58 KB
/
webpack.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
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
const path = require("path");
const { ModuleFederationPlugin } = require("../../").container;
const rules = [
{
test: /\.js$/,
include: path.resolve(__dirname, "src"),
use: {
loader: "babel-loader",
options: {
presets: ["@babel/react"]
}
}
}
];
const optimization = {
chunkIds: "named", // for this example only: readable filenames in production too
nodeEnv: "production" // for this example only: always production version of react
};
const stats = {
chunks: true,
modules: false,
chunkModules: true,
chunkOrigins: true
};
module.exports = (env = "development") => [
// For this example we have 3 configs in a single file
// In practice you probably would have separate config
// maybe even separate repos for each build.
// For Module Federation there is not compile-time dependency
// between the builds.
// Each one can have different config options.
{
name: "app",
mode: env,
entry: {
app: "./src/index.js"
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist/aaa"),
publicPath: "dist/aaa/",
// Each build needs a unique name
// to avoid runtime collisions
// The default uses "name" from package.json
uniqueName: "module-federation-aaa"
},
module: { rules },
optimization,
plugins: [
new ModuleFederationPlugin({
// List of remotes with URLs
remotes: {
"mfe-b": "mfeBBB@/dist/bbb/mfeBBB.js",
"mfe-c": "mfeCCC@/dist/ccc/mfeCCC.js"
},
// list of shared modules with optional options
shared: {
// specifying a module request as shared module
// will provide all used modules matching this name (version from package.json)
// and consume shared modules in the version specified in dependencies from package.json
// (or in dev/peer/optionalDependencies)
// So it use the highest available version of this package matching the version requirement
// from package.json, while providing it's own version to others.
react: {
singleton: true // make sure only a single react module is used
}
}
})
],
stats
},
{
name: "mfe-b",
mode: env,
entry: {},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist/bbb"),
publicPath: "dist/bbb/",
uniqueName: "module-federation-bbb"
},
module: { rules },
optimization,
plugins: [
new ModuleFederationPlugin({
// A unique name
name: "mfeBBB",
// List of exposed modules
exposes: {
"./Component": "./src-b/Component"
},
// list of shared modules
shared: [
// date-fns is shared with the other remote, app doesn't know about that
"date-fns",
{
react: {
singleton: true // must be specified in each config
}
}
]
})
],
stats
},
{
name: "mfe-c",
mode: env,
entry: {},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist/ccc"),
publicPath: "dist/ccc/",
uniqueName: "module-federation-ccc"
},
module: { rules },
optimization,
plugins: [
new ModuleFederationPlugin({
name: "mfeCCC",
exposes: {
"./Component": "./src-c/Component",
"./Component2": "./src-c/LazyComponent"
},
shared: [
// All (used) requests within lodash are shared.
"lodash/",
"date-fns",
{
react: {
// Do not load our own version.
// There must be a valid shared module available at runtime.
// This improves build time as this module doesn't need to be compiled,
// but it opts-out of possible fallbacks and runtime version upgrade.
import: false,
singleton: true
}
}
]
})
],
stats
}
];