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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

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

21 changes: 4 additions & 17 deletions src/config/share-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
32 changes: 2 additions & 30 deletions src/config/share-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -166,21 +156,3 @@ function fromBuilder<T>(
? (value as ConfigBuilder<T>).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;
}
20 changes: 20 additions & 0 deletions src/schematics/init/steps/add-dependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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']
);
});
});
2 changes: 1 addition & 1 deletion src/schematics/init/steps/add-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

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 ...');

Check warning on line 36 in src/schematics/init/steps/add-dependencies.ts

View workflow job for this annotation

GitHub Actions / verify

Unexpected console statement. Only these console methods are allowed: warn, error
console.log('Activating CORS ...');

Check warning on line 37 in src/schematics/init/steps/add-dependencies.ts

View workflow job for this annotation

GitHub Actions / verify

Unexpected console statement. Only these console methods are allowed: warn, error

addDependency('dependencies', 'cors', '^2.8.5');
}
Expand Down
41 changes: 41 additions & 0 deletions src/schematics/init/steps/generate-remote-map.spec.ts
Original file line number Diff line number Diff line change
@@ -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: '<project>'` 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({});
});
});
5 changes: 2 additions & 3 deletions src/schematics/init/steps/generate-remote-map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { strings } from '@angular-devkit/core';

export function generateRemoteMap(workspace: any, projectName: string) {

Check warning on line 1 in src/schematics/init/steps/generate-remote-map.ts

View workflow job for this annotation

GitHub Actions / verify

Unexpected any. Specify a different type
const result = {} as Record<string, string>;

for (const p in workspace.projects) {
Expand All @@ -17,7 +15,8 @@
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`;
}
}

Expand Down
87 changes: 87 additions & 0 deletions src/schematics/remove/schematic.spec.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>)(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');
});
});
Loading
Loading