UNPKG

2.11 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.BuildBrowserFeatures = void 0;
11const browserslist = require("browserslist");
12const caniuse_lite_1 = require("caniuse-lite");
13const ts = require("typescript");
14class BuildBrowserFeatures {
15 constructor(projectRoot) {
16 this.projectRoot = projectRoot;
17 this.supportedBrowsers = browserslist(undefined, { path: this.projectRoot });
18 }
19 /**
20 * True, when one or more browsers requires ES5
21 * support and the script target is ES2015 or greater.
22 */
23 isDifferentialLoadingNeeded(scriptTarget) {
24 const es6TargetOrLater = scriptTarget > ts.ScriptTarget.ES5;
25 return es6TargetOrLater && this.isEs5SupportNeeded();
26 }
27 /**
28 * True, when one or more browsers requires ES5 support
29 */
30 isEs5SupportNeeded() {
31 return !this.isFeatureSupported('es6-module');
32 }
33 /**
34 * True, when a browser feature is supported partially or fully.
35 */
36 isFeatureSupported(featureId) {
37 // y: feature is fully available
38 // n: feature is unavailable
39 // a: feature is partially supported
40 // x: feature is prefixed
41 const criteria = ['y', 'a'];
42 const data = caniuse_lite_1.feature(caniuse_lite_1.features[featureId]);
43 return !this.supportedBrowsers.some((browser) => {
44 const [agentId, version] = browser.split(' ');
45 const browserData = data.stats[agentId];
46 const featureStatus = (browserData && browserData[version]);
47 // We are only interested in the first character
48 // Ex: when 'a #4 #5', we only need to check for 'a'
49 // as for such cases we should polyfill these features as needed
50 return !featureStatus || !criteria.includes(featureStatus.charAt(0));
51 });
52 }
53}
54exports.BuildBrowserFeatures = BuildBrowserFeatures;