-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(@angular/build): remove automatic addition of `@angular/loca…
…lize/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.
- Loading branch information
1 parent
e58c585
commit d6a3403
Showing
4 changed files
with
159 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
packages/schematics/angular/migrations/migration-collection.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
{ | ||
"schematics": { | ||
"use-application-builder": { | ||
"version": "18.0.0", | ||
"version": "19.0.0", | ||
"factory": "./use-application-builder/migration", | ||
"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", | ||
"optional": true, | ||
"documentation": "tools/cli/build-system-migration" | ||
}, | ||
"update-workspace-config": { | ||
"version": "19.0.0", | ||
"factory": "./update-workspace-config/migration", | ||
"description": "Update the workspace configuration by replacing deprecated options in 'angular.json' for compatibility with the latest Angular CLI changes." | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/schematics/angular/migrations/update-workspace-config/migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { Rule } from '@angular-devkit/schematics'; | ||
import { allTargetOptions, updateWorkspace } from '../../utility/workspace'; | ||
import { Builders, ProjectType } from '../../utility/workspace-models'; | ||
|
||
/** | ||
* Main entry point for the migration rule. | ||
* | ||
* This migration performs the following tasks: | ||
* - Loops through all application projects in the workspace. | ||
* - Identifies the build target for each application. | ||
* - If the `localize` option is enabled but the polyfill `@angular/localize/init` is not present, | ||
* it adds the polyfill to the `polyfills` option of the build target. | ||
* | ||
* This migration is specifically for application projects that use either the `application` or `browser-esbuild` builders. | ||
*/ | ||
export default function (): Rule { | ||
return updateWorkspace((workspace) => { | ||
for (const project of workspace.projects.values()) { | ||
if (project.extensions.projectType !== ProjectType.Application) { | ||
continue; | ||
} | ||
|
||
const buildTarget = project.targets.get('build'); | ||
if ( | ||
!buildTarget || | ||
(buildTarget.builder !== Builders.BuildApplication && | ||
buildTarget.builder !== Builders.Application && | ||
buildTarget.builder !== Builders.BrowserEsbuild) | ||
) { | ||
continue; | ||
} | ||
|
||
const polyfills = buildTarget.options?.['polyfills']; | ||
if ( | ||
Array.isArray(polyfills) && | ||
polyfills.some( | ||
(polyfill) => typeof polyfill === 'string' && polyfill.startsWith('@angular/localize'), | ||
) | ||
) { | ||
// Skip the polyfill is already added | ||
continue; | ||
} | ||
|
||
for (const [, options] of allTargetOptions(buildTarget, false)) { | ||
if (options['localize']) { | ||
buildTarget.options ??= {}; | ||
((buildTarget.options['polyfills'] ??= []) as string[]).push('@angular/localize/init'); | ||
break; | ||
} | ||
} | ||
} | ||
}); | ||
} |
92 changes: 92 additions & 0 deletions
92
packages/schematics/angular/migrations/update-workspace-config/migration_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { EmptyTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { ProjectType } from '../../utility/workspace-models'; | ||
|
||
function createWorkSpaceConfig(tree: UnitTestTree) { | ||
const angularConfig = { | ||
version: 1, | ||
projects: { | ||
app: { | ||
root: '/project/app', | ||
sourceRoot: '/project/app/src', | ||
projectType: ProjectType.Application, | ||
prefix: 'app', | ||
architect: { | ||
build: { | ||
builder: '@angular/build:application', | ||
options: { | ||
localize: true, | ||
polyfills: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); | ||
} | ||
|
||
describe(`Migration to update the workspace configuration`, () => { | ||
const schematicName = 'update-workspace-config'; | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
beforeEach(() => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
createWorkSpaceConfig(tree); | ||
}); | ||
|
||
it(`should add '@angular/localize/init' to polyfills if localize is enabled`, async () => { | ||
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
const { | ||
projects: { app }, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} = newTree.readJson('/angular.json') as any; | ||
|
||
expect(app.architect.build.options.polyfills).toContain('@angular/localize/init'); | ||
}); | ||
|
||
it(`should not add '@angular/localize/init' to polyfills if it already exists`, async () => { | ||
// Add '@angular/localize/init' manually | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const config = tree.readJson('/angular.json') as any; | ||
config.projects.app.architect.build.options.polyfills.push('@angular/localize/init'); | ||
tree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); | ||
|
||
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
const { | ||
projects: { app }, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} = newTree.readJson('/angular.json') as any; | ||
|
||
const polyfills = app.architect.build.options.polyfills; | ||
expect(polyfills.filter((p: string) => p === '@angular/localize/init').length).toBe(1); | ||
}); | ||
|
||
it(`should not add polyfills if localize is not enabled`, async () => { | ||
// Disable 'localize' | ||
const config = JSON.parse(tree.readContent('/angular.json')); | ||
config.projects.app.architect.build.options.localize = false; | ||
tree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); | ||
|
||
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
const { | ||
projects: { app }, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} = newTree.readJson('/angular.json') as any; | ||
|
||
expect(app.architect.build.options.polyfills).not.toContain('@angular/localize/init'); | ||
}); | ||
}); |