ivy-libraries.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google Inc. 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.io/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const core_1 = require("@angular-devkit/core");
  11. const config_1 = require("../../utility/config");
  12. const json_utils_1 = require("../../utility/json-utils");
  13. const workspace_models_1 = require("../../utility/workspace-models");
  14. const utils_1 = require("./utils");
  15. /**
  16. * Updates a pre version 9 library to version 9 Ivy library.
  17. *
  18. * The main things that this migrations does are:
  19. * - Creates a production configuration for VE compilations.
  20. * - Create a prod tsconfig for which disables Ivy and enables VE compilations.
  21. */
  22. function updateLibraries() {
  23. return (tree, context) => {
  24. const logger = context.logger;
  25. const workspacePath = config_1.getWorkspacePath(tree);
  26. const workspace = utils_1.getWorkspace(tree);
  27. const recorder = tree.beginUpdate(workspacePath);
  28. for (const { target, project } of utils_1.getTargets(workspace, 'build', workspace_models_1.Builders.NgPackagr)) {
  29. const projectRoot = json_utils_1.findPropertyInAstObject(project, 'root');
  30. if (!projectRoot || projectRoot.kind !== 'string') {
  31. break;
  32. }
  33. const configurations = json_utils_1.findPropertyInAstObject(target, 'configurations');
  34. const tsConfig = core_1.join(core_1.normalize(projectRoot.value), 'tsconfig.lib.prod.json');
  35. if (!configurations || configurations.kind !== 'object') {
  36. // Configurations doesn't exist.
  37. json_utils_1.appendPropertyInAstObject(recorder, target, 'configurations', { production: { tsConfig } }, 10);
  38. createTsConfig(tree, tsConfig);
  39. continue;
  40. }
  41. const prodConfig = json_utils_1.findPropertyInAstObject(configurations, 'production');
  42. if (!prodConfig || prodConfig.kind !== 'object') {
  43. // Production configuration doesn't exist.
  44. json_utils_1.insertPropertyInAstObjectInOrder(recorder, configurations, 'production', { tsConfig }, 12);
  45. createTsConfig(tree, tsConfig);
  46. continue;
  47. }
  48. const tsConfigOption = json_utils_1.findPropertyInAstObject(prodConfig, 'tsConfig');
  49. if (!tsConfigOption || tsConfigOption.kind !== 'string') {
  50. // No tsconfig for production has been defined.
  51. json_utils_1.insertPropertyInAstObjectInOrder(recorder, prodConfig, 'tsConfig', tsConfig, 14);
  52. createTsConfig(tree, tsConfig);
  53. continue;
  54. }
  55. // tsConfig for production already exists.
  56. const tsConfigPath = tsConfigOption.value;
  57. const tsConfigAst = utils_1.readJsonFileAsAstObject(tree, tsConfigPath);
  58. if (!tsConfigAst) {
  59. logger.warn(`Cannot find file: ${tsConfigPath}`);
  60. continue;
  61. }
  62. const tsConfigRecorder = tree.beginUpdate(tsConfigPath);
  63. const ngCompilerOptions = json_utils_1.findPropertyInAstObject(tsConfigAst, 'angularCompilerOptions');
  64. if (!ngCompilerOptions) {
  65. // Add angularCompilerOptions to the production tsConfig
  66. json_utils_1.appendPropertyInAstObject(tsConfigRecorder, tsConfigAst, 'angularCompilerOptions', { enableIvy: false }, 2);
  67. tree.commitUpdate(tsConfigRecorder);
  68. continue;
  69. }
  70. if (ngCompilerOptions.kind === 'object') {
  71. const enableIvy = json_utils_1.findPropertyInAstObject(ngCompilerOptions, 'enableIvy');
  72. // Add enableIvy false
  73. if (!enableIvy) {
  74. json_utils_1.appendPropertyInAstObject(tsConfigRecorder, ngCompilerOptions, 'enableIvy', false, 4);
  75. tree.commitUpdate(tsConfigRecorder);
  76. continue;
  77. }
  78. if (enableIvy.kind !== 'false') {
  79. const { start, end } = enableIvy;
  80. tsConfigRecorder.remove(start.offset, end.offset - start.offset);
  81. tsConfigRecorder.insertLeft(start.offset, 'false');
  82. tree.commitUpdate(tsConfigRecorder);
  83. }
  84. }
  85. }
  86. tree.commitUpdate(recorder);
  87. return tree;
  88. };
  89. }
  90. exports.updateLibraries = updateLibraries;
  91. function createTsConfig(tree, tsConfigPath) {
  92. const tsConfigContent = {
  93. extends: './tsconfig.lib.json',
  94. angularCompilerOptions: {
  95. enableIvy: false,
  96. },
  97. };
  98. if (!tree.exists(tsConfigPath)) {
  99. tree.create(tsConfigPath, JSON.stringify(tsConfigContent, undefined, 2));
  100. }
  101. }