From eb3ce3c94552ed78efe8273e8797d24b5e2613d7 Mon Sep 17 00:00:00 2001 From: Aukevanoost Date: Fri, 27 Nov 2026 17:01:31 +0100 Subject: [PATCH] feat: Support @softarc/native-federation 4.11 - fromPackageJson() takes an optional base config, so it falls back to core's strict-singleton, build: 'package' default - autoShareScope resolves projectPath with core's inferProjectPath, against workspaceRoot instead of the working directory - The init schematic generates fromPackageJson().patch(...) --- README.md | 8 +--- package.json | 2 +- pnpm-lock.yaml | 37 +++++++++---------- src/config/share-utils.spec.ts | 22 +++++++++++ src/config/share-utils.ts | 14 ++++--- .../init/files/federation.config.mjs__tmpl__ | 2 +- 6 files changed, 52 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 2b8fdce..53da27c 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,8 @@ export default withNativeFederation({ exposes: { "./Component": "./projects/mfe1/src/app/app.component.ts", }, - shared: fromPackageJson({ - singleton: true, - strictVersion: true, - requiredVersion: "auto", - build: "package", - }) + // Without a base config: singleton, strictVersion, requiredVersion 'auto', build 'package'. + shared: fromPackageJson() // includeSecondaries is an opt-out of ignoreUnusedDeps, so every Angular // entrypoint is shared and builds can't publish different sets of them. .patch(["@angular/*"], { includeSecondaries: { keepAll: true } }), diff --git a/package.json b/package.json index 5e4c03b..d324adc 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@angular-devkit/core": "~22.2.0", "@angular-devkit/schematics": "~22.2.0", "@chialab/esbuild-plugin-commonjs": "^0.19.0", - "@softarc/native-federation": "~4.10.0", + "@softarc/native-federation": "~4.11.0", "@softarc/native-federation-orchestrator": "^4.10.0", "es-module-shims": "^2.8.0", "esbuild": "^0.28.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2e8c107..a79c934 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,11 +25,11 @@ importers: specifier: ^0.19.0 version: 0.19.1 '@softarc/native-federation': - specifier: ~4.10.0 - version: 4.10.0 + specifier: ~4.11.0 + version: 4.11.0 '@softarc/native-federation-orchestrator': specifier: ^4.10.0 - version: 4.10.0(@softarc/native-federation@4.10.0) + version: 4.10.0(@softarc/native-federation@4.11.0) es-module-shims: specifier: ^2.8.0 version: 2.8.4 @@ -1572,11 +1572,12 @@ packages: '@softarc/native-federation': optional: true - '@softarc/native-federation@4.10.0': - resolution: {integrity: sha512-z9upOxlOgFsgxLr/44bv2hP+t4pUwzuyKrs4qUHWbRJY1jx277Lkvo9Ua2IEeysUaoZYh+wHGEl2mVvIotEQpA==, tarball: https://registry.npmjs.org/@softarc/native-federation/-/native-federation-4.10.0.tgz} + '@softarc/native-federation@4.11.0': + resolution: {integrity: sha512-hBVOO014K6SXRH6NYpvNVLqeFpZe7OTvA6cBFPA/chLIJC+hUM5GZr7hh6D1xJNcvtnuJ8d8mEhQIurACu+iDA==, tarball: https://registry.npmjs.org/@softarc/native-federation/-/native-federation-4.11.0.tgz} '@softarc/sheriff-core@0.20.0': resolution: {integrity: sha512-dTm5dOUMOGTV3ql7ZIuI8rE+gZawIcJLEluqRYX3KDVJ215vfUzpC9LlSL4pTW0lAAeNLelmPZBzY57jLvOPpg==, tarball: https://registry.npmjs.org/@softarc/sheriff-core/-/sheriff-core-0.20.0.tgz} + deprecated: Renamed to @sheriff-arch/sheriff-core. Update your dependencies and imports. hasBin: true peerDependencies: typescript: '>=4.8' @@ -3492,26 +3493,25 @@ snapshots: '@clack/core@1.4.3': dependencies: - fast-wrap-ansi: 0.2.0 + fast-wrap-ansi: 0.2.2 sisteransi: 1.0.5 '@clack/prompts@1.7.0': dependencies: '@clack/core': 1.4.3 fast-string-width: 3.0.2 - fast-wrap-ansi: 0.2.0 + fast-wrap-ansi: 0.2.2 sisteransi: 1.0.5 '@emnapi/core@1.11.2': dependencies: '@emnapi/wasi-threads': 1.2.2 tslib: 2.9.1 - optional: true '@emnapi/core@1.4.5': dependencies: '@emnapi/wasi-threads': 1.0.4 - tslib: 2.8.1 + tslib: 2.9.1 '@emnapi/core@2.0.0-alpha.6': dependencies: @@ -3522,11 +3522,10 @@ snapshots: '@emnapi/runtime@1.11.2': dependencies: tslib: 2.9.1 - optional: true '@emnapi/runtime@1.4.5': dependencies: - tslib: 2.8.1 + tslib: 2.9.1 '@emnapi/runtime@2.0.0-alpha.6': dependencies: @@ -3535,12 +3534,11 @@ snapshots: '@emnapi/wasi-threads@1.0.4': dependencies: - tslib: 2.8.1 + tslib: 2.9.1 '@emnapi/wasi-threads@1.2.2': dependencies: tslib: 2.9.1 - optional: true '@emnapi/wasi-threads@2.2.0': dependencies: @@ -3851,8 +3849,8 @@ snapshots: '@napi-rs/wasm-runtime@0.2.4': dependencies: - '@emnapi/core': 1.4.5 - '@emnapi/runtime': 1.4.5 + '@emnapi/core': 1.11.2 + '@emnapi/runtime': 1.11.2 '@tybys/wasm-util': 0.9.0 '@napi-rs/wasm-runtime@1.2.7(@emnapi/core@1.11.2)(@emnapi/runtime@1.11.2)': @@ -4279,18 +4277,19 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.68.0': optional: true - '@softarc/native-federation-orchestrator@4.10.0(@softarc/native-federation@4.10.0)': + '@softarc/native-federation-orchestrator@4.10.0(@softarc/native-federation@4.11.0)': dependencies: semver: 7.8.6 optionalDependencies: - '@softarc/native-federation': 4.10.0 + '@softarc/native-federation': 4.11.0 - '@softarc/native-federation@4.10.0': + '@softarc/native-federation@4.11.0': dependencies: '@jridgewell/sourcemap-codec': 1.6.0 '@softarc/sheriff-core': 0.20.0(typescript@6.0.3) chalk: 6.0.1 es-module-lexer: 2.3.2 + semver: 7.8.6 tinyglobby: 0.2.17 typescript: 6.0.3 @@ -4307,7 +4306,7 @@ snapshots: '@tybys/wasm-util@0.9.0': dependencies: - tslib: 2.8.1 + tslib: 2.9.1 '@types/chai@5.2.3': dependencies: diff --git a/src/config/share-utils.spec.ts b/src/config/share-utils.spec.ts index 3367c76..9a52ef7 100644 --- a/src/config/share-utils.spec.ts +++ b/src/config/share-utils.spec.ts @@ -23,6 +23,12 @@ vi.mock('@softarc/native-federation/config', () => ({ withNativeFederation: (...args: unknown[]) => mockCoreWithNativeFederation(...args), })); +const mockInferProjectPath = vi.fn((p?: string) => p ?? '/workspace'); + +vi.mock('@softarc/native-federation/internal', () => ({ + inferProjectPath: (...args: unknown[]) => mockInferProjectPath(...(args as [string?])), +})); + const mockExistsSync = vi.fn((p: string) => p.endsWith('package.json')); const mockReadFileSync = vi.fn(() => JSON.stringify({ dependencies: { '@angular/core': '^21.1.4' } }), @@ -77,6 +83,13 @@ describe('fromPackageJson', () => { expect(mockFromPackageJsonSkip).toHaveBeenCalledWith(NG_SKIP_LIST); }); + it('leaves the base config to core when none is given', () => { + fromPackageJson(); + + expect(mockCoreFromPackageJson).toHaveBeenCalledWith(undefined, ''); + expect(mockFromPackageJsonSkip).toHaveBeenCalledWith(NG_SKIP_LIST); + }); + it('passes through an explicit projectPath', () => { fromPackageJson({} as never, '/project'); @@ -185,6 +198,15 @@ describe('autoShareScope', () => { expect(autoShareScope({ projectPath: '/project' })).toBe('ng21.1'); }); + it('resolves the projectPath like core does (#256)', () => { + autoShareScope({ projectPath: 'apps/shell' }); + expect(mockInferProjectPath).toHaveBeenLastCalledWith('apps/shell'); + + autoShareScope(); + expect(mockInferProjectPath).toHaveBeenLastCalledWith(undefined); + expect(mockExistsSync).toHaveBeenCalledWith('/workspace/package.json'); + }); + it('pins to the major version at level "major"', () => { mockReadFileSync.mockReturnValueOnce( JSON.stringify({ dependencies: { '@angular/core': '^21.1.4' } }), diff --git a/src/config/share-utils.ts b/src/config/share-utils.ts index beb18d6..f838a26 100644 --- a/src/config/share-utils.ts +++ b/src/config/share-utils.ts @@ -14,10 +14,12 @@ import { withNativeFederation as coreWithNativeFederation, } from "@softarc/native-federation/config"; import { NG_SKIP_LIST } from "./angular-skip-list.js"; -import type { NormalizedFederationConfig } from "@softarc/native-federation/internal"; +import { + inferProjectPath, + type NormalizedFederationConfig, +} from "@softarc/native-federation/internal"; import { existsSync, readFileSync } from "node:fs"; import * as path from "node:path"; -import { cwd } from "node:process"; export function shareAll( config: ShareAllExternalsOptions, @@ -47,7 +49,7 @@ export function share( * {@link NG_SKIP_LIST} instead of core's `DEFAULT_SKIP_LIST`. */ export function fromPackageJson( - baseCfg: ShareAllExternalsOptions, + baseCfg?: ShareAllExternalsOptions, projectPath = "", ): PackageJsonExternalsBuilder { return coreFromPackageJson(baseCfg, projectPath).skip(NG_SKIP_LIST); @@ -85,7 +87,7 @@ export interface PackageShareScopeOptions { level?: "major" | "minor" | "patch"; /** Package whose version drives the scope. Defaults to `@angular/core`. */ dependency?: string; - /** Directory to start resolving `package.json` from. Defaults to `cwd()`. */ + /** Relative to `workspaceRoot`; defaults to the `packageJson` folder or `workspaceRoot`. */ projectPath?: string; prefix?: string; } @@ -107,12 +109,12 @@ export interface PackageShareScopeOptions { export function autoShareScope(opts: PackageShareScopeOptions = {}): string { const { level = "minor", - projectPath = cwd(), + projectPath, dependency = "@angular/core", prefix = "ng", } = opts; - let dir = projectPath; + let dir = inferProjectPath(projectPath); while ( !existsSync(path.join(dir, "package.json")) && path.dirname(dir) !== dir diff --git a/src/schematics/init/files/federation.config.mjs__tmpl__ b/src/schematics/init/files/federation.config.mjs__tmpl__ index a529f43..e049c51 100644 --- a/src/schematics/init/files/federation.config.mjs__tmpl__ +++ b/src/schematics/init/files/federation.config.mjs__tmpl__ @@ -11,7 +11,7 @@ export default withNativeFederation({ <% } %> //shareScope: autoShareScope({level: 'minor'}), - shared: fromPackageJson({ singleton: true, strictVersion: true, requiredVersion: 'auto', build: 'package' }) + shared: fromPackageJson() .patch(['@angular/*'], {includeSecondaries: { keepAll: true } } ), //sharedMappings: mappingsFromWorkspace({ singleton: true }).filter(['@my-org/*']).get(),