Skip to content

Commit 2c62c19

Browse files
author
Zissis Tabouras
committed
fix(nf): support adding native federation in ESM application
1 parent 434a94e commit 2c62c19

File tree

6 files changed

+84
-4
lines changed

6 files changed

+84
-4
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
{
22
"$schema": "../../node_modules/@angular-devkit/schematics/collection-schema.json",
33
"name": "native-federation",
4-
"version": "0.0.1",
4+
"version": "0.0.2",
55
"schematics": {
66
"update18": {
77
"version": "18",
88
"factory": "./src/schematics/update18/schematic",
99
"schema": "./src/schematics/update18/schema.json",
1010
"description": "migrating to v18"
11+
},
12+
"updateConfigExtension": {
13+
"version": "18.1",
14+
"factory": "./src/schematics/updateConfigExtension/schematic",
15+
"schema": "./src/schematics/updateConfigExtension/schema.json",
16+
"description": "renaming config file to .cjs"
1117
}
1218
}
1319
}

libs/native-federation/src/builders/build/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ function removeBaseHref(req: any, baseHref?: string) {
311311

312312
function infereConfigPath(tsConfig: string): string {
313313
const relProjectPath = path.dirname(tsConfig);
314-
const relConfigPath = path.join(relProjectPath, 'federation.config.js');
314+
const relConfigPath = path.join(relProjectPath, 'federation.config.cjs');
315315

316316
return relConfigPath;
317317
}

libs/native-federation/src/schematics/init/schematic.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '@schematics/angular/utility/dependencies';
2626

2727
import * as path from 'path';
28+
import * as fs from 'fs';
2829

2930
type NormalizedOptions = {
3031
polyfills: string;
@@ -38,6 +39,10 @@ type NormalizedOptions = {
3839
port: number;
3940
};
4041

42+
const CONFIG_FILE_NAME_PREFIX = 'federation.config';
43+
const CONFIG_FILE_NAME = `${CONFIG_FILE_NAME_PREFIX}.cjs`;
44+
const CONFIG_FILE_NAME_JS = `${CONFIG_FILE_NAME_PREFIX}.js`;
45+
4146
export function updatePackageJson(tree: Tree): void {
4247
const packageJson = tree.readJson('package.json');
4348

@@ -91,9 +96,28 @@ export default function config(options: MfSchematicSchema): Rule {
9196
tree.create(manifestPath, JSON.stringify(remoteMap, null, '\t'));
9297
}
9398

94-
const federationConfigPath = path.join(projectRoot, 'federation.config.js');
99+
const federationConfigPath = path.join(projectRoot, CONFIG_FILE_NAME);
100+
const jsFederationConfigPath = path.join(projectRoot, CONFIG_FILE_NAME_JS);
95101

96102
const exists = tree.exists(federationConfigPath);
103+
const jsConfigExists = tree.exists(jsFederationConfigPath);
104+
105+
if (jsConfigExists) {
106+
// If old .js config is found check if new .cjs exists
107+
if (!exists) {
108+
// .js config is found and no .cjs is found. Inform user to delete old config file so new one is created from scratch
109+
// or rename old one to .cjs in order to keep same configuration.
110+
throw new Error(
111+
`Outdated configuration file found (federation.config.js), please delete it or rename it to .cjs in case you want to keep existing configuration.`
112+
);
113+
}
114+
// Both .js and .cjs configurations are found, delete .js one
115+
console.log(
116+
'Multiple configuration files found, deleting outdated one (federation.config.js)'
117+
);
118+
//tree.delete(jsFederationConfigPath); // Doesn't seem to work, will use fs
119+
fs.unlinkSync(jsFederationConfigPath);
120+
}
97121

98122
const generateRule = !exists
99123
? await generateFederationConfig(
@@ -144,6 +168,36 @@ export function patchAngularBuild(tree: Tree) {
144168
}
145169
}
146170

171+
export function renameConfigToCjs(options: MfSchematicSchema, tree: Tree) {
172+
const workspaceFileName = getWorkspaceFileName(tree);
173+
const workspace = JSON.parse(tree.read(workspaceFileName).toString('utf8'));
174+
175+
const normalized = normalizeOptions(options, workspace, tree);
176+
177+
const { projectRoot } = normalized;
178+
179+
const federationConfigPath = path.join(projectRoot, CONFIG_FILE_NAME);
180+
const jsFederationConfigPath = path.join(projectRoot, CONFIG_FILE_NAME_JS);
181+
182+
const exists = tree.exists(federationConfigPath);
183+
const jsConfigExists = tree.exists(jsFederationConfigPath);
184+
185+
if (jsConfigExists) {
186+
// If old .js config is found check if new .cjs exists
187+
if (!exists) {
188+
// .js config is found and no .cjs is found. Rename existing .js to .cjs.
189+
tree.rename(jsFederationConfigPath, federationConfigPath);
190+
return;
191+
}
192+
// Both .js and .cjs configurations are found, delete .js one
193+
console.log(
194+
'Multiple configuration files found, deleting outdated one (federation.config.js)'
195+
);
196+
//tree.delete(jsFederationConfigPath); // Doesn't seem to work, will use fs
197+
fs.unlinkSync(jsFederationConfigPath);
198+
}
199+
}
200+
147201
function updateWorkspaceConfig(
148202
tree: Tree,
149203
options: NormalizedOptions,
@@ -298,7 +352,9 @@ function normalizeOptions(
298352

299353
const main =
300354
projectConfig.architect.build.options.main ||
301-
projectConfig.architect.build.options.browser;
355+
projectConfig.architect.build.options.browser ||
356+
projectConfig.architect.esbuild?.options?.main ||
357+
projectConfig.architect.esbuild?.options?.browser;
302358

303359
if (!projectConfig.architect.build.options.polyfills) {
304360
projectConfig.architect.build.options.polyfills = [];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"$id": "mf",
4+
"title": "",
5+
"type": "object",
6+
"properties": {}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Rule, Tree } from '@angular-devkit/schematics';
2+
import { MfSchematicSchema } from '../init/schema';
3+
import { renameConfigToCjs } from '../init/schematic';
4+
5+
export default function updateConfigExtension(
6+
options: MfSchematicSchema
7+
): Rule {
8+
return async function (tree: Tree) {
9+
renameConfigToCjs(options, tree);
10+
};
11+
}

0 commit comments

Comments
 (0)