differential-loading.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const core_1 = require("@angular-devkit/core");
  11. const json_utils_1 = require("../../utility/json-utils");
  12. const browserslistContent = `# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
  13. # For additional information regarding the format and rule options, please see:
  14. # https://github.com/browserslist/browserslist#queries
  15. # You can see what browsers were selected by your queries by running:
  16. # npx browserslist
  17. > 0.5%
  18. last 2 versions
  19. Firefox ESR
  20. not dead
  21. not IE 9-11 # For IE 9-11 support, remove 'not'.`;
  22. function updateES5Projects() {
  23. return (tree) => {
  24. // update workspace tsconfig
  25. updateTsConfig(tree, '/tsconfig.json');
  26. const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');
  27. if (!angularConfigContent) {
  28. return;
  29. }
  30. const angularJson = core_1.parseJson(angularConfigContent.toString(), core_1.JsonParseMode.Loose);
  31. if (!core_1.isJsonObject(angularJson) || !core_1.isJsonObject(angularJson.projects)) {
  32. // If that field isn't there, no use...
  33. return;
  34. }
  35. // For all projects
  36. for (const [name, project] of Object.entries(angularJson.projects)) {
  37. if (!core_1.isJsonObject(project)) {
  38. continue;
  39. }
  40. if (typeof project.root != 'string' || project.projectType !== 'application') {
  41. continue;
  42. }
  43. if (name.endsWith('-e2e')) {
  44. // Skip existing separate E2E projects
  45. continue;
  46. }
  47. // Older projects app and spec ts configs had script and module set in them.
  48. const architect = project.architect;
  49. if (!(core_1.isJsonObject(architect)
  50. && core_1.isJsonObject(architect.build)
  51. && architect.build.builder === '@angular-devkit/build-angular:browser')) {
  52. // Skip projects who's build builder is not build-angular:browser
  53. continue;
  54. }
  55. const buildOptionsConfig = architect.build.options;
  56. if (core_1.isJsonObject(buildOptionsConfig) && typeof buildOptionsConfig.tsConfig === 'string') {
  57. updateTsConfig(tree, buildOptionsConfig.tsConfig);
  58. }
  59. const testConfig = architect.test;
  60. if (core_1.isJsonObject(testConfig)
  61. && core_1.isJsonObject(testConfig.options)
  62. && typeof testConfig.options.tsConfig === 'string') {
  63. updateTsConfig(tree, testConfig.options.tsConfig);
  64. }
  65. const browserslistPath = core_1.join(core_1.normalize(project.root), 'browserslist');
  66. // Move the CLI 7 style browserlist to root if it's there.
  67. const sourceRoot = project.sourceRoot === 'string'
  68. ? project.sourceRoot
  69. : core_1.join(core_1.normalize(project.root), 'src');
  70. const srcBrowsersList = core_1.join(core_1.normalize(sourceRoot), 'browserslist');
  71. if (tree.exists(srcBrowsersList)) {
  72. tree.rename(srcBrowsersList, browserslistPath);
  73. }
  74. else if (!tree.exists(browserslistPath)) {
  75. tree.create(browserslistPath, browserslistContent);
  76. }
  77. }
  78. return tree;
  79. };
  80. }
  81. exports.updateES5Projects = updateES5Projects;
  82. function updateTsConfig(tree, tsConfigPath) {
  83. const buffer = tree.read(tsConfigPath);
  84. if (!buffer) {
  85. return;
  86. }
  87. const tsCfgAst = core_1.parseJsonAst(buffer.toString(), core_1.JsonParseMode.Loose);
  88. if (tsCfgAst.kind !== 'object') {
  89. return;
  90. }
  91. const configExtends = json_utils_1.findPropertyInAstObject(tsCfgAst, 'extends');
  92. const isExtendedConfig = configExtends && configExtends.kind === 'string';
  93. const compilerOptions = json_utils_1.findPropertyInAstObject(tsCfgAst, 'compilerOptions');
  94. if (!compilerOptions || compilerOptions.kind !== 'object') {
  95. return;
  96. }
  97. const recorder = tree.beginUpdate(tsConfigPath);
  98. if (isExtendedConfig) {
  99. json_utils_1.removePropertyInAstObject(recorder, compilerOptions, 'target');
  100. json_utils_1.removePropertyInAstObject(recorder, compilerOptions, 'module');
  101. json_utils_1.removePropertyInAstObject(recorder, compilerOptions, 'downlevelIteration');
  102. }
  103. else {
  104. const downlevelIteration = json_utils_1.findPropertyInAstObject(compilerOptions, 'downlevelIteration');
  105. if (!downlevelIteration) {
  106. json_utils_1.insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'downlevelIteration', true, 4);
  107. }
  108. else if (!downlevelIteration.value) {
  109. const { start, end } = downlevelIteration;
  110. recorder.remove(start.offset, end.offset - start.offset);
  111. recorder.insertLeft(start.offset, 'true');
  112. }
  113. const scriptTarget = json_utils_1.findPropertyInAstObject(compilerOptions, 'target');
  114. if (!scriptTarget) {
  115. json_utils_1.insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4);
  116. }
  117. else if (scriptTarget.value !== 'es2015') {
  118. const { start, end } = scriptTarget;
  119. recorder.remove(start.offset, end.offset - start.offset);
  120. recorder.insertLeft(start.offset, '"es2015"');
  121. }
  122. const scriptModule = json_utils_1.findPropertyInAstObject(compilerOptions, 'module');
  123. if (!scriptModule) {
  124. json_utils_1.insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'module', 'esnext', 4);
  125. }
  126. else if (scriptModule.value !== 'esnext') {
  127. const { start, end } = scriptModule;
  128. recorder.remove(start.offset, end.offset - start.offset);
  129. recorder.insertLeft(start.offset, '"esnext"');
  130. }
  131. }
  132. tree.commitUpdate(recorder);
  133. }