Skip to content

Commit 7cc389f

Browse files
committed
feat(mf): add partial application to withFederation for DX
1 parent 23ffe08 commit 7cc389f

File tree

8 files changed

+77
-60
lines changed

8 files changed

+77
-60
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export function toRuntimeConfig(
5555
): UserOptions {
5656
return {
5757
//
58-
// The runtime assumes an empty string as the name for
59-
// the host. Alternatively, we have to pass the same
58+
// The runtime assumes an empty string as the name for
59+
// the host. Alternatively, we have to pass the same
6060
// name to withFederation (compile time config) and
6161
// initFederation (runtime time config on app start)
6262
//

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ export type FederationConfig = {
99
skip?: SkipList;
1010
};
1111

12-
export type FederationOptions = Omit<ModuleFederationConfig['options'], 'name'> & {
12+
export type FederationOptions = Omit<
13+
ModuleFederationConfig['options'],
14+
'name'
15+
> & {
1316
name?: string;
17+
};
18+
19+
export function withFederation(config: FederationConfig) {
20+
//
21+
// This provides partial application for better DX,
22+
// as it allows to split the config into a file with
23+
// rsbuild and an other one with federation settings
24+
//
25+
return (rsbuildConfig: RsbuildConfig) => {
26+
return applyFederation(rsbuildConfig, config);
27+
};
1428
}
1529

16-
export function withFederation(
30+
export function applyFederation(
1731
rsbuildConfig: RsbuildConfig,
1832
federationConfig: FederationConfig
1933
): RsbuildConfig {
@@ -83,7 +97,7 @@ export function withFederation(
8397
//
8498
// Shells use an empty name by default
8599
// Alternative: Specifiying the *same* name
86-
// in initFederation (runtime) and
100+
// in initFederation (runtime) and
87101
// withFederation (build time)
88102
//
89103
name: '',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { withFederation, shareAll } from '@angular-architects/module-federation/rspack'
2+
3+
export default withFederation({
4+
options: {
5+
<% if (type === 'remote') { %>
6+
name: '<%=project%>',
7+
8+
exposes: {
9+
'./Component': './src/app/app.component.ts',
10+
},
11+
<% } else if (type === 'host') { %>
12+
remotes: {<% for (key in remoteMap) { %>
13+
"<%=key%>": "<%=remoteMap[key]%>",<% } %>
14+
},
15+
<% } %>
16+
shared: {
17+
...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
18+
},
19+
20+
},
21+
skip: [
22+
// Add the names of packages, entrypoints
23+
// and libs you don't want to share here
24+
// Strings are compared with ===
25+
26+
// Examples:
27+
// 'rxjs/ajax'
28+
// p => p.startsWith('rxjs/ajax')
29+
// /^rxjs\/ajax/
30+
]
31+
});
Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createConfig } from '@ng-rsbuild/plugin-angular';
2-
import { withFederation, shareAll } from '@angular-architects/module-federation/rspack'
2+
import applyFederation from './federation.config';
33

44
const rsbuildConfig = createConfig(
55
{
@@ -12,32 +12,4 @@ const rsbuildConfig = createConfig(
1212
}
1313
);
1414

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-
});
15+
export default applyFederation(rsbuildConfig);

libs/mf/src/schematics/init-rspack/schematic.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ function updatePackageJson(
278278
tree.overwrite('package.json', JSON.stringify(packageJson, null, 2));
279279
}
280280

281-
function printScriptInfo(projectRoot: string, startScriptName: string, buildScriptName: string) {
281+
function printScriptInfo(
282+
projectRoot: string,
283+
startScriptName: string,
284+
buildScriptName: string
285+
) {
282286
console.info();
283287
console.info(
284288
`[INFO] Please remember that the rspack integration is in early stages`

libs/mf/src/schematics/init/schema.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export interface InitSchema {
33
port: string | number;
44
nxBuilders: boolean | undefined;
55
type: 'host' | 'dynamic-host' | 'remote' | 'legacy';
6-
stack: 'webpack' | 'rsbuild' | 'native'
6+
stack: 'webpack' | 'rsbuild' | 'native';
77
}

libs/mf/src/schematics/init/schematic.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
chain,
3-
Rule,
4-
schematic,
5-
noop,
6-
} from '@angular-devkit/schematics';
1+
import { chain, Rule, schematic, noop } from '@angular-devkit/schematics';
72

83
import {
94
NodePackageInstallTask,

libs/mf/src/schematics/patch/schematic.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
import { noop, Rule } from "@angular-devkit/schematics";
2-
import { execSync } from "child_process";
1+
import { noop, Rule } from '@angular-devkit/schematics';
2+
import { execSync } from 'child_process';
33

4-
//
5-
// Schematic for patching Angular for rsbuild integration while experimental.
4+
//
5+
// Schematic for patching Angular for rsbuild integration while experimental.
66
// Will be removed when stable!
77
// Called via the init-rspack schematic to execute it AFTER the task running
88
// npm install
9-
//
9+
//
1010

1111
export default function init(options: { workspaceRoot: string }): Rule {
1212
return async function () {
13-
14-
const cmd = 'node node_modules/@ng-rsbuild/plugin-angular/patch/patch-angular-build.js';
13+
const cmd =
14+
'node node_modules/@ng-rsbuild/plugin-angular/patch/patch-angular-build.js';
1515

1616
try {
17-
execSync(cmd, {
18-
cwd: options.workspaceRoot
19-
});
20-
}
21-
catch(e) {
22-
console.error('Error patching Angular for rspack');
23-
console.error('This is only needed while the rspack integration is experimental');
24-
console.error('Try to run this command by hand:')
25-
console.error('\t' + cmd);
26-
console.error('Error', e);
17+
execSync(cmd, {
18+
cwd: options.workspaceRoot,
19+
});
20+
} catch (e) {
21+
console.error('Error patching Angular for rspack');
22+
console.error(
23+
'This is only needed while the rspack integration is experimental'
24+
);
25+
console.error('Try to run this command by hand:');
26+
console.error('\t' + cmd);
27+
console.error('Error', e);
2728
}
2829

2930
return noop();

0 commit comments

Comments
 (0)