'use strict'; var postcss = require('postcss'); var cssnano = require('cssnano'); var inline = require('postcss-import'); var url = require('postcss-url'); var failOnWarn = require('postcss-fail-on-warn'); var esbuild = require('esbuild'); var promises = require('node:fs/promises'); var node_path = require('node:path'); var picomatch = require('picomatch'); const minifyOptions = { preset: ['default', { discardComments: { removeAll: true } }] }; function createMinifier(options = minifyOptions) { return postcss([cssnano(options), failOnWarn]) } const minifier = createMinifier(); const assetOptions = { url: 'inline' }; function createInliner({ stylesheets, assets = assetOptions } = {}) { return postcss([inline(stylesheets), url(assets), failOnWarn]) } const inliner = createInliner(); function createMinifyInliner({ minify = minifyOptions, inlineStylesheets, inlineAssets = assetOptions } = {}) { return postcss([ inline(inlineStylesheets), url(inlineAssets), cssnano(minify), failOnWarn ]) } const minifyInliner = createMinifyInliner(); function convertMap(map) { if (!map) return false return map === true || map.inline !== false ? 'inline' : 'external' } async function convertWarnings(warnings) { const messages = await esbuild.formatMessages(warnings, { kind: 'warning', color: true }); return warnings.map(({ text, location = {} }, i) => ({ message: messages[i], reason: text, location: { column: location.column, line: location.line } })) } async function convertTransform({ code: css, map, from, warnings }) { if (warnings.length > 0) { warnings = await convertWarnings(warnings); } if (map) css += `/*# sourceMappingURL=${from}.map */`; return { css, map, warnings } } function convertBundle({ outputFiles, from, warnings }) { let code; let map; for (const { path, text } of outputFiles) { if (path.endsWith('.map')) map = text; else code = text; } return convertTransform({ code, map, from, warnings }) } function createEsbuildMinifier({ map: defaultMap } = {}) { return { async process(contents, { from, map: inputMap = defaultMap } = {}) { const { code, map, warnings } = await esbuild.transform(contents, { loader: 'css', minify: true, legalComments: 'none', sourcefile: from, sourcemap: convertMap(inputMap) }); return convertTransform({ code, map, from, warnings }) } } } const esbuildMinifier = createEsbuildMinifier(); function createEsbuildInliner({ map: defaultMap, resolveDir: defaultDir } = {}) { return { async process(contents, { from, map: inputMap = defaultMap, resolveDir = defaultDir } = {}) { const { outputFiles, warnings } = await esbuild.build({ stdin: { contents, sourcefile: from, resolveDir: resolveDir || process.cwd(), loader: 'css' }, outdir: '.', bundle: true, write: false, sourcemap: convertMap(inputMap) }); return convertBundle({ outputFiles, from, warnings }) } } } const esbuildInliner = createEsbuildInliner(); function createEsbuildMinifyInliner({ map: defaultMap, resolveDir: defaultDir } = {}) { return { async process(contents, { from, map: inputMap = defaultMap, resolveDir = defaultDir } = {}) { const { outputFiles, warnings } = await esbuild.build({ stdin: { contents, sourcefile: from, resolveDir: resolveDir || process.cwd(), loader: 'css' }, outdir: '.', bundle: true, write: false, minify: true, legalComments: 'none', sourcemap: convertMap(inputMap) }); return convertBundle({ outputFiles, from, warnings }) } } } const esbuildMinifyInliner = createEsbuildMinifyInliner(); function createProcessor({ minify, inline, plugins } = {}) { if (plugins) return postcss(plugins) if (minify && inline) { if (minify === true && inline === true) return minifyInliner if (minify.fast || inline.fast) return esbuildMinifyInliner const { stylesheets, assets } = inline; return createMinifyInliner({ minify, inlineStylesheets: stylesheets, inlineAssets: assets }) }if (minify) { if (minify === true) return minifier if (minify.fast) return esbuildMinifier return createMinifier(minify) } if (inline) { if (inline === true) return inliner if (inline.fast) return esbuildInliner return createInliner(inline) } throw new Error('Neither minify nor inline nor plugins were set') } function matches(filename, filter) { if (typeof filter === 'function') return filter(filename) return !filter || picomatch.isMatch(filename, filter) } function createTransform({ minify, inline, plugins, map: defaultMap, filter: defaultFilter } = {}) { const processor = createProcessor({ minify, inline, plugins }); return async (contents, filename, { map = defaultMap, filter = defaultFilter } = {}) => { if (!matches(filename, filter)) return contents if (map === true) { map = { inline: true }; } if (map && map.inline === undefined) { map.inline = true; } let { css, map: outputMap, warnings } = await processor.process(contents, { from: filename, map }); if (warnings?.length) { for (const { message } of warnings) console.warn(message); throw new Error('Transformation failed') } if (outputMap) { const { dir, pathTransform } = map; if (dir) { filename = node_path.join(dir, node_path.basename(filename)); } const mapPath =`${filename}.map`; if (pathTransform) { outputMap = typeof outputMap !== 'string' ? outputMap.toJSON() : JSON.parse(outputMap); const { sources } = outputMap; for (let i = 0, l = sources.length; i < l; ++i) { sources[i] = pathTransform(sources[i], mapPath); } } if (typeof outputMap !== 'string') outputMap = JSON.stringify(outputMap); await promises.writeFile(mapPath, outputMap); } return css } } exports.createInliner = createInliner; exports.createMinifier = createMinifier; exports.createMinifyInliner = createMinifyInliner; exports.createProcessor = createProcessor; exports.createTransform = createTransform; exports.inliner = inliner; exports.minifier = minifier; exports.minifyInliner = minifyInliner; //# sourceMappingURL=index.cjs.map