UNPKG

ng-packagr

Version:

Compile and package Angular libraries in Angular Package Format (APF)

162 lines 6.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const autoprefixer_1 = __importDefault(require("autoprefixer")); const node_path_1 = require("node:path"); const node_url_1 = require("node:url"); const node_worker_threads_1 = require("node:worker_threads"); const postcss_1 = __importDefault(require("postcss")); const postcss_url_1 = __importDefault(require("postcss-url")); const esbuild_executor_1 = require("../esbuild/esbuild-executor"); const cache_1 = require("../utils/cache"); const log = __importStar(require("../utils/log")); const stylesheet_processor_1 = require("./stylesheet-processor"); const { tailwindConfigPath, projectBasePath, browserslistData, targets, cssUrl, styleIncludePaths } = node_worker_threads_1.workerData; let cacheDirectory = node_worker_threads_1.workerData.cacheDirectory; let postCssProcessor; let esbuild; const CACHE_KEY_VALUES = [...browserslistData, ...styleIncludePaths, cssUrl].join(':'); async function render({ content, filePath }) { let key; if (cacheDirectory && !content.includes('@import') && !content.includes('@use')) { // No transitive deps, we can cache more aggressively. key = await (0, cache_1.generateKey)(content, CACHE_KEY_VALUES); const result = await (0, cache_1.readCacheEntry)(cacheDirectory, key); if (result) { result.warnings.forEach(msg => log.warn(msg)); return result.css; } } // Render pre-processor language (sass, styl, less) const renderedCss = await renderCss(filePath, content); // We cannot cache CSS re-rendering phase, because a transitive dependency via (@import) can case different CSS output. // Example a change in a mixin or SCSS variable. if (!key) { key = await (0, cache_1.generateKey)(renderedCss, CACHE_KEY_VALUES); } if (cacheDirectory) { const cachedResult = await (0, cache_1.readCacheEntry)(cacheDirectory, key); if (cachedResult) { cachedResult.warnings.forEach(msg => log.warn(msg)); return cachedResult.css; } } // Render postcss (autoprefixing and friends) const result = await postCssProcessor.process(renderedCss, { from: filePath, to: filePath.replace((0, node_path_1.extname)(filePath), '.css'), }); const warnings = result.warnings().map(w => w.toString()); const { code, warnings: esBuildWarnings } = await esbuild.transform(result.css, { loader: 'css', minify: true, target: targets, sourcefile: filePath, }); if (esBuildWarnings.length > 0) { warnings.push(...(await esbuild.formatMessages(esBuildWarnings, { kind: 'warning' }))); } if (cacheDirectory) { await (0, cache_1.saveCacheEntry)(cacheDirectory, key, JSON.stringify({ css: code, warnings, })); } warnings.forEach(msg => log.warn(msg)); return code; } async function renderCss(filePath, css) { const ext = (0, node_path_1.extname)(filePath); switch (ext) { case '.sass': case '.scss': { return (await Promise.resolve().then(() => __importStar(require('sass')))).compileString(css, { url: (0, node_url_1.pathToFileURL)(filePath), syntax: '.sass' === ext ? 'indented' : 'scss', loadPaths: styleIncludePaths, }).css; } case '.less': { const { css: content } = await (await Promise.resolve().then(() => __importStar(require('less')))).default.render(css, { filename: filePath, javascriptEnabled: true, paths: styleIncludePaths, }); return content; } case '.css': default: return css; } } function getTailwindPlugin() { // Attempt to setup Tailwind CSS // Only load Tailwind CSS plugin if configuration file was found. // This acts as a guard to ensure the project actually wants to use Tailwind CSS. // The package may be unknowningly present due to a third-party transitive package dependency. if (tailwindConfigPath) { let tailwindPackagePath; try { tailwindPackagePath = require.resolve('tailwindcss', { paths: [projectBasePath] }); } catch { const relativeTailwindConfigPath = (0, node_path_1.relative)(projectBasePath, tailwindConfigPath); log.warn(`Tailwind CSS configuration file found (${relativeTailwindConfigPath})` + ` but the 'tailwindcss' package is not installed.` + ` To enable Tailwind CSS, please install the 'tailwindcss' package.`); } if (tailwindPackagePath) { return require(tailwindPackagePath)({ config: tailwindConfigPath }); } } } async function initialize() { const postCssPlugins = []; const tailwinds = getTailwindPlugin(); if (tailwinds) { postCssPlugins.push(tailwinds); cacheDirectory = undefined; } if (cssUrl !== stylesheet_processor_1.CssUrl.none) { postCssPlugins.push((0, postcss_url_1.default)({ url: cssUrl })); } postCssPlugins.push((0, autoprefixer_1.default)({ ignoreUnknownVersions: true, overrideBrowserslist: browserslistData, })); postCssProcessor = (0, postcss_1.default)(postCssPlugins); esbuild = new esbuild_executor_1.EsbuildExecutor(); // Return the render function for use return render; } /** * The default export will be the promise returned by the initialize function. * This is awaited by piscina prior to using the Worker. */ exports.default = initialize(); //# sourceMappingURL=stylesheet-processor-worker.js.map