-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmodern.config.js
More file actions
77 lines (72 loc) · 2.4 KB
/
modern.config.js
File metadata and controls
77 lines (72 loc) · 2.4 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
import appTools, { defineConfig } from '@modern-js/app-tools';
import { ModuleFederationPlugin } from '@module-federation/enhanced';
import { RuntimeGlobals } from 'webpack';
const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin');
class AsyncEntryStartupPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap('AsyncEntryStartupPlugin', compilation => {
JavascriptModulesPlugin.getCompilationHooks(compilation).renderStartup.tap(
'AsyncEntryStartupPlugin',
(source, renderContext, upperContext) => {
if (upperContext.chunk.hasRuntime()) {
return source;
}
const startup = [
'var promiseTrack = [];',
`if(__webpack_require__.f && __webpack_require__.f.remotes) __webpack_require__.f.remotes(${JSON.stringify(
upperContext.chunk.id,
)}, promiseTrack);`,
`if(__webpack_require__.f && __webpack_require__.f.consumes) __webpack_require__.f.consumes(${JSON.stringify(
upperContext.chunk.id,
)}, promiseTrack);`,
`var __webpack_exports__ = Promise.all(promiseTrack).then(function() {`,
source.source(),
'return __webpack_exports__;',
`});`,
].join('\n');
return startup;
},
);
});
}
}
// https://modernjs.dev/en/configure/app/usage
export default defineConfig({
server: {
port: 3002,
},
dev: {
// set publicPath
// assetPrefix: 'auto',
},
runtime: {
router: true,
},
source: {
// automatically generated asynchronous boundary via Dynamic Import, allowing the page code to consume remote modules generated by the module federation.
enableAsyncEntry: false,
},
tools: {
webpack: (config, { webpack, appendPlugins }) => {
delete config.optimization.splitChunks;
config.output.publicPath = 'auto';
appendPlugins([
new AsyncEntryStartupPlugin(),
new ModuleFederationPlugin({
name: 'app2',
library: { type: 'window', name: 'app2' },
runtime: false,
filename: 'static/js/remoteEntry.js',
exposes: {
'./Button': './src/components/button.js',
},
shared: {
react: { singleton: true },
'react-dom': { singleton: true },
},
}),
]);
},
},
plugins: [appTools()],
});