-
Notifications
You must be signed in to change notification settings - Fork 1
/
craco-module-federation.js
70 lines (58 loc) · 2.23 KB
/
craco-module-federation.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
// const webpack = require('webpack');
const paths = require('react-scripts/config/paths');
const getModuleFederationConfigPath = (additionalPaths = []) => {
const path = require('node:path');
const fs = require('node:fs');
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const moduleFederationConfigFiles = [
'modulefederation.config.js',
...additionalPaths,
];
return moduleFederationConfigFiles
.map(resolveApp)
.filter(fs.existsSync)
.shift();
};
module.exports = {
overrideWebpackConfig: ({webpackConfig, pluginOptions}) => {
const moduleFederationConfigPath = getModuleFederationConfigPath();
if (moduleFederationConfigPath) {
webpackConfig.output.publicPath = 'auto';
if (pluginOptions?.useNamedChunkIds) {
webpackConfig.optimization.chunkIds = 'named';
}
const htmlWebpackPlugin = webpackConfig.plugins.find(
plugin => plugin.constructor.name === 'HtmlWebpackPlugin'
);
const myModule = require(moduleFederationConfigPath);
htmlWebpackPlugin.userOptions = {
...htmlWebpackPlugin.userOptions,
publicPath: paths.publicUrlOrPath,
excludeChunks: [myModule.name],
};
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
webpackConfig.plugins = [
...webpackConfig.plugins,
new ModuleFederationPlugin(myModule),
];
// webpackConfig.module = {
// ...webpackConfig.module,
// generator: {
// "asset/resource": {
// publicPath: paths.publicUrlOrPath,
// },
// },
// };
}
return webpackConfig;
},
overrideDevServerConfig: ({devServerConfig}) => {
devServerConfig.headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
};
return devServerConfig;
},
};