Skip to content

Commit 81486c2

Browse files
alan-agius4alxhub
authored andcommitted
fix(localize): add @angular/localize/init as polyfill in angular.json (angular#56300)
This commit addresses an issue where the `@angular/localize/init` polyfill is not included when there are no polyfills specified in the `angular.json` file. PR Close angular#56300
1 parent 181ed2a commit 81486c2

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

packages/localize/schematics/ng-add/index.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,51 @@ import {
2323
removePackageJsonDependency,
2424
} from '@schematics/angular/utility/dependencies';
2525
import {JSONFile, JSONPath} from '@schematics/angular/utility/json-file';
26-
import {getWorkspace} from '@schematics/angular/utility/workspace';
26+
import {getWorkspace, updateWorkspace} from '@schematics/angular/utility/workspace';
2727
import {Builders} from '@schematics/angular/utility/workspace-models';
2828

2929
import {Schema} from './schema';
3030

3131
const localizeType = `@angular/localize`;
32+
const localizePolyfill = '@angular/localize/init';
3233
const localizeTripleSlashType = `/// <reference types="@angular/localize" />`;
3334

35+
function addPolyfillToConfig(projectName: string): Rule {
36+
return updateWorkspace((workspace) => {
37+
const project = workspace.projects.get(projectName);
38+
if (!project) {
39+
throw new SchematicsException(`Invalid project name '${projectName}'.`);
40+
}
41+
42+
const isLocalizePolyfill = (path: string) => path.startsWith('@angular/localize');
43+
44+
for (const target of project.targets.values()) {
45+
switch (target.builder) {
46+
case Builders.Karma:
47+
case Builders.Server:
48+
case Builders.Browser:
49+
case Builders.BrowserEsbuild:
50+
case Builders.Application:
51+
target.options ??= {};
52+
const value = target.options['polyfills'];
53+
if (typeof value === 'string') {
54+
if (!isLocalizePolyfill(value)) {
55+
target.options['polyfills'] = [value, localizePolyfill];
56+
}
57+
} else if (Array.isArray(value)) {
58+
if (!(value as string[]).some(isLocalizePolyfill)) {
59+
value.push(localizePolyfill);
60+
}
61+
} else {
62+
target.options['polyfills'] = [localizePolyfill];
63+
}
64+
65+
break;
66+
}
67+
}
68+
});
69+
}
70+
3471
function addTypeScriptConfigTypes(projectName: string): Rule {
3572
return async (host: Tree) => {
3673
const workspace = await getWorkspace(host);
@@ -45,6 +82,7 @@ function addTypeScriptConfigTypes(projectName: string): Rule {
4582
switch (target.builder) {
4683
case Builders.Karma:
4784
case Builders.Server:
85+
case Builders.BrowserEsbuild:
4886
case Builders.Browser:
4987
case Builders.Application:
5088
const value = target.options?.['tsConfig'];
@@ -55,7 +93,7 @@ function addTypeScriptConfigTypes(projectName: string): Rule {
5593
break;
5694
}
5795

58-
if (target.builder === Builders.Browser) {
96+
if (target.builder === Builders.Browser || target.builder === Builders.BrowserEsbuild) {
5997
const value = target.options?.['main'];
6098
if (typeof value === 'string') {
6199
addTripleSlashType(host, value);
@@ -132,6 +170,7 @@ export default function (options: Schema): Rule {
132170

133171
return chain([
134172
addTypeScriptConfigTypes(projectName),
173+
addPolyfillToConfig(projectName),
135174
// If `$localize` will be used at runtime then must install `@angular/localize`
136175
// into `dependencies`, rather than the default of `devDependencies`.
137176
options.useAtRuntime ? moveToDependencies : noop(),

packages/localize/schematics/ng-add/index_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('ng-add schematic', () => {
6060
options: {
6161
browser: './main.ts',
6262
tsConfig: './tsconfig.application.json',
63+
polyfills: ['zone.js'],
6364
},
6465
},
6566
build: {
@@ -73,6 +74,7 @@ describe('ng-add schematic', () => {
7374
builder: '@angular-devkit/build-angular:karma',
7475
options: {
7576
tsConfig: './tsconfig.spec.json',
77+
polyfills: 'zone.js',
7678
},
7779
},
7880
server: {
@@ -157,6 +159,27 @@ describe('ng-add schematic', () => {
157159
expect(types).toHaveSize(2);
158160
});
159161

162+
it(`should add '@angular/localize/init' in 'polyfills' in application builder`, async () => {
163+
host = await schematicRunner.runSchematic('ng-add', defaultOptions, host);
164+
const workspace = host.readJson('angular.json') as any;
165+
const polyfills = workspace.projects['demo'].architect.application.options.polyfills;
166+
expect(polyfills).toEqual(['zone.js', '@angular/localize/init']);
167+
});
168+
169+
it(`should add '@angular/localize/init' in 'polyfills' in karma builder`, async () => {
170+
host = await schematicRunner.runSchematic('ng-add', defaultOptions, host);
171+
const workspace = host.readJson('angular.json') as any;
172+
const polyfills = workspace.projects['demo'].architect.test.options.polyfills;
173+
expect(polyfills).toEqual(['zone.js', '@angular/localize/init']);
174+
});
175+
176+
it(`should add '@angular/localize/init' in 'polyfills' in browser builder`, async () => {
177+
host = await schematicRunner.runSchematic('ng-add', defaultOptions, host);
178+
const workspace = host.readJson('angular.json') as any;
179+
const polyfills = workspace.projects['demo'].architect.build.options.polyfills;
180+
expect(polyfills).toEqual(['@angular/localize/init']);
181+
});
182+
160183
it(`should add '@angular/localize' in 'types' tsconfigs referenced in karma builder`, async () => {
161184
const tsConfig = JSON.stringify({
162185
compilerOptions: {

0 commit comments

Comments
 (0)