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 @@ -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.9.0-next.0",
"@softarc/native-federation": "~4.9.0",
"@softarc/native-federation-orchestrator": "^4.8.0",
"es-module-shims": "^2.8.0",
"esbuild": "^0.28.0",
Expand Down
28 changes: 12 additions & 16 deletions pnpm-lock.yaml

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

11 changes: 5 additions & 6 deletions src/builders/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import { type JsonObject } from "@angular-devkit/core";
import {
buildForFederation,
createFederationCache,
type FederationInfo,
getExternals,
normalizeFederationOptions,
rebuildForFederation,
setBuildAdapter,
} from "@softarc/native-federation";
import {
AbortedError,
collectOutputFiles,
createNfWatcher,
linkedSharedDirs,
logger,
Expand Down Expand Up @@ -495,9 +495,8 @@ export async function* runBuilder(
fs.mkdirSync(federationOutputPath, { recursive: true });
}

let federationResult: FederationInfo;
try {
federationResult = await buildForFederation(
await buildForFederation(
normalized.config,
normalized.options,
externals,
Expand All @@ -523,7 +522,7 @@ export async function* runBuilder(
i18n,
localeFilter,
path.join(outputOptions.base, outputOptions.browser),
federationResult,
collectOutputFiles(normalized.options.federationCache).map((f) => f.fileName),
context.workspaceRoot,
);
logger.measure(start, "To translate the artifacts.");
Expand Down Expand Up @@ -610,7 +609,7 @@ export async function* runBuilder(

if (nfWatcher) nfWatcher.clear();

federationResult = await rebuildForFederation(
await rebuildForFederation(
normalized.config,
normalized.options,
externals,
Expand All @@ -629,7 +628,7 @@ export async function* runBuilder(
i18n,
localeFilter,
path.join(outputOptions.base, outputOptions.browser),
federationResult,
collectOutputFiles(normalized.options.federationCache).map((f) => f.fileName),
context.workspaceRoot,
);
}
Expand Down
20 changes: 6 additions & 14 deletions src/builders/build/i18n.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import type { FederationInfo } from '@softarc/native-federation';

import { translateFederationArtifacts, type I18nConfig } from './i18n.js';

Expand Down Expand Up @@ -44,22 +43,21 @@ describe('translateFederationArtifacts with the real localize-translate CLI', ()
write(sourceFile('cmp.js'), 'export const t = $localize`:@@greeting:Hello`;');
write(sourceFile('chunk.js'), 'export const c = $localize`:@@greeting:Hello`;');
write(sourceFile('chunk.css'), 'a{}');
write(sourceFile('chunk.js.map'), '{"version":3}');
// Angular's own output sits in the same folder and must be left alone
write(sourceFile('main.js'), 'export const m = $localize`:@@greeting:Hello`;');

const i18n: I18nConfig = { sourceLocale: 'en', locales: { de: translation } };
const federationResult = {
shared: [],
exposes: [{ outFileName: 'cmp.js' }],
chunks: { c1: ['chunk.js', 'chunk.css'] },
} as unknown as FederationInfo;
// core reports every file it wrote, chunks of non-dense bundles and source maps included
const federationFiles = ['cmp.js', 'chunk.js', 'chunk.css', 'chunk.js.map'];

translateFederationArtifacts(i18n, true, 'dist/browser', federationResult, workspaceRoot);
translateFederationArtifacts(i18n, true, 'dist/browser', federationFiles, workspaceRoot);

const de = (name: string) => fs.readFileSync(path.join(browser, 'de', name), 'utf8');
expect(de('cmp.js')).toContain('Hallo');
expect(de('chunk.js')).toContain('Hallo');
expect(de('chunk.css')).toBe('a{}');
expect(de('chunk.js.map')).toBe('{"version":3}');
expect(fs.existsSync(path.join(browser, 'de', 'main.js'))).toBe(false);
});

Expand All @@ -71,14 +69,8 @@ describe('translateFederationArtifacts with the real localize-translate CLI', ()
write(path.join(browser, 'en', 'remoteEntry.json'), '{}');

const i18n: I18nConfig = { sourceLocale: 'en', locales: { de: translation } };
const federationResult = {
shared: [],
exposes: [{ outFileName: 'missing.js' }],
chunks: {},
} as unknown as FederationInfo;

expect(() =>
translateFederationArtifacts(i18n, true, 'dist/browser', federationResult, workspaceRoot)
translateFederationArtifacts(i18n, true, 'dist/browser', ['missing.js'], workspaceRoot)
).toThrow(/ENOENT/);
});
});
56 changes: 14 additions & 42 deletions src/builders/build/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as fs from 'fs';
import { execFileSync } from 'child_process';
import { logger } from '@softarc/native-federation/internal';
import type { BuilderContext } from '@angular-devkit/architect';
import type { FederationInfo } from '@softarc/native-federation';

import {
getI18nConfig,
Expand Down Expand Up @@ -34,11 +33,7 @@ const infoSpy = vi.spyOn(logger, 'info').mockImplementation(() => undefined);
const debugSpy = vi.spyOn(logger, 'debug').mockImplementation(() => undefined);
const errorSpy = vi.spyOn(logger, 'error').mockImplementation(() => undefined);

const federationResult = {
shared: [{ outFileName: 'dep1.js' }],
exposes: [{ outFileName: 'cmp.js' }],
chunks: { c1: ['chunk1.js'] },
} as unknown as FederationInfo;
const federationFiles = ['dep1.js', 'cmp.js', 'chunk1.js'];

afterEach(() => {
vi.clearAllMocks();
Expand Down Expand Up @@ -91,23 +86,23 @@ describe('translateFederationArtifacts', () => {
});

it('does nothing when no configured locale matches the requested ones', async () => {
translateFederationArtifacts(i18n, ['es'], 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(i18n, ['es'], 'dist/browser', federationFiles, '/ws');

expect(execFileSync).not.toHaveBeenCalled();
expect(fs.mkdirSync).not.toHaveBeenCalled();
expect(infoSpy).not.toHaveBeenCalled();
});

it('filters to the intersection of requested and configured locales', async () => {
translateFederationArtifacts(i18n, ['de'], 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(i18n, ['de'], 'dist/browser', federationFiles, '/ws');

const args = localizeArgs();
const targets = args.slice(args.indexOf('--target-locales') + 1, args.indexOf('-l'));
expect(targets).toEqual(['de']);
});

it('builds the localize-translate command from the federation output files', async () => {
translateFederationArtifacts(i18n, true, 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(i18n, true, 'dist/browser', federationFiles, '/ws');

const [file, args] = vi.mocked(execFileSync).mock.calls[0]!;
// the CLI script is run with node, not through a shell
Expand Down Expand Up @@ -137,7 +132,7 @@ describe('translateFederationArtifacts', () => {
.map(([from, to]) => [posix(String(from)), posix(String(to))]);

it('stages the federation output files from the source locale folder', () => {
translateFederationArtifacts(i18n, true, 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(i18n, true, 'dist/browser', federationFiles, '/ws');

expect(fs.mkdirSync).toHaveBeenCalledWith(expect.stringMatching(/\.input$/), {
recursive: true,
Expand All @@ -149,32 +144,9 @@ describe('translateFederationArtifacts', () => {
]);
});

it('collects dense shared entries when denseExternals is enabled', () => {
const denseResult = {
shared: [{ entries: { esm: 'dep1.js', legacy: 'dep1.legacy.js' } }],
exposes: [{ outFileName: 'cmp.js' }],
chunks: { c1: ['chunk1.js'] },
} as unknown as FederationInfo;

translateFederationArtifacts(i18n, true, 'dist/browser', denseResult, '/ws');

expect(stagedFiles().map(([from]) => from!.split('/').pop())).toEqual([
'dep1.js',
'dep1.legacy.js',
'cmp.js',
'chunk1.js',
]);
});

it('runs localize-translate once, however many files there are', () => {
const manyFiles = Array.from({ length: 1000 }, (_, i) => `chunk-${i}-${'x'.repeat(40)}.js`);
const bigResult = {
shared: [],
exposes: [],
chunks: { c1: [...manyFiles, manyFiles[0]!] },
} as unknown as FederationInfo;

translateFederationArtifacts(i18n, true, 'dist/browser', bigResult, '/ws');
translateFederationArtifacts(i18n, true, 'dist/browser', [...manyFiles, manyFiles[0]!], '/ws');

expect(execFileSync).toHaveBeenCalledTimes(1);
expect(valueAfter(localizeArgs(), '-s')).toBe('**/*');
Expand All @@ -189,7 +161,7 @@ describe('translateFederationArtifacts', () => {
locales: { de: 'm.de.xlf' },
};

translateFederationArtifacts(objLocaleI18n, true, 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(objLocaleI18n, true, 'dist/browser', federationFiles, '/ws');

expect(valueAfter(localizeArgs(), '-l')).toBe('en-US');
});
Expand All @@ -198,14 +170,14 @@ describe('translateFederationArtifacts', () => {

// outputPath.browser can be customised (or empty) in angular.json
it('translates under the configured browser output folder instead of "browser"', () => {
translateFederationArtifacts(i18n, ['de'], 'dist/web', federationResult, '/ws');
translateFederationArtifacts(i18n, ['de'], 'dist/web', federationFiles, '/ws');

expect(stagedFiles()[0]![0]).toBe('/ws/dist/web/en/dep1.js');
expect(mkdirTargets()).toContain('/ws/dist/web/de');
});

it('writes the locale folders directly under the base when the browser folder is empty', () => {
translateFederationArtifacts(i18n, ['de'], 'dist', federationResult, '/ws');
translateFederationArtifacts(i18n, ['de'], 'dist', federationFiles, '/ws');

expect(stagedFiles()[0]![0]).toBe('/ws/dist/en/dep1.js');
expect(mkdirTargets()).toContain('/ws/dist/de');
Expand All @@ -217,7 +189,7 @@ describe('translateFederationArtifacts', () => {
{ locales: { de: 'm.de.xlf' } },
true,
'dist/browser',
federationResult,
federationFiles,
'/ws'
);

Expand All @@ -230,7 +202,7 @@ describe('translateFederationArtifacts', () => {
});

it('creates a dist folder and copies the remoteEntry for each target locale', async () => {
translateFederationArtifacts(i18n, true, 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(i18n, true, 'dist/browser', federationFiles, '/ws');

const mkdirPaths = vi.mocked(fs.mkdirSync).mock.calls.map(c => String(c[0]));
expect(mkdirPaths.some(p => p.endsWith('browser/de'))).toBe(true);
Expand All @@ -247,7 +219,7 @@ describe('translateFederationArtifacts', () => {
});

it('resolves output, binary and cwd against workspaceRoot', async () => {
translateFederationArtifacts(i18n, ['de'], 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(i18n, ['de'], 'dist/browser', federationFiles, '/ws');

const [, args, opts] = vi.mocked(execFileSync).mock.calls[0]!;
expect(posix(args![0])).toContain('/ws/node_modules/@angular/localize/');
Expand All @@ -266,7 +238,7 @@ describe('translateFederationArtifacts', () => {
locales: { de: { translation: 'm.de.xlf', subPath: 'deutsch' }, fr: 'm.fr.xlf' },
};

translateFederationArtifacts(subPathI18n, true, 'dist/browser', federationResult, '/ws');
translateFederationArtifacts(subPathI18n, true, 'dist/browser', federationFiles, '/ws');

const args = localizeArgs();
// localize-translate reads the staged files, which come from the source subPath, but writes per locale code
Expand Down Expand Up @@ -299,7 +271,7 @@ describe('translateFederationArtifacts', () => {
});

expect(() =>
translateFederationArtifacts(i18n, ['de'], 'dist/browser', federationResult, '/ws')
translateFederationArtifacts(i18n, ['de'], 'dist/browser', federationFiles, '/ws')
).toThrow('localize boom');

expect(errorSpy).toHaveBeenCalledWith('localize boom');
Expand Down
Loading
Loading