migration.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const workspace_1 = require("../../utility/workspace");
  12. const workspace_models_1 = require("../../utility/workspace-models");
  13. /**
  14. * Main entry point for the migration rule.
  15. *
  16. * This schematic migration performs updates to the Angular workspace configuration
  17. * to ensure that application projects are properly configured with polyfills
  18. * required for internationalization (`localize`).
  19. *
  20. * It specifically targets application projects that use either the `application`
  21. * or `browser-esbuild` builders.
  22. *
  23. * The migration process involves:
  24. *
  25. * 1. Iterating over all projects in the workspace.
  26. * 2. Checking each project to determine if it is an application-type project.
  27. * 3. For each application project, examining the associated build targets.
  28. * 4. If a build target's `localize` option is enabled but the polyfill
  29. * `@angular/localize/init` is missing from the `polyfills` array, the polyfill
  30. * is automatically added to ensure proper internationalization support.
  31. *
  32. * Additionally, this migration updates projects that use the `dev-server` or `extract-i18n`
  33. * builders to ensure that deprecated `browserTarget` options are migrated to the
  34. * newer `buildTarget` field.
  35. *
  36. */
  37. function default_1() {
  38. return (0, workspace_1.updateWorkspace)((workspace) => {
  39. for (const project of workspace.projects.values()) {
  40. if (project.extensions.projectType !== workspace_models_1.ProjectType.Application) {
  41. continue;
  42. }
  43. for (const target of project.targets.values()) {
  44. if (target.builder === workspace_models_1.Builders.DevServer || target.builder === workspace_models_1.Builders.ExtractI18n) {
  45. // Migrate `browserTarget` to `buildTarget`
  46. for (const [, options] of (0, workspace_1.allTargetOptions)(target, false)) {
  47. if (options['browserTarget'] && !options['buildTarget']) {
  48. options['buildTarget'] = options['browserTarget'];
  49. }
  50. delete options['browserTarget'];
  51. }
  52. }
  53. // Check if the target uses application-related builders
  54. if (target.builder !== workspace_models_1.Builders.BuildApplication &&
  55. target.builder !== workspace_models_1.Builders.Application &&
  56. target.builder !== workspace_models_1.Builders.BrowserEsbuild) {
  57. continue;
  58. }
  59. // Check if polyfills include '@angular/localize/init'
  60. const polyfills = target.options?.['polyfills'];
  61. if (Array.isArray(polyfills) &&
  62. polyfills.some((polyfill) => typeof polyfill === 'string' && polyfill.startsWith('@angular/localize'))) {
  63. // Skip if the polyfill is already present
  64. continue;
  65. }
  66. // Add '@angular/localize/init' polyfill if localize option is enabled
  67. for (const [, options] of (0, workspace_1.allTargetOptions)(target, false)) {
  68. if (options['localize']) {
  69. target.options ??= {};
  70. (target.options['polyfills'] ??= []).push('@angular/localize/init');
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. });
  77. }