UNPKG

5.17 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.StylesheetProcessor = exports.CssUrl = void 0;
7const browserslist_1 = __importDefault(require("browserslist"));
8const fs_1 = require("fs");
9const path_1 = require("path");
10const piscina_1 = __importDefault(require("piscina"));
11const color_1 = require("../utils/color");
12const postcss_configuration_1 = require("./postcss-configuration");
13const maxWorkersVariable = process.env['NG_BUILD_MAX_WORKERS'];
14const maxThreads = typeof maxWorkersVariable === 'string' && maxWorkersVariable !== '' ? +maxWorkersVariable : 4;
15var CssUrl;
16(function (CssUrl) {
17 CssUrl["inline"] = "inline";
18 CssUrl["none"] = "none";
19})(CssUrl || (exports.CssUrl = CssUrl = {}));
20class StylesheetProcessor {
21 constructor(projectBasePath, basePath, cssUrl, includePaths, cacheDirectory) {
22 this.projectBasePath = projectBasePath;
23 this.basePath = basePath;
24 this.cssUrl = cssUrl;
25 this.includePaths = includePaths;
26 this.cacheDirectory = cacheDirectory;
27 // By default, browserslist defaults are too inclusive
28 // https://github.com/browserslist/browserslist/blob/83764ea81ffaa39111c204b02c371afa44a4ff07/index.js#L516-L522
29 // We change the default query to browsers that Angular support.
30 // https://angular.io/guide/browser-support
31 browserslist_1.default.defaults = [
32 'last 2 Chrome versions',
33 'last 1 Firefox version',
34 'last 2 Edge major versions',
35 'last 2 Safari major versions',
36 'last 2 iOS major versions',
37 'Firefox ESR',
38 ];
39 }
40 async process({ filePath, content }) {
41 this.createRenderWorker();
42 return this.renderWorker.run({ content, filePath });
43 }
44 /** Destory workers in pool. */
45 destroy() {
46 var _a;
47 void ((_a = this.renderWorker) === null || _a === void 0 ? void 0 : _a.destroy());
48 }
49 createRenderWorker() {
50 // Do not use async FS calls in here as otherwise a race will be created which causes multiple FS calls to be done.
51 if (this.renderWorker) {
52 return;
53 }
54 const styleIncludePaths = [...this.includePaths];
55 let prevDir = null;
56 let currentDir = this.basePath;
57 while (currentDir !== prevDir) {
58 const p = (0, path_1.join)(currentDir, 'node_modules');
59 if ((0, fs_1.existsSync)(p)) {
60 styleIncludePaths.push(p);
61 }
62 prevDir = currentDir;
63 currentDir = (0, path_1.dirname)(prevDir);
64 }
65 const browserslistData = (0, browserslist_1.default)(undefined, { path: this.basePath });
66 const searchDirs = (0, postcss_configuration_1.generateSearchDirectories)([this.projectBasePath]);
67 this.renderWorker = new piscina_1.default({
68 filename: require.resolve('./stylesheet-processor-worker'),
69 maxThreads,
70 recordTiming: false,
71 env: {
72 ...process.env,
73 FORCE_COLOR: '' + color_1.colors.enabled,
74 },
75 workerData: {
76 postcssConfiguration: (0, postcss_configuration_1.loadPostcssConfiguration)(searchDirs),
77 tailwindConfigPath: (0, postcss_configuration_1.findTailwindConfiguration)(searchDirs),
78 projectBasePath: this.projectBasePath,
79 browserslistData,
80 targets: transformSupportedBrowsersToTargets(browserslistData),
81 cacheDirectory: this.cacheDirectory,
82 cssUrl: this.cssUrl,
83 styleIncludePaths,
84 },
85 });
86 }
87}
88exports.StylesheetProcessor = StylesheetProcessor;
89function transformSupportedBrowsersToTargets(supportedBrowsers) {
90 const transformed = [];
91 // https://esbuild.github.io/api/#target
92 const esBuildSupportedBrowsers = new Set(['safari', 'firefox', 'edge', 'chrome', 'ios']);
93 for (const browser of supportedBrowsers) {
94 let [browserName, version] = browser.split(' ');
95 // browserslist uses the name `ios_saf` for iOS Safari whereas esbuild uses `ios`
96 if (browserName === 'ios_saf') {
97 browserName = 'ios';
98 }
99 // browserslist uses ranges `15.2-15.3` versions but only the lowest is required
100 // to perform minimum supported feature checks. esbuild also expects a single version.
101 [version] = version.split('-');
102 if (esBuildSupportedBrowsers.has(browserName)) {
103 if (browserName === 'safari' && version === 'tp') {
104 // esbuild only supports numeric versions so `TP` is converted to a high number (999) since
105 // a Technology Preview (TP) of Safari is assumed to support all currently known features.
106 version = '999';
107 }
108 transformed.push(browserName + version);
109 }
110 }
111 return transformed.length ? transformed : undefined;
112}
113//# sourceMappingURL=stylesheet-processor.js.map
\No newline at end of file