|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { json } from '@angular-devkit/core'; |
| 9 | +import { Rule, Tree, chain, noop } from '@angular-devkit/schematics'; |
| 10 | +import * as ts from 'typescript'; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Remove the Reflect import from a polyfill file. |
| 15 | + * @param tree The tree to use. |
| 16 | + * @param path Path of the polyfill file found. |
| 17 | + * @private |
| 18 | + */ |
| 19 | +function _removeReflectFromPolyfills(tree: Tree, path: string) { |
| 20 | + const source = tree.read(path); |
| 21 | + if (!source) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + // Start the update of the file. |
| 26 | + const recorder = tree.beginUpdate(path); |
| 27 | + |
| 28 | + const sourceFile = ts.createSourceFile(path, source.toString(), ts.ScriptTarget.Latest); |
| 29 | + const imports = ( |
| 30 | + sourceFile.statements |
| 31 | + .filter(s => s.kind === ts.SyntaxKind.ImportDeclaration) as ts.ImportDeclaration[] |
| 32 | + ); |
| 33 | + |
| 34 | + for (const i of imports) { |
| 35 | + const module = i.moduleSpecifier.kind == ts.SyntaxKind.StringLiteral |
| 36 | + && (i.moduleSpecifier as ts.StringLiteral).text; |
| 37 | + |
| 38 | + switch (module) { |
| 39 | + case 'core-js/es7/reflect': |
| 40 | + recorder.remove(i.getStart(sourceFile), i.getWidth(sourceFile)); |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + tree.commitUpdate(recorder); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Update a project's target, maybe. Only if it's a builder supported and the options look right. |
| 50 | + * This is a rule factory so we return the new rule (or noop if we don't support doing the change). |
| 51 | + * @param root The root of the project source. |
| 52 | + * @param targetObject The target information. |
| 53 | + * @private |
| 54 | + */ |
| 55 | +function _updateProjectTarget(root: string, targetObject: json.JsonObject): Rule { |
| 56 | + // Make sure we're using the correct builder. |
| 57 | + if (targetObject.builder !== '@angular-devkit/build-angular:browser' |
| 58 | + || !json.isJsonObject(targetObject.options)) { |
| 59 | + return noop(); |
| 60 | + } |
| 61 | + const options = targetObject.options; |
| 62 | + if (typeof options.polyfills != 'string') { |
| 63 | + return noop(); |
| 64 | + } |
| 65 | + |
| 66 | + const polyfillsToUpdate = [`${root}/${options.polyfills}`]; |
| 67 | + const configurations = targetObject.configurations; |
| 68 | + if (json.isJsonObject(configurations)) { |
| 69 | + for (const configName of Object.keys(configurations)) { |
| 70 | + const config = configurations[configName]; |
| 71 | + |
| 72 | + // Just in case, only do non-AOT configurations. |
| 73 | + if (json.isJsonObject(config) |
| 74 | + && typeof config.polyfills == 'string' |
| 75 | + && config.aot !== true) { |
| 76 | + polyfillsToUpdate.push(`${root}/${config.polyfills}`); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return chain( |
| 82 | + polyfillsToUpdate.map(polyfillPath => { |
| 83 | + return (tree: Tree) => _removeReflectFromPolyfills(tree, polyfillPath); |
| 84 | + }), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Move the import reflect metadata polyfill from the polyfill file to the dev environment. This is |
| 90 | + * not guaranteed to work, but if it doesn't it will result in no changes made. |
| 91 | + */ |
| 92 | +export function polyfillMetadataRule(): Rule { |
| 93 | + return (tree) => { |
| 94 | + // Simple. Take the ast of polyfills (if it exists) and find the import metadata. Remove it. |
| 95 | + const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json'); |
| 96 | + const rules: Rule[] = []; |
| 97 | + |
| 98 | + if (!angularConfigContent) { |
| 99 | + // Is this even an angular project? |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const angularJson = json.parseJson(angularConfigContent.toString(), json.JsonParseMode.Loose); |
| 104 | + |
| 105 | + if (!json.isJsonObject(angularJson) || !json.isJsonObject(angularJson.projects)) { |
| 106 | + // If that field isn't there, no use... |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // For all projects, for all targets, read the polyfill field, and read the environment. |
| 111 | + for (const projectName of Object.keys(angularJson.projects)) { |
| 112 | + const project = angularJson.projects[projectName]; |
| 113 | + if (!json.isJsonObject(project)) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + if (typeof project.root != 'string') { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + const targets = project.targets || project.architect; |
| 121 | + if (!json.isJsonObject(targets)) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + for (const targetName of Object.keys(targets)) { |
| 126 | + const target = targets[targetName]; |
| 127 | + if (json.isJsonObject(target)) { |
| 128 | + rules.push(_updateProjectTarget(project.root, target)); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Remove null or undefined rules. |
| 134 | + return chain(rules); |
| 135 | + }; |
| 136 | +} |
0 commit comments