Skip to content

Commit 623837c

Browse files
committed
feat(mf): add support for eager and pinned
1 parent 64ec2dc commit 623837c

File tree

2 files changed

+112
-30
lines changed

2 files changed

+112
-30
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export class ModifyEntryPlugin {
2+
config: unknown;
3+
constructor(config) {
4+
this.config = config;
5+
}
6+
7+
apply(compiler) {
8+
const mergeEntry = (keyFn, key) => [
9+
...(keyFn(this.config[key]) || []),
10+
...(keyFn(compiler.options.entry[key]) || []),
11+
];
12+
const cfgOrRemove = (objFn, valueFn, key) => {
13+
const values = mergeEntry(valueFn, key);
14+
return values.length > 0 ? objFn(values) : {};
15+
};
16+
Object.keys(this.config).forEach((key) => {
17+
compiler.options.entry[key] = {
18+
...cfgOrRemove(
19+
(v) => ({ import: v }),
20+
(c) => c.import,
21+
key
22+
),
23+
...cfgOrRemove(
24+
(v) => ({ dependOn: v }),
25+
(c) => c.dependOn,
26+
key
27+
),
28+
};
29+
});
30+
}
31+
}
32+

libs/mf/src/utils/with-mf-plugin.ts

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { findRootTsConfigJson, shareAll } from './share-utils';
22
import { SharedMappings } from './shared-mappings';
3+
import { ModifyEntryPlugin } from './modify-entry-plugin';
34

45
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
56

@@ -11,37 +12,13 @@ export function withModuleFederationPlugin(config: unknown) {
1112
const mappings = new SharedMappings();
1213
mappings.register(findRootTsConfigJson(), sharedMappings);
1314

14-
if (!config['library']) {
15-
config['library'] = {
16-
type: 'module'
17-
}
18-
}
19-
20-
if (!config['filename']) {
21-
config['filename'] = 'remoteEntry.js';
22-
}
23-
24-
if (!config['shared']) {
25-
config['shared'] = shareAll(
26-
{ singleton: true, strictVersion: true, requiredVersion: 'auto'});
27-
}
28-
29-
if (typeof config['shared'] === 'object') {
30-
config['shared'] = {
31-
...config['shared'],
32-
...mappings.getDescriptors()
33-
}
34-
}
35-
36-
if (Array.isArray(config['shared'])) {
37-
config['shared'] = [
38-
...config['shared'],
39-
mappings.getDescriptors()
40-
]
41-
}
15+
setDefaults(config, mappings);
16+
const modifyEntryPlugin = createModifyEntryPlugin(config);
4217

4318
const isModule = config['library']?.['type'] === 'module';
4419

20+
// console.log('sharedConfig.modFed', sharedConfig.modFed);
21+
4522
return {
4623
output: {
4724
publicPath: "auto"
@@ -61,7 +38,80 @@ export function withModuleFederationPlugin(config: unknown) {
6138
} : {},
6239
plugins: [
6340
new ModuleFederationPlugin(config),
64-
mappings.getPlugin()
41+
mappings.getPlugin(),
42+
...(modifyEntryPlugin) ? [modifyEntryPlugin] : []
6543
],
6644
};
67-
}
45+
}
46+
47+
function setDefaults(config: unknown, mappings: SharedMappings) {
48+
if (!config['library']) {
49+
config['library'] = {
50+
type: 'module'
51+
};
52+
}
53+
54+
if (!config['filename']) {
55+
config['filename'] = 'remoteEntry.js';
56+
}
57+
58+
if (!config['shared']) {
59+
config['shared'] = shareAll(
60+
{ singleton: true, strictVersion: true, requiredVersion: 'auto' });
61+
}
62+
63+
if (typeof config['shared'] === 'object') {
64+
config['shared'] = {
65+
...config['shared'],
66+
...mappings.getDescriptors()
67+
};
68+
}
69+
70+
if (Array.isArray(config['shared'])) {
71+
config['shared'] = [
72+
...config['shared'],
73+
mappings.getDescriptors()
74+
];
75+
}
76+
}
77+
78+
function createModifyEntryPlugin(config: unknown) {
79+
const pinned = [];
80+
const eager = [];
81+
for (const key in config['shared']) {
82+
const entry = config['shared'][key];
83+
if (entry.pinned) {
84+
pinned.push(key);
85+
delete entry.pinned;
86+
}
87+
if (entry.eager) {
88+
eager.push(key);
89+
}
90+
}
91+
const hasPinned = pinned.length > 0;
92+
const hasEager = eager.length > 0;
93+
94+
if (hasPinned && config['remotes']) {
95+
throw new Error([
96+
'Pinned dependencies in combination with build-time remotes are not allowed. ',
97+
'Either remove "pinned: true" from all shared dependencies or delete all ',
98+
'remotes in your webpack config and use runtime remote loading instead.'
99+
].join(''));
100+
}
101+
102+
let modifyEntryConfig = {};
103+
let modifyEntryPlugin = null;
104+
if (hasPinned) {
105+
modifyEntryConfig['main'] = { import: pinned };
106+
}
107+
108+
if (hasEager) {
109+
modifyEntryConfig['styles'] = { dependOn: ['main'] };
110+
modifyEntryConfig['polyfills'] = { dependOn: ['main'] };
111+
}
112+
113+
if (hasPinned || hasEager) {
114+
modifyEntryPlugin = new ModifyEntryPlugin(modifyEntryConfig);
115+
}
116+
return modifyEntryPlugin;
117+
}

0 commit comments

Comments
 (0)