[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 29 | const core_1 = require("@angular-devkit/core");
|
---|
| 30 | const workspace_1 = require("../../utility/workspace");
|
---|
| 31 | const workspace_models_1 = require("../../utility/workspace-models");
|
---|
| 32 | function default_1() {
|
---|
| 33 | return async (host, context) => {
|
---|
| 34 | const workspace = await workspace_1.getWorkspace(host);
|
---|
| 35 | for (const [projectName, project] of workspace.projects) {
|
---|
| 36 | if (project.extensions.projectType !== workspace_models_1.ProjectType.Application) {
|
---|
| 37 | // Only interested in application projects
|
---|
| 38 | continue;
|
---|
| 39 | }
|
---|
| 40 | for (const [, target] of project.targets) {
|
---|
| 41 | // Only interested in Angular Devkit Browser builder
|
---|
| 42 | if ((target === null || target === void 0 ? void 0 : target.builder) !== workspace_models_1.Builders.Browser) {
|
---|
| 43 | continue;
|
---|
| 44 | }
|
---|
| 45 | const isES5Needed = await isES5SupportNeeded(core_1.resolve(core_1.normalize(host.root.path), core_1.normalize(project.root)));
|
---|
| 46 | // Check options
|
---|
| 47 | if (target.options) {
|
---|
| 48 | target.options = removeE5BrowserSupportOption(projectName, target.options, isES5Needed, context.logger);
|
---|
| 49 | }
|
---|
| 50 | // Go through each configuration entry
|
---|
| 51 | if (!target.configurations) {
|
---|
| 52 | continue;
|
---|
| 53 | }
|
---|
| 54 | for (const [configurationName, options] of Object.entries(target.configurations)) {
|
---|
| 55 | target.configurations[configurationName] = removeE5BrowserSupportOption(projectName, options, isES5Needed, context.logger, configurationName);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | return workspace_1.updateWorkspace(workspace);
|
---|
| 60 | };
|
---|
| 61 | }
|
---|
| 62 | exports.default = default_1;
|
---|
| 63 | function removeE5BrowserSupportOption(projectName, options, isES5Needed, logger, configurationName = '') {
|
---|
| 64 | if (typeof (options === null || options === void 0 ? void 0 : options.es5BrowserSupport) !== 'boolean') {
|
---|
| 65 | return options;
|
---|
| 66 | }
|
---|
| 67 | const configurationPath = configurationName ? `configurations.${configurationName}.` : '';
|
---|
| 68 | if (options.es5BrowserSupport && isES5Needed === false) {
|
---|
| 69 | logger.warn(`Project '${projectName}' doesn't require ES5 support, but '${configurationPath}es5BrowserSupport' was set to 'true'.\n` +
|
---|
| 70 | `ES5 polyfills will no longer be added when building this project${configurationName ? ` with '${configurationName}' configuration.` : '.'}\n` +
|
---|
| 71 | `If ES5 polyfills are needed, add the supported ES5 browsers in the browserslist configuration.`);
|
---|
| 72 | }
|
---|
| 73 | else if (!options.es5BrowserSupport && isES5Needed === true) {
|
---|
| 74 | logger.warn(`Project '${projectName}' requires ES5 support, but '${configurationPath}es5BrowserSupport' was set to 'false'.\n` +
|
---|
| 75 | `ES5 polyfills will be added when building this project${configurationName ? ` with '${configurationName}' configuration.` : '.'}\n` +
|
---|
| 76 | `If ES5 polyfills are not needed, remove the unsupported ES5 browsers from the browserslist configuration.`);
|
---|
| 77 | }
|
---|
| 78 | return {
|
---|
| 79 | ...options,
|
---|
| 80 | es5BrowserSupport: undefined,
|
---|
| 81 | };
|
---|
| 82 | }
|
---|
| 83 | /**
|
---|
| 84 | * True, when one or more browsers requires ES5 support
|
---|
| 85 | */
|
---|
| 86 | async function isES5SupportNeeded(projectRoot) {
|
---|
| 87 | // y: feature is fully available
|
---|
| 88 | // n: feature is unavailable
|
---|
| 89 | // a: feature is partially supported
|
---|
| 90 | // x: feature is prefixed
|
---|
| 91 | const criteria = ['y', 'a'];
|
---|
| 92 | try {
|
---|
| 93 | // eslint-disable-next-line import/no-extraneous-dependencies
|
---|
| 94 | const browserslist = (await Promise.resolve().then(() => __importStar(require('browserslist')))).default;
|
---|
| 95 | const supportedBrowsers = browserslist(undefined, {
|
---|
| 96 | path: core_1.getSystemPath(projectRoot),
|
---|
| 97 | });
|
---|
| 98 | // eslint-disable-next-line import/no-extraneous-dependencies
|
---|
| 99 | const { feature, features } = await Promise.resolve().then(() => __importStar(require('caniuse-lite')));
|
---|
| 100 | const data = feature(features['es6-module']);
|
---|
| 101 | return supportedBrowsers.some((browser) => {
|
---|
| 102 | const [agentId, version] = browser.split(' ');
|
---|
| 103 | const browserData = data.stats[agentId];
|
---|
| 104 | const featureStatus = (browserData && browserData[version]);
|
---|
| 105 | // We are only interested in the first character
|
---|
| 106 | // Ex: when 'a #4 #5', we only need to check for 'a'
|
---|
| 107 | // as for such cases we should polyfill these features as needed
|
---|
| 108 | return !featureStatus || !criteria.includes(featureStatus.charAt(0));
|
---|
| 109 | });
|
---|
| 110 | }
|
---|
| 111 | catch {
|
---|
| 112 | return undefined;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|