Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BundleContextResult, BundlerContext } from '../../tools/esbuild/bundler
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
import { BuildOutputFileType } from '../../tools/esbuild/bundler-files';
import { checkCommonJSModules } from '../../tools/esbuild/commonjs-checker';
import { LOCALE_DATA_BASE_MODULE } from '../../tools/esbuild/i18n-locale-plugin';
import { extractLicenses } from '../../tools/esbuild/license-extractor';
import { profileAsync } from '../../tools/esbuild/profiling';
import {
Expand Down Expand Up @@ -210,6 +211,12 @@ export async function executeBuild(
const explicitExternal = new Set<string>();

const isExplicitExternal = (dep: string): boolean => {
// Locale-data imports are injected by the builder itself (see i18n-locale-plugin) and must stay resolvable
// even when the containing package is externalized by the user.
if (dep.startsWith(`${LOCALE_DATA_BASE_MODULE}/`)) {
return false;
}

if (exclusions.has(dep)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,33 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
// If not externalized, build will fail with a Node.js platform builtin error
expect(result?.success).toBeTrue();
});

it('should not externalize builder-injected i18n locale-data imports when @angular/common is external', async () => {
harness.useProject('test', {
root: '.',
sourceRoot: 'src',
cli: {
cache: {
enabled: false,
},
},
i18n: {
sourceLocale: 'fr',
},
});

harness.useTarget('build', {
...BASE_OPTIONS,
externalDependencies: ['@angular/common'],
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();

harness.expectFile('dist/browser/polyfills.js').toExist();
harness
.expectFile('dist/browser/polyfills.js')
.content.not.toMatch(/['"]@angular\/common\/locales\/global\/fr['"]/);
});
});
});
19 changes: 15 additions & 4 deletions packages/angular/build/src/tools/esbuild/i18n-locale-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { Plugin, ResolveResult } from 'esbuild';
import { createRequire } from 'node:module';
import { createProjectResolver } from '../../utils/resolve-project';

/**
* The internal namespace used by generated locale import statements and Angular locale data plugin.
Expand Down Expand Up @@ -50,7 +50,7 @@ export function createAngularLocaleDataPlugin(): Plugin {
}

let exact = true;
let localeRequire: NodeJS.Require | undefined;
let projectResolve: ((packageName: string) => string) | undefined;
while (partialLocaleTag) {
// Angular embeds the `en`/`en-US` locale into the framework and it does not need to be included again here.
// The onLoad hook below for the locale data namespace has an `empty` loader that will prevent inclusion.
Expand All @@ -73,10 +73,10 @@ export function createAngularLocaleDataPlugin(): Plugin {
let result: ResolveResult | undefined;
const { packages, absWorkingDir } = build.initialOptions;
if (packages === 'external' && absWorkingDir) {
localeRequire ??= createRequire(absWorkingDir + '/');
projectResolve ??= createProjectResolver(absWorkingDir);

try {
localeRequire.resolve(potentialPath);
projectResolve(potentialPath);

result = {
errors: [],
Expand All @@ -94,6 +94,17 @@ export function createAngularLocaleDataPlugin(): Plugin {
kind: 'import-statement',
resolveDir: absWorkingDir,
});
if (result && result.external && absWorkingDir) {
projectResolve ??= createProjectResolver(absWorkingDir);
try {
const resolvedPath = projectResolve(potentialPath);
result = {
...result,
external: false,
path: resolvedPath,
};
} catch {}
}
}

if (result?.path) {
Expand Down
Loading