Skip to content

Commit d6a3403

Browse files
committed
refactor(@angular/build): remove automatic addition of @angular/localize/init polyfill and related warnings
The logic that automatically added the `@angular/localize/init` polyfill has been removed. BREAKING CHANGE: The `@angular/localize/init` polyfill will no longer be added automatically to projects. To prevent runtime issues, ensure that this polyfill is manually included in the "polyfills" section of your "angular.json" file if your application relies on Angular localization features.
1 parent e58c585 commit d6a3403

File tree

4 files changed

+159
-29
lines changed

4 files changed

+159
-29
lines changed

packages/angular/build/src/tools/esbuild/application-code-bundle.ts

-28
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,12 @@ function getEsBuildCommonPolyfillsOptions(
444444
namespace,
445445
cache: sourceFileCache?.loadResultCache,
446446
loadContent: async (_, build) => {
447-
let hasLocalizePolyfill = false;
448447
let polyfillPaths = polyfills;
449448
let warnings: PartialMessage[] | undefined;
450449

451450
if (tryToResolvePolyfillsAsRelative) {
452451
polyfillPaths = await Promise.all(
453452
polyfills.map(async (path) => {
454-
hasLocalizePolyfill ||= path.startsWith('@angular/localize');
455453
if (path.startsWith('zone.js') || !extname(path)) {
456454
return path;
457455
}
@@ -465,32 +463,6 @@ function getEsBuildCommonPolyfillsOptions(
465463
return result.path ? potentialPathRelative : path;
466464
}),
467465
);
468-
} else {
469-
hasLocalizePolyfill = polyfills.some((p) => p.startsWith('@angular/localize'));
470-
}
471-
472-
// Add localize polyfill if needed.
473-
// TODO: remove in version 19 or later.
474-
if (!i18nOptions.shouldInline && !hasLocalizePolyfill) {
475-
const result = await build.resolve('@angular/localize', {
476-
kind: 'import-statement',
477-
resolveDir: workspaceRoot,
478-
});
479-
480-
if (result.path) {
481-
polyfillPaths.push('@angular/localize/init');
482-
483-
(warnings ??= []).push({
484-
text: 'Polyfill for "@angular/localize/init" was added automatically.',
485-
notes: [
486-
{
487-
text:
488-
'In the future, this functionality will be removed. ' +
489-
'Please add this polyfill in the "polyfills" section of your "angular.json" instead.',
490-
},
491-
],
492-
});
493-
}
494466
}
495467

496468
// Generate module contents with an import statement per defined polyfill
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
{
22
"schematics": {
33
"use-application-builder": {
4-
"version": "18.0.0",
4+
"version": "19.0.0",
55
"factory": "./use-application-builder/migration",
66
"description": "Migrate application projects to the new build system. Application projects that are using the '@angular-devkit/build-angular' package's 'browser' and/or 'browser-esbuild' builders will be migrated to use the new 'application' builder. You can read more about this, including known issues and limitations, here: https://angular.dev/tools/cli/build-system-migration",
77
"optional": true,
88
"documentation": "tools/cli/build-system-migration"
9+
},
10+
"update-workspace-config": {
11+
"version": "19.0.0",
12+
"factory": "./update-workspace-config/migration",
13+
"description": "Update the workspace configuration by replacing deprecated options in 'angular.json' for compatibility with the latest Angular CLI changes."
914
}
1015
}
1116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { Rule } from '@angular-devkit/schematics';
10+
import { allTargetOptions, updateWorkspace } from '../../utility/workspace';
11+
import { Builders, ProjectType } from '../../utility/workspace-models';
12+
13+
/**
14+
* Main entry point for the migration rule.
15+
*
16+
* This migration performs the following tasks:
17+
* - Loops through all application projects in the workspace.
18+
* - Identifies the build target for each application.
19+
* - If the `localize` option is enabled but the polyfill `@angular/localize/init` is not present,
20+
* it adds the polyfill to the `polyfills` option of the build target.
21+
*
22+
* This migration is specifically for application projects that use either the `application` or `browser-esbuild` builders.
23+
*/
24+
export default function (): Rule {
25+
return updateWorkspace((workspace) => {
26+
for (const project of workspace.projects.values()) {
27+
if (project.extensions.projectType !== ProjectType.Application) {
28+
continue;
29+
}
30+
31+
const buildTarget = project.targets.get('build');
32+
if (
33+
!buildTarget ||
34+
(buildTarget.builder !== Builders.BuildApplication &&
35+
buildTarget.builder !== Builders.Application &&
36+
buildTarget.builder !== Builders.BrowserEsbuild)
37+
) {
38+
continue;
39+
}
40+
41+
const polyfills = buildTarget.options?.['polyfills'];
42+
if (
43+
Array.isArray(polyfills) &&
44+
polyfills.some(
45+
(polyfill) => typeof polyfill === 'string' && polyfill.startsWith('@angular/localize'),
46+
)
47+
) {
48+
// Skip the polyfill is already added
49+
continue;
50+
}
51+
52+
for (const [, options] of allTargetOptions(buildTarget, false)) {
53+
if (options['localize']) {
54+
buildTarget.options ??= {};
55+
((buildTarget.options['polyfills'] ??= []) as string[]).push('@angular/localize/init');
56+
break;
57+
}
58+
}
59+
}
60+
});
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { EmptyTree } from '@angular-devkit/schematics';
10+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
11+
import { ProjectType } from '../../utility/workspace-models';
12+
13+
function createWorkSpaceConfig(tree: UnitTestTree) {
14+
const angularConfig = {
15+
version: 1,
16+
projects: {
17+
app: {
18+
root: '/project/app',
19+
sourceRoot: '/project/app/src',
20+
projectType: ProjectType.Application,
21+
prefix: 'app',
22+
architect: {
23+
build: {
24+
builder: '@angular/build:application',
25+
options: {
26+
localize: true,
27+
polyfills: [],
28+
},
29+
},
30+
},
31+
},
32+
},
33+
};
34+
35+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
36+
}
37+
38+
describe(`Migration to update the workspace configuration`, () => {
39+
const schematicName = 'update-workspace-config';
40+
const schematicRunner = new SchematicTestRunner(
41+
'migrations',
42+
require.resolve('../migration-collection.json'),
43+
);
44+
45+
let tree: UnitTestTree;
46+
beforeEach(() => {
47+
tree = new UnitTestTree(new EmptyTree());
48+
createWorkSpaceConfig(tree);
49+
});
50+
51+
it(`should add '@angular/localize/init' to polyfills if localize is enabled`, async () => {
52+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
53+
const {
54+
projects: { app },
55+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56+
} = newTree.readJson('/angular.json') as any;
57+
58+
expect(app.architect.build.options.polyfills).toContain('@angular/localize/init');
59+
});
60+
61+
it(`should not add '@angular/localize/init' to polyfills if it already exists`, async () => {
62+
// Add '@angular/localize/init' manually
63+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64+
const config = tree.readJson('/angular.json') as any;
65+
config.projects.app.architect.build.options.polyfills.push('@angular/localize/init');
66+
tree.overwrite('/angular.json', JSON.stringify(config, undefined, 2));
67+
68+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
69+
const {
70+
projects: { app },
71+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
72+
} = newTree.readJson('/angular.json') as any;
73+
74+
const polyfills = app.architect.build.options.polyfills;
75+
expect(polyfills.filter((p: string) => p === '@angular/localize/init').length).toBe(1);
76+
});
77+
78+
it(`should not add polyfills if localize is not enabled`, async () => {
79+
// Disable 'localize'
80+
const config = JSON.parse(tree.readContent('/angular.json'));
81+
config.projects.app.architect.build.options.localize = false;
82+
tree.overwrite('/angular.json', JSON.stringify(config, undefined, 2));
83+
84+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
85+
const {
86+
projects: { app },
87+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88+
} = newTree.readJson('/angular.json') as any;
89+
90+
expect(app.architect.build.options.polyfills).not.toContain('@angular/localize/init');
91+
});
92+
});

0 commit comments

Comments
 (0)