diff --git a/package.json b/package.json index 2de27c62..9f027597 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@angular-devkit/schematics": "~22.2.0", "@chialab/esbuild-plugin-commonjs": "^0.19.0", "@softarc/native-federation": "~4.10.0", - "@softarc/native-federation-orchestrator": "^4.9.0", + "@softarc/native-federation-orchestrator": "^4.10.0", "es-module-shims": "^2.8.0", "esbuild": "^0.28.0", "jsonc-parser": "^3.3.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fdd9794c..085120f2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -28,8 +28,8 @@ importers: specifier: ~4.10.0 version: 4.10.0 '@softarc/native-federation-orchestrator': - specifier: ^4.9.0 - version: 4.9.0(@softarc/native-federation@4.10.0) + specifier: ^4.10.0 + version: 4.10.0(@softarc/native-federation@4.10.0) es-module-shims: specifier: ^2.8.0 version: 2.8.4 @@ -502,8 +502,8 @@ packages: peerDependencies: eslint: ^6.0.0 || ^7.0.0 || >=8.0.0 - '@eslint-community/regexpp@4.12.2': - resolution: {integrity: sha512-EriSTlt5OC9/7SXkRSCAhfSxxoSUgBm33OH+IkwbdpgoqsSsUg7y3uh+IICI/Qg4BBWr3U2i39RpmycbxMq4ew==, tarball: https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.12.2.tgz} + '@eslint-community/regexpp@4.12.3': + resolution: {integrity: sha512-R76UKbvsqI68DJJVTVyqFKqU98CN4+B+acxNfEA2IFarSzP0SjtKf/3+fFiXzTyv5Fj3PxLkGHe2wnVWWMxobQ==, tarball: https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.12.3.tgz} engines: {node: ^12.0.0 || ^14.0.0 || >=16.0.0} '@eslint/config-array@0.23.5': @@ -1563,11 +1563,11 @@ packages: cpu: [x64] os: [win32] - '@softarc/native-federation-orchestrator@4.9.0': - resolution: {integrity: sha512-qPe4SrIgdLC6k3Hpcb7v+rFrtHEpIOYUfIj6OsmaAL8z1hUKd9YSliTRE7Q12spFfDj2x9jG+RPebrtj2ImDjw==, tarball: https://registry.npmjs.org/@softarc/native-federation-orchestrator/-/native-federation-orchestrator-4.9.0.tgz} - engines: {node: '>=24.16.0'} + '@softarc/native-federation-orchestrator@4.10.0': + resolution: {integrity: sha512-XtbJ7/mfq/Qf3dx5vWon2XMBOcGdt63JxiARXVmDiEgENQEKaGmGOnl5Y9HZZUr1teOAm48BYJ/JutPGgQ9U8w==, tarball: https://registry.npmjs.org/@softarc/native-federation-orchestrator/-/native-federation-orchestrator-4.10.0.tgz} + engines: {node: '>=20.6.0'} peerDependencies: - '@softarc/native-federation': ^4.8.0 + '@softarc/native-federation': ^4.10.0 peerDependenciesMeta: '@softarc/native-federation': optional: true @@ -1783,8 +1783,8 @@ packages: resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==, tarball: https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz} engines: {node: '>=12'} - ast-v8-to-istanbul@1.0.7: - resolution: {integrity: sha512-kFL68AG6ajd8fg248zwM9GQrUWEp79gsmjum34OEXjs4yHuUMZfYKwOLW9GMmB4oNvVrj+EAGxsP7ye2UR9UlA==, tarball: https://registry.npmjs.org/ast-v8-to-istanbul/-/ast-v8-to-istanbul-1.0.7.tgz} + ast-v8-to-istanbul@1.0.8: + resolution: {integrity: sha512-lPdpagk13ANXV+HClJEpCZM7kUsl5XcvgqKzZhaj6Ve1ZFICcY2sGhAN1n1vT0tML6EclT7bfVgTxqL57Gkpow==, tarball: https://registry.npmjs.org/ast-v8-to-istanbul/-/ast-v8-to-istanbul-1.0.8.tgz} balanced-match@4.0.3: resolution: {integrity: sha512-1pHv8LX9CpKut1Zp4EXey7Z8OfH11ONNH6Dhi2WDUt31VVZFXZzKwXcysBgqSumFCmR+0dqjMK5v5JiFHzi0+g==, tarball: https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.3.tgz} @@ -3630,7 +3630,7 @@ snapshots: eslint: 10.14.0(jiti@2.7.0) eslint-visitor-keys: 3.4.3 - '@eslint-community/regexpp@4.12.2': {} + '@eslint-community/regexpp@4.12.3': {} '@eslint/config-array@0.23.5': dependencies: @@ -4279,7 +4279,7 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.68.0': optional: true - '@softarc/native-federation-orchestrator@4.9.0(@softarc/native-federation@4.10.0)': + '@softarc/native-federation-orchestrator@4.10.0(@softarc/native-federation@4.10.0)': dependencies: semver: 7.8.6 optionalDependencies: @@ -4332,7 +4332,7 @@ snapshots: '@typescript-eslint/eslint-plugin@8.72.1(@typescript-eslint/parser@8.72.1(eslint@10.14.0(jiti@2.7.0))(typescript@6.0.3))(eslint@10.14.0(jiti@2.7.0))(typescript@6.0.3)': dependencies: - '@eslint-community/regexpp': 4.12.2 + '@eslint-community/regexpp': 4.12.3 '@typescript-eslint/parser': 8.72.1(eslint@10.14.0(jiti@2.7.0))(typescript@6.0.3) '@typescript-eslint/scope-manager': 8.72.1 '@typescript-eslint/type-utils': 8.72.1(eslint@10.14.0(jiti@2.7.0))(typescript@6.0.3) @@ -4429,7 +4429,7 @@ snapshots: dependencies: '@bcoe/v8-coverage': 1.0.2 '@vitest/utils': 4.1.12 - ast-v8-to-istanbul: 1.0.7 + ast-v8-to-istanbul: 1.0.8 istanbul-lib-coverage: 3.2.2 istanbul-lib-report: 3.0.1 istanbul-reports: 3.2.0 @@ -4532,7 +4532,7 @@ snapshots: assertion-error@2.0.1: {} - ast-v8-to-istanbul@1.0.7: + ast-v8-to-istanbul@1.0.8: dependencies: '@jridgewell/trace-mapping': 0.3.31 estree-walker: 3.0.3 @@ -4784,7 +4784,7 @@ snapshots: eslint@10.14.0(jiti@2.7.0): dependencies: '@eslint-community/eslint-utils': 4.10.1(eslint@10.14.0(jiti@2.7.0)) - '@eslint-community/regexpp': 4.12.2 + '@eslint-community/regexpp': 4.12.3 '@eslint/config-array': 0.23.5 '@eslint/config-helpers': 0.7.0 '@eslint/core': 1.2.1 diff --git a/src/config/share-utils.spec.ts b/src/config/share-utils.spec.ts index e7058a54..3367c761 100644 --- a/src/config/share-utils.spec.ts +++ b/src/config/share-utils.spec.ts @@ -156,24 +156,11 @@ describe('withNativeFederation', () => { expect(mockCoreWithNativeFederation.mock.calls[0]![0].platform).toBe('browser'); }); - it('removes @angular/common/locales entries when ignoreUnusedDeps is disabled', () => { + // Locale keys only get here when the user lists them (shareAngularLocales) or opts into + // `includeSecondaries: { resolveGlob: true }`, so they are kept either way. + it.each([true, false])('keeps locale entries when ignoreUnusedDeps is %s', ignoreUnusedDeps => { mockCoreWithNativeFederation.mockReturnValue({ - features: { ignoreUnusedDeps: false }, - shared: { - '@angular/core': { singleton: true }, - '@angular/common/locales/de': { singleton: true }, - '@angular/common/locales/fr': { singleton: true }, - }, - }); - - const result = withNativeFederation({ shared: {} } as never); - - expect(Object.keys(result.shared)).toEqual(['@angular/core']); - }); - - it('keeps locale entries when ignoreUnusedDeps is enabled', () => { - mockCoreWithNativeFederation.mockReturnValue({ - features: { ignoreUnusedDeps: true }, + features: { ignoreUnusedDeps }, shared: { '@angular/core': { singleton: true }, '@angular/common/locales/de': { singleton: true }, diff --git a/src/config/share-utils.ts b/src/config/share-utils.ts index 6c59d17b..beb18d67 100644 --- a/src/config/share-utils.ts +++ b/src/config/share-utils.ts @@ -14,10 +14,7 @@ import { withNativeFederation as coreWithNativeFederation, } from "@softarc/native-federation/config"; import { NG_SKIP_LIST } from "./angular-skip-list.js"; -import type { - NormalizedFederationConfig, - NormalizedSharedExternalsConfig, -} from "@softarc/native-federation/internal"; +import type { NormalizedFederationConfig } from "@softarc/native-federation/internal"; import { existsSync, readFileSync } from "node:fs"; import * as path from "node:path"; import { cwd } from "node:process"; @@ -64,14 +61,7 @@ export function withNativeFederation( cfg.platform = getDefaultPlatform(Object.keys(cfg.shared ?? {})); } - const normalized = coreWithNativeFederation(cfg); - - // This is for being backwards compatible - if (!normalized.features.ignoreUnusedDeps) { - normalized.shared = removeNgLocales(normalized.shared); - } - - return normalized; + return coreWithNativeFederation(cfg); } /** @@ -166,21 +156,3 @@ function fromBuilder( ? (value as ConfigBuilder).get() : (value as T | undefined); } - -function removeNgLocales( - shared: NormalizedSharedExternalsConfig, -): NormalizedSharedExternalsConfig { - const keys = Object.keys(shared).filter( - (k) => !k.startsWith("@angular/common/locales"), - ); - - const filtered = keys.reduce( - (acc, curr) => ({ - ...acc, - [curr]: shared[curr], - }), - {}, - ); - - return filtered; -} diff --git a/src/schematics/init/steps/add-dependencies.spec.ts b/src/schematics/init/steps/add-dependencies.spec.ts index f2e10e57..acd41e7f 100644 --- a/src/schematics/init/steps/add-dependencies.spec.ts +++ b/src/schematics/init/steps/add-dependencies.spec.ts @@ -1,5 +1,7 @@ import { EmptyTree, type Tree } from '@angular-devkit/schematics'; +import { readFileSync } from 'node:fs'; + import { addDependencies } from './add-dependencies.js'; const CONTEXT = { addTask: () => undefined } as never; @@ -51,4 +53,22 @@ describe('addDependencies', () => { /@angular\/core is not a dependency/ ); }); + + // A lower range than the adapter's own lets an existing lockfile keep an older orchestrator + // next to the adapter's, so the workspace ends up with two copies. + it('pins the orchestrator to the range the adapter itself depends on', () => { + const adapterPkg = JSON.parse( + readFileSync(new URL('../../../../package.json', import.meta.url), 'utf8') + ); + tree.overwrite( + 'package.json', + JSON.stringify({ devDependencies: { '@softarc/native-federation-orchestrator': '^4.0.0' } }) + ); + + addDependencies(tree, CONTEXT, { ssr: false, webcomponent: false }); + + expect(packageJson(tree).devDependencies['@softarc/native-federation-orchestrator']).toBe( + adapterPkg.dependencies['@softarc/native-federation-orchestrator'] + ); + }); }); diff --git a/src/schematics/init/steps/add-dependencies.ts b/src/schematics/init/steps/add-dependencies.ts index d0244ef1..f65ca025 100644 --- a/src/schematics/init/steps/add-dependencies.ts +++ b/src/schematics/init/steps/add-dependencies.ts @@ -30,7 +30,7 @@ export function addDependencies( addDependency('dependencies', 'es-module-shims', '^2.8.0'); - addDependency('devDependencies', '@softarc/native-federation-orchestrator', '^4.8.0', true); + addDependency('devDependencies', '@softarc/native-federation-orchestrator', '^4.10.0', true); if (ssr) { console.log('SSR detected ...'); diff --git a/src/schematics/init/steps/generate-remote-map.spec.ts b/src/schematics/init/steps/generate-remote-map.spec.ts new file mode 100644 index 00000000..166c351e --- /dev/null +++ b/src/schematics/init/steps/generate-remote-map.spec.ts @@ -0,0 +1,41 @@ +import { generateRemoteMap } from './generate-remote-map.js'; + +function app(port?: number) { + return { + projectType: 'application', + architect: { build: {}, serve: { options: port ? { port } : {} } }, + }; +} + +describe('generateRemoteMap', () => { + // The init template writes `name: ''` into the remote's federation config, so a + // camelized key (`wcRemote`) would never match the remote's registered name `wc-remote`. + it('keys each remote by its raw project name', () => { + const workspace = { + projects: { shell: app(4200), 'wc-remote': app(4201) }, + }; + + expect(generateRemoteMap(workspace, 'shell')).toEqual({ + 'wc-remote': 'http://localhost:4201/remoteEntry.json', + }); + }); + + it('prefers the original serve port once a sibling has been federated', () => { + const remote = app(9999); + Object.assign(remote.architect, { 'serve-original': { options: { port: 4201 } } }); + + expect( + generateRemoteMap({ projects: { shell: app(), mfe1: remote } }, 'shell'), + ).toEqual({ + mfe1: 'http://localhost:4201/remoteEntry.json', + }); + }); + + it('skips libraries and the project itself', () => { + const workspace = { + projects: { shell: app(4200), lib: { ...app(), projectType: 'library' } }, + }; + + expect(generateRemoteMap(workspace, 'shell')).toEqual({}); + }); +}); diff --git a/src/schematics/init/steps/generate-remote-map.ts b/src/schematics/init/steps/generate-remote-map.ts index 6fa9d558..7d981f28 100644 --- a/src/schematics/init/steps/generate-remote-map.ts +++ b/src/schematics/init/steps/generate-remote-map.ts @@ -1,5 +1,3 @@ -import { strings } from '@angular-devkit/core'; - export function generateRemoteMap(workspace: any, projectName: string) { const result = {} as Record; @@ -17,7 +15,8 @@ export function generateRemoteMap(workspace: any, projectName: string) { project.architect['serve-original']?.options?.port ?? project.architect.serve?.options?.port ?? 4200; - result[strings.camelize(p)] = `http://localhost:${pPort}/remoteEntry.json`; + // The key must equal the remote's federation.config `name`, which is the raw project name. + result[p] = `http://localhost:${pPort}/remoteEntry.json`; } } diff --git a/src/schematics/remove/schematic.spec.ts b/src/schematics/remove/schematic.spec.ts new file mode 100644 index 00000000..bb6c26a5 --- /dev/null +++ b/src/schematics/remove/schematic.spec.ts @@ -0,0 +1,87 @@ +import { EmptyTree, type Tree } from '@angular-devkit/schematics'; + +import remove from './schematic.js'; + +// Shape of a project right after `init`: the federation builder took over `build` and the +// original application builder (with polyfills) moved to `esbuild`. +function federatedWorkspace() { + return { + projects: { + mfe1: { + root: 'projects/mfe1', + sourceRoot: 'projects/mfe1/src', + architect: { + build: { + builder: '@angular-architects/native-federation:build', + options: { target: 'mfe1:esbuild:production' }, + }, + esbuild: { + builder: '@angular/build:application', + options: { + browser: 'projects/mfe1/src/main.ts', + polyfills: ['zone.js', 'es-module-shims'], + }, + }, + serve: { + builder: '@angular-architects/native-federation:build', + options: {}, + }, + 'serve-original': { + builder: '@angular/build:dev-server', + configurations: { + production: { buildTarget: 'mfe1:esbuild:production' }, + development: { buildTarget: 'mfe1:esbuild:development' }, + }, + }, + }, + }, + }, + }; +} + +function withPolyfills(polyfills: string[]) { + const workspace = federatedWorkspace(); + workspace.projects.mfe1.architect.esbuild.options.polyfills = polyfills; + return JSON.stringify(workspace); +} + +async function run(tree: Tree) { + const rule = remove({ project: 'mfe1' } as never); + await (rule as (t: Tree) => Promise)(tree); + return JSON.parse(tree.readText('angular.json')); +} + +describe('remove', () => { + let tree: Tree; + + beforeEach(() => { + tree = new EmptyTree(); + tree.create('angular.json', JSON.stringify(federatedWorkspace())); + }); + + it('restores the original build target without es-module-shims', async () => { + const { architect } = (await run(tree)).projects.mfe1; + + expect(architect.esbuild).toBeUndefined(); + expect(architect.build.builder).toBe('@angular/build:application'); + expect(architect.build.options.polyfills).toEqual(['zone.js']); + }); + + it('points serve back at the build target', async () => { + const { architect } = (await run(tree)).projects.mfe1; + + expect(architect.serve.builder).toBe('@angular/build:dev-server'); + expect(architect.serve.configurations.production.buildTarget).toBe( + 'mfe1:build:production', + ); + }); + + // A zoneless app only had es-module-shims, which init added; leave no empty array behind. + it('drops the polyfills option once es-module-shims was the only entry', async () => { + tree.overwrite('angular.json', withPolyfills(['es-module-shims'])); + + const { architect } = (await run(tree)).projects.mfe1; + + expect(architect.build.options).not.toHaveProperty('polyfills'); + }); +}); diff --git a/src/schematics/remove/schematic.ts b/src/schematics/remove/schematic.ts index 1610b6f5..fda65e2c 100644 --- a/src/schematics/remove/schematic.ts +++ b/src/schematics/remove/schematic.ts @@ -5,13 +5,12 @@ import type { NfSchematicSchema } from './schema.js'; import * as path from 'path'; type NormalizedOptions = { - polyfills: string; + buildOptions: { polyfills?: string | string[] }; projectName: string; projectRoot: string; projectSourceRoot: string; manifestPath: string; projectConfig: any; - main: string; }; export default function remove(options: NfSchematicSchema): Rule { @@ -21,13 +20,13 @@ export default function remove(options: NfSchematicSchema): Rule { const normalized = normalizeOptions(options, workspace); - const { polyfills, projectRoot } = normalized; + const { buildOptions, projectRoot } = normalized; const bootstrapPath = path.join(projectRoot, 'src/bootstrap.ts'); const mainPath = path.join(projectRoot, 'src/main.ts'); makeMainSync(tree, bootstrapPath, mainPath); - updatePolyfills(tree, polyfills); + updatePolyfills(tree, buildOptions); updateWorkspaceConfig(tree, normalized, workspace, workspaceFileName); }; } @@ -96,29 +95,31 @@ function normalizeOptions(options: NfSchematicSchema, workspace: any): Normalize .join(projectRoot, 'src/assets/federation.manifest.json') .replace(/\\/g, '/'); - const main = projectConfig.architect.build.options.main; + // After init, `build` is the federation builder and the original options live on `esbuild`. + const buildOptions = (projectConfig.architect.esbuild ?? projectConfig.architect.build).options; - if (!projectConfig.architect.build.options.polyfills) { - projectConfig.architect.build.options.polyfills = []; - } - - const polyfills = projectConfig.architect.build.options.polyfills; return { - polyfills, + buildOptions, projectName, projectRoot, projectSourceRoot, manifestPath, projectConfig, - main, }; } -function updatePolyfills(tree: Tree, polyfills: string | string[]) { +function updatePolyfills(tree: Tree, buildOptions: NormalizedOptions['buildOptions']) { + const { polyfills } = buildOptions; if (typeof polyfills === 'string') { updatePolyfillsFile(tree, polyfills); + return; + } + + const remaining = (polyfills ?? []).filter(p => p !== 'es-module-shims'); + if (remaining.length) { + buildOptions.polyfills = remaining; } else { - updatePolyfillsArray(tree, polyfills); + delete buildOptions.polyfills; } } @@ -130,17 +131,6 @@ function updatePolyfillsFile(tree: Tree, polyfills: string) { } } -function updatePolyfillsArray(_tree: Tree, polyfills: string[]) { - const polyfillsConfig = polyfills as string[]; - - const index = polyfillsConfig.findIndex(p => p === 'es-module-shims'); - if (index === -1) { - return; - } - - polyfillsConfig.splice(index, 1); -} - export function getWorkspaceFileName(tree: Tree): string { if (tree.exists('angular.json')) { return 'angular.json';