|
| 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 | + |
| 9 | +import { Path, getSystemPath, logging, normalize, resolve, workspaces } from '@angular-devkit/core'; |
| 10 | +import { Rule } from '@angular-devkit/schematics'; |
| 11 | +import { getWorkspace, updateWorkspace } from '../../utility/workspace'; |
| 12 | +import { Builders, ProjectType } from '../../utility/workspace-models'; |
| 13 | + |
| 14 | +export default function (): Rule { |
| 15 | + return async (host, context) => { |
| 16 | + const workspace = await getWorkspace(host); |
| 17 | + |
| 18 | + for (const [projectName, project] of workspace.projects) { |
| 19 | + if (project.extensions.projectType !== ProjectType.Application) { |
| 20 | + // Only interested in application projects |
| 21 | + continue; |
| 22 | + } |
| 23 | + |
| 24 | + for (const [, target] of project.targets) { |
| 25 | + // Only interested in Angular Devkit Browser builder |
| 26 | + if (target?.builder !== Builders.Browser) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + const isES5Needed = await isES5SupportNeeded( |
| 31 | + resolve( |
| 32 | + normalize(host.root.path), |
| 33 | + normalize(project.root), |
| 34 | + ), |
| 35 | + ); |
| 36 | + |
| 37 | + // Check options |
| 38 | + if (target.options) { |
| 39 | + target.options = removeE5BrowserSupportOption(projectName, target.options, isES5Needed, context.logger); |
| 40 | + } |
| 41 | + |
| 42 | + // Go through each configuration entry |
| 43 | + if (!target.configurations) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + for (const [configurationName, options] of Object.entries(target.configurations)) { |
| 48 | + target.configurations[configurationName] = removeE5BrowserSupportOption( |
| 49 | + projectName, |
| 50 | + options, |
| 51 | + isES5Needed, |
| 52 | + context.logger, |
| 53 | + configurationName, |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return updateWorkspace(workspace); |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +type TargetOptions = workspaces.TargetDefinition['options']; |
| 64 | + |
| 65 | +function removeE5BrowserSupportOption( |
| 66 | + projectName: string, |
| 67 | + options: TargetOptions, |
| 68 | + isES5Needed: boolean | undefined, |
| 69 | + logger: logging.LoggerApi, |
| 70 | + configurationName = '', |
| 71 | +): TargetOptions { |
| 72 | + if (typeof options?.es5BrowserSupport !== 'boolean') { |
| 73 | + return options; |
| 74 | + } |
| 75 | + |
| 76 | + const configurationPath = configurationName ? `configurations.${configurationName}.` : ''; |
| 77 | + |
| 78 | + if (options.es5BrowserSupport && isES5Needed === false) { |
| 79 | + logger.warn( |
| 80 | + `Project '${projectName}' doesn't require ES5 support, but '${configurationPath}es5BrowserSupport' was set to 'true'.\n` + |
| 81 | + `ES5 polyfills will no longer be added when building this project${configurationName ? ` with '${configurationName}' configuration.` : '.'}\n` + |
| 82 | + `If ES5 polyfills are needed, add the supported ES5 browsers in the browserslist configuration.`, |
| 83 | + ); |
| 84 | + } else if (!options.es5BrowserSupport && isES5Needed === true) { |
| 85 | + logger.warn( |
| 86 | + `Project '${projectName}' requires ES5 support, but '${configurationPath}es5BrowserSupport' was set to 'false'.\n` + |
| 87 | + `ES5 polyfills will be added when building this project${configurationName ? ` with '${configurationName}' configuration.` : '.'}\n` + |
| 88 | + `If ES5 polyfills are not needed, remove the unsupported ES5 browsers from the browserslist configuration.`, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + ...options, |
| 94 | + es5BrowserSupport: undefined, |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * True, when one or more browsers requires ES5 support |
| 100 | + */ |
| 101 | +async function isES5SupportNeeded(projectRoot: Path): Promise<boolean | undefined> { |
| 102 | + // y: feature is fully available |
| 103 | + // n: feature is unavailable |
| 104 | + // a: feature is partially supported |
| 105 | + // x: feature is prefixed |
| 106 | + const criteria = [ |
| 107 | + 'y', |
| 108 | + 'a', |
| 109 | + ]; |
| 110 | + |
| 111 | + try { |
| 112 | + // tslint:disable-next-line:no-implicit-dependencies |
| 113 | + const browserslist = await import('browserslist'); |
| 114 | + const supportedBrowsers = browserslist(undefined, { |
| 115 | + path: getSystemPath(projectRoot), |
| 116 | + }); |
| 117 | + |
| 118 | + // tslint:disable-next-line:no-implicit-dependencies |
| 119 | + const { feature, features } = await import('caniuse-lite'); |
| 120 | + const data = feature(features['es6-module']); |
| 121 | + |
| 122 | + return supportedBrowsers |
| 123 | + .some(browser => { |
| 124 | + const [agentId, version] = browser.split(' '); |
| 125 | + |
| 126 | + const browserData = data.stats[agentId]; |
| 127 | + const featureStatus = (browserData && browserData[version]) as string | undefined; |
| 128 | + |
| 129 | + // We are only interested in the first character |
| 130 | + // Ex: when 'a #4 #5', we only need to check for 'a' |
| 131 | + // as for such cases we should polyfill these features as needed |
| 132 | + return !featureStatus || !criteria.includes(featureStatus.charAt(0)); |
| 133 | + }); |
| 134 | + } catch { |
| 135 | + return undefined; |
| 136 | + } |
| 137 | +} |
0 commit comments