|
| 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 { |
| 9 | + JsonParseMode, |
| 10 | + isJsonObject, |
| 11 | + join, |
| 12 | + normalize, |
| 13 | + parseJson, |
| 14 | + parseJsonAst, |
| 15 | +} from '@angular-devkit/core'; |
| 16 | +import { Rule, Tree } from '@angular-devkit/schematics'; |
| 17 | +import { |
| 18 | + findPropertyInAstObject, |
| 19 | + insertPropertyInAstObjectInOrder, |
| 20 | +} from '../../utility/json-utils'; |
| 21 | + |
| 22 | +// tslint:disable-next-line:max-line-length |
| 23 | +const browserslistContent = `# This file is used by the build system to adjust CSS and JS output to support the specified browsers below. |
| 24 | +# For additional information regarding the format and rule options, please see: |
| 25 | +# https://github.com/browserslist/browserslist#queries |
| 26 | +
|
| 27 | +# Googlebot uses an older version of Chrome |
| 28 | +# For additional information see: https://developers.google.com/search/docs/guides/rendering |
| 29 | +
|
| 30 | +> 0.5% |
| 31 | +last 2 versions |
| 32 | +Firefox ESR |
| 33 | +not dead |
| 34 | +not IE 9-11 # For IE 9-11 support, remove 'not'. |
| 35 | +not Chrome 41 # For Googlebot support, remove 'not'.`; |
| 36 | + |
| 37 | +export function updateES5Projects(): Rule { |
| 38 | + return (host: Tree) => { |
| 39 | + const tsConfigPath = '/tsconfig.json'; |
| 40 | + const buffer = host.read(tsConfigPath); |
| 41 | + if (!buffer) { |
| 42 | + return host; |
| 43 | + } |
| 44 | + |
| 45 | + const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose); |
| 46 | + |
| 47 | + if (tsCfgAst.kind !== 'object') { |
| 48 | + return host; |
| 49 | + } |
| 50 | + |
| 51 | + const compilerOptions = findPropertyInAstObject(tsCfgAst, 'compilerOptions'); |
| 52 | + if (!compilerOptions || compilerOptions.kind !== 'object') { |
| 53 | + return host; |
| 54 | + } |
| 55 | + |
| 56 | + const scriptTarget = findPropertyInAstObject(compilerOptions, 'target'); |
| 57 | + if (scriptTarget && scriptTarget.value === 'es2015') { |
| 58 | + return host; |
| 59 | + } |
| 60 | + |
| 61 | + const recorder = host.beginUpdate(tsConfigPath); |
| 62 | + if (scriptTarget) { |
| 63 | + const { start, end } = scriptTarget; |
| 64 | + recorder.remove(start.offset, end.offset - start.offset); |
| 65 | + recorder.insertLeft(start.offset, '"es2015"'); |
| 66 | + } else { |
| 67 | + insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4); |
| 68 | + } |
| 69 | + host.commitUpdate(recorder); |
| 70 | + |
| 71 | + return updateBrowserlist; |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function updateBrowserlist(): Rule { |
| 76 | + return (tree) => { |
| 77 | + const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json'); |
| 78 | + |
| 79 | + if (!angularConfigContent) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const angularJson = parseJson(angularConfigContent.toString(), JsonParseMode.Loose); |
| 84 | + |
| 85 | + if (!isJsonObject(angularJson) || !isJsonObject(angularJson.projects)) { |
| 86 | + // If that field isn't there, no use... |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // For all projects |
| 91 | + for (const projectName of Object.keys(angularJson.projects)) { |
| 92 | + const project = angularJson.projects[projectName]; |
| 93 | + if (!isJsonObject(project)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + if (typeof project.root != 'string' || project.projectType !== 'application') { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + const browserslistPath = join(normalize(project.root), '/browserslist'); |
| 101 | + const source = tree.read(browserslistPath); |
| 102 | + if (!source) { |
| 103 | + tree.create(browserslistPath, browserslistContent); |
| 104 | + } else { |
| 105 | + const recorder = tree.beginUpdate(browserslistPath); |
| 106 | + recorder.insertRight(source.length, '\nChrome 41 # Googlebot'); |
| 107 | + tree.commitUpdate(recorder); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return tree; |
| 112 | + }; |
| 113 | +} |
0 commit comments