polyfill-metadata.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 schematics_1 = require("@angular-devkit/schematics");
  12. const ts = require("../../third_party/github.com/Microsoft/TypeScript/lib/typescript");
  13. function isJsonObject(value) {
  14. return value != null && typeof value === 'object' && !Array.isArray(value);
  15. }
  16. /**
  17. * Remove the Reflect import from a polyfill file.
  18. * @param tree The tree to use.
  19. * @param path Path of the polyfill file found.
  20. * @private
  21. */
  22. function _removeReflectFromPolyfills(tree, path) {
  23. const source = tree.read(path);
  24. if (!source) {
  25. return;
  26. }
  27. // Start the update of the file.
  28. const recorder = tree.beginUpdate(path);
  29. const sourceFile = ts.createSourceFile(path, source.toString(), ts.ScriptTarget.Latest);
  30. const imports = sourceFile.statements
  31. .filter(s => s.kind === ts.SyntaxKind.ImportDeclaration);
  32. for (const i of imports) {
  33. const module = ts.isStringLiteral(i.moduleSpecifier) && i.moduleSpecifier.text;
  34. switch (module) {
  35. case 'core-js/es7/reflect':
  36. recorder.remove(i.getFullStart(), i.getFullWidth());
  37. break;
  38. }
  39. }
  40. tree.commitUpdate(recorder);
  41. }
  42. /**
  43. * Update a project's target, maybe. Only if it's a builder supported and the options look right.
  44. * This is a rule factory so we return the new rule (or noop if we don't support doing the change).
  45. * @param root The root of the project source.
  46. * @param targetObject The target information.
  47. * @private
  48. */
  49. function _updateProjectTarget(targetObject) {
  50. // Make sure we're using the correct builder.
  51. if (targetObject.builder !== '@angular-devkit/build-angular:browser'
  52. || !isJsonObject(targetObject.options)) {
  53. return schematics_1.noop();
  54. }
  55. const options = targetObject.options;
  56. if (typeof options.polyfills != 'string') {
  57. return schematics_1.noop();
  58. }
  59. const polyfillsToUpdate = [options.polyfills];
  60. const configurations = targetObject.configurations;
  61. if (isJsonObject(configurations)) {
  62. for (const configName of Object.keys(configurations)) {
  63. const config = configurations[configName];
  64. // Just in case, only do non-AOT configurations.
  65. if (isJsonObject(config)
  66. && typeof config.polyfills == 'string'
  67. && config.aot !== true) {
  68. polyfillsToUpdate.push(config.polyfills);
  69. }
  70. }
  71. }
  72. return schematics_1.chain(polyfillsToUpdate.map(polyfillPath => {
  73. return (tree) => _removeReflectFromPolyfills(tree, polyfillPath);
  74. }));
  75. }
  76. /**
  77. * Move the import reflect metadata polyfill from the polyfill file to the dev environment. This is
  78. * not guaranteed to work, but if it doesn't it will result in no changes made.
  79. */
  80. function polyfillMetadataRule() {
  81. return (tree) => {
  82. // Simple. Take the ast of polyfills (if it exists) and find the import metadata. Remove it.
  83. const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');
  84. const rules = [];
  85. if (!angularConfigContent) {
  86. // Is this even an angular project?
  87. return;
  88. }
  89. const angularJson = core_1.parseJson(angularConfigContent.toString(), core_1.JsonParseMode.Loose);
  90. if (!isJsonObject(angularJson) || !isJsonObject(angularJson.projects)) {
  91. // If that field isn't there, no use...
  92. return;
  93. }
  94. // For all projects, for all targets, read the polyfill field, and read the environment.
  95. for (const projectName of Object.keys(angularJson.projects)) {
  96. const project = angularJson.projects[projectName];
  97. if (!isJsonObject(project)) {
  98. continue;
  99. }
  100. if (typeof project.root != 'string') {
  101. continue;
  102. }
  103. const targets = project.targets || project.architect;
  104. if (!isJsonObject(targets)) {
  105. continue;
  106. }
  107. for (const targetName of Object.keys(targets)) {
  108. const target = targets[targetName];
  109. if (isJsonObject(target)) {
  110. rules.push(_updateProjectTarget(target));
  111. }
  112. }
  113. }
  114. // Remove null or undefined rules.
  115. return schematics_1.chain(rules);
  116. };
  117. }
  118. exports.polyfillMetadataRule = polyfillMetadataRule;