-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathwebpack.config.js
More file actions
171 lines (158 loc) · 4.74 KB
/
webpack.config.js
File metadata and controls
171 lines (158 loc) · 4.74 KB
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
// const { registerPluginTSTranspiler } = require('nx/src/utils/nx-plugin.js');
// registerPluginTSTranspiler();
const path = require('path');
const reactPath = path.dirname(require.resolve('react/package.json'));
const reactDomPath = path.dirname(require.resolve('react-dom/package.json'));
const { composePlugins, withNx } = require('@nx/webpack');
const { withReact } = require('@nx/react');
const {
ModuleFederationPlugin,
} = require('@module-federation/enhanced/webpack');
const WORKSPACE_DIST_PATTERN = /[\\/]packages[\\/][^\\/]+[\\/]dist[\\/]/;
function hasBabelLoader(rule) {
const uses = Array.isArray(rule?.use)
? rule.use
: rule?.use
? [rule.use]
: [];
return uses.some(
(useEntry) =>
useEntry &&
typeof useEntry === 'object' &&
typeof useEntry.loader === 'string' &&
useEntry.loader.includes('babel-loader'),
);
}
function appendExclude(existingExclude, pattern) {
if (!existingExclude) {
return pattern;
}
if (Array.isArray(existingExclude)) {
return [...existingExclude, pattern];
}
return [existingExclude, pattern];
}
function excludeWorkspaceDistFromBabel(rules = []) {
for (const rule of rules) {
if (Array.isArray(rule.oneOf)) {
excludeWorkspaceDistFromBabel(rule.oneOf);
}
if (Array.isArray(rule.rules)) {
excludeWorkspaceDistFromBabel(rule.rules);
}
if (hasBabelLoader(rule)) {
rule.exclude = appendExclude(rule.exclude, WORKSPACE_DIST_PATTERN);
}
}
}
function extendReactRefreshExcludes(plugins = []) {
for (const plugin of plugins) {
if (plugin?.constructor?.name !== 'ReactRefreshPlugin') {
continue;
}
const existingExclude = plugin.options?.exclude;
if (!existingExclude) {
plugin.options.exclude = WORKSPACE_DIST_PATTERN;
continue;
}
if (Array.isArray(existingExclude)) {
plugin.options.exclude = [...existingExclude, WORKSPACE_DIST_PATTERN];
continue;
}
plugin.options.exclude = [existingExclude, WORKSPACE_DIST_PATTERN];
}
}
module.exports = composePlugins(
withNx(),
withReact(),
async (config, context) => {
excludeWorkspaceDistFromBabel(config.module?.rules || []);
extendReactRefreshExcludes(config.plugins || []);
config.watchOptions = config.watchOptions || {};
config.watchOptions.ignored = config.watchOptions.ignored || [];
// Ensure ignored is an array
if (!Array.isArray(config.watchOptions.ignored)) {
config.watchOptions.ignored = [config.watchOptions.ignored];
}
// Add our patterns
['**/node_modules/**', '**/@mf-types/**', '**/dist/**'].forEach(
(pattern) => {
if (!config.watchOptions.ignored.includes(pattern)) {
config.watchOptions.ignored.push(pattern);
}
},
);
config.devServer = {
...config.devServer,
devMiddleware: {
writeToDisk: true,
},
};
// publicPath must be specific url
config.output.publicPath = 'auto';
config.plugins.push(
new ModuleFederationPlugin({
name: 'webpack_provider',
filename: 'remoteEntry.js',
exposes: {
'./useCustomRemoteHook': './src/components/useCustomRemoteHook',
'./WebpackSvg': './src/components/WebpackSvg',
'./WebpackPng': './src/components/WebpackPng',
},
shared: {
lodash: {},
antd: {},
'react/': {
singleton: true,
requiredVersion: '^18.3.1',
},
react: {
singleton: true,
requiredVersion: '^18.3.1',
},
'react-dom': {
singleton: true,
requiredVersion: '^18.3.1',
},
'react-dom/': {
singleton: true,
requiredVersion: '^18.3.1',
},
},
experiments: {
externalRuntime: true,
},
}),
);
config.plugins.push({
name: 'nx-dev-webpack-plugin',
apply(compiler) {
compiler.options.devtool = false;
compiler.options.resolve.alias = {
...compiler.options.resolve.alias,
react: reactPath,
'react-dom': reactDomPath,
};
},
});
config.optimization.runtimeChunk = false;
config.plugins.forEach((p) => {
if (p.constructor.name === 'ModuleFederationPlugin') {
//Temporary workaround - https://github.com/nrwl/nx/issues/16983
p._options.library = undefined;
}
});
//Temporary workaround - https://github.com/nrwl/nx/issues/16983
config.experiments = { outputModule: false };
config.output = {
...config.output,
scriptType: 'text/javascript',
};
config.optimization = {
...config.optimization,
runtimeChunk: false,
splitChunks: false,
};
return config;
},
);