Skip to content

Commit 23ffe08

Browse files
committed
feat(mf): add schematic for rspack
1 parent 1bd6082 commit 23ffe08

File tree

20 files changed

+632
-37
lines changed

20 files changed

+632
-37
lines changed

libs/mf-runtime/enhanced/src/lib/init-federation.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ export async function initFederation(
4949
return init(runtimeConfig);
5050
}
5151

52-
// TODO: Consider making internal
5352
export function toRuntimeConfig(
5453
config: Manifest<RemoteConfig>,
5554
options?: InitFederationOptions
5655
): UserOptions {
5756
return {
58-
name: 'shell',
57+
//
58+
// The runtime assumes an empty string as the name for
59+
// the host. Alternatively, we have to pass the same
60+
// name to withFederation (compile time config) and
61+
// initFederation (runtime time config on app start)
62+
//
63+
name: '',
5964
...options?.runtimeOptions,
6065
remotes: [
6166
...(options?.runtimeOptions?.remotes ?? []),
@@ -94,7 +99,6 @@ export async function loadManifest<T extends Manifest = Manifest>(
9499
return config as T;
95100
}
96101

97-
// TODO: Consider making internal
98102
export function parseConfig(config: ManifestFile): Manifest {
99103
const result: Manifest = {};
100104
for (const key in config) {

libs/mf/collection.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@
44
"version": "0.0.1",
55
"schematics": {
66
"ng-add": {
7-
"factory": "./src/schematics/mf/schematic#add",
8-
"schema": "./src/schematics/mf/schema.json",
9-
"description": "Initialize an angular project for webpack module federation"
7+
"factory": "./src/schematics/init/schematic#init",
8+
"schema": "./src/schematics/init/schema.json",
9+
"description": "Initialize an angular project for module federation"
1010
},
1111
"init": {
1212
"factory": "./src/schematics/init/schematic#init",
1313
"schema": "./src/schematics/init/schema.json",
1414
"description": "Initialize an angular project for module federation"
1515
},
16+
"init-webpack": {
17+
"factory": "./src/schematics/init-webpack/schematic#init",
18+
"schema": "./src/schematics/init-webpack/schema.json",
19+
"description": "Initialize an angular project for webpack module federation"
20+
},
21+
"init-rspack": {
22+
"factory": "./src/schematics/init-rspack/schematic#init",
23+
"schema": "./src/schematics/init-rspack/schema.json",
24+
"description": "Initialize an angular project for rspack module federation"
25+
},
1626
"config": {
1727
"factory": "./src/schematics/mf/schematic",
1828
"schema": "./src/schematics/mf/schema.json",
@@ -32,6 +42,11 @@
3242
"factory": "./src/schematics/remove/schematic",
3343
"schema": "./src/schematics/remove/schema.json",
3444
"description": "Removes Module Federation"
45+
},
46+
"patch": {
47+
"factory": "./src/schematics/patch/schematic",
48+
"description": "Patching Angular for rsbuild integration while experimental. Will be removed when stable!",
49+
"private": true
3550
}
3651
}
3752
}

libs/mf/src/rspack/with-federation.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ import { applySkipList, normalizeSkipList, SkipList } from '../utils/skip-list';
44
import { SharedObject } from '@module-federation/enhanced/dist/src/declarations/plugins/sharing/SharePlugin';
55
import { findRootTsConfigJson, SharedMappings } from '../webpack';
66

7-
export type FederationConfig = ModuleFederationConfig & {
7+
export type FederationConfig = {
8+
options: FederationOptions;
89
skip?: SkipList;
910
};
1011

12+
export type FederationOptions = Omit<ModuleFederationConfig['options'], 'name'> & {
13+
name?: string;
14+
}
15+
1116
export function withFederation(
1217
rsbuildConfig: RsbuildConfig,
1318
federationConfig: FederationConfig
1419
): RsbuildConfig {
1520
const { skip, ...mfConfig } = federationConfig;
1621
const normalizedSkip = normalizeSkipList(skip);
17-
const shared = (mfConfig.options.shared ?? {}) as SharedObject;
18-
1922
const mappings = new SharedMappings();
2023
mappings.register(findRootTsConfigJson());
2124

25+
const shared = (mfConfig.options.shared ?? {}) as SharedObject;
2226
const sharedWithLibs = {
2327
...mappings.getDescriptors(),
2428
...shared,
@@ -76,6 +80,13 @@ export function withFederation(
7680
moduleFederation: {
7781
...mfConfig,
7882
options: {
83+
//
84+
// Shells use an empty name by default
85+
// Alternative: Specifiying the *same* name
86+
// in initFederation (runtime) and
87+
// withFederation (build time)
88+
//
89+
name: '',
7990
...mfConfig.options,
8091
library: {
8192
...mfConfig.options.library,

libs/mf/src/schematics/boot-async/schematic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path = require('path');
22
import { noop } from 'rxjs';
3-
import { getWorkspaceFileName } from '../mf/schematic';
3+
import { getWorkspaceFileName } from '../init-webpack/schematic';
44
import { BootAsyncSchema } from './schema';
55
import { Rule } from '@angular-devkit/schematics';
66

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createConfig } from '@ng-rsbuild/plugin-angular';
2+
import { withFederation, shareAll } from '@angular-architects/module-federation/rspack'
3+
4+
const rsbuildConfig = createConfig(
5+
{
6+
browser: './src/main.ts',
7+
},
8+
{
9+
server: {
10+
port: <%=port%>,
11+
},
12+
}
13+
);
14+
15+
export default withFederation(rsbuildConfig, {
16+
options: {
17+
<% if (type === 'remote') { %>
18+
name: '<%=project%>',
19+
20+
exposes: {
21+
'./Component': './<%=projectSourceRoot%>/app/app.component.ts',
22+
},
23+
<% } else if (type === 'host') { %>
24+
remotes: {<% for (key in remoteMap) { %>
25+
"<%=key%>": "<%=remoteMap[key]%>",<% } %>
26+
},
27+
<% } %>
28+
shared: {
29+
...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
30+
},
31+
32+
},
33+
skip: [
34+
// Add the names of packages, entrypoints
35+
// and libs you don't want to share here
36+
// Strings are compared with ===
37+
38+
// Examples:
39+
// 'rxjs/ajax'
40+
// p => p.startsWith('rxjs/ajax')
41+
// /^rxjs\/ajax/
42+
]
43+
});

0 commit comments

Comments
 (0)