Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 18 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/config/share-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }),
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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' } }),
Expand Down
14 changes: 8 additions & 6 deletions src/config/share-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/schematics/init/files/federation.config.mjs__tmpl__
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading