|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author sheo13666q @sheo13666q |
| 4 | +*/ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +/** @typedef {import("../util/SourceProcessor").SourceMap} SourceMap */ |
| 9 | + |
| 10 | +/** |
| 11 | + * A `minify` function for `minimizer-webpack-plugin` (passed as its `minify` |
| 12 | + * option): safely serializes one CSS asset's minimized form (collapse |
| 13 | + * whitespace, drop redundant separators, keep strings / `url(...)` / |
| 14 | + * custom-property values verbatim), so CSS minification reuses that plugin's |
| 15 | + * pipeline — source maps, caching and worker-thread parallelization. |
| 16 | + * |
| 17 | + * The parser is read from the public `webpack.css.syntax` API inside the body, not |
| 18 | + * imported at module scope: `minimizer-webpack-plugin` ships this function to its |
| 19 | + * worker pool as source (a top-level import wouldn't survive), and `require("webpack")` |
| 20 | + * re-resolves in the worker — via the installed package, or the dev self-link. |
| 21 | + * @param {{ [file: string]: string }} input a single `{ filename: code }` entry |
| 22 | + * @param {object=} sourceMap the asset's input source map — the plugin chains it onto the map returned here, so it isn't read directly |
| 23 | + * @param {object=} minimizerOptions minimizer options (none used yet) |
| 24 | + * @returns {{ code: string, map: SourceMap }} the minified CSS and its input->output source map |
| 25 | + */ |
| 26 | +const cssMinify = (input, sourceMap, minimizerOptions = {}) => { |
| 27 | + // eslint-disable-next-line import/no-extraneous-dependencies -- webpack self-require, re-resolved inside the worker |
| 28 | + const webpack = require(/** @type {string} */ ("webpack")); |
| 29 | + |
| 30 | + const { SourceProcessor } = /** @type {typeof import("../index")} */ (webpack) |
| 31 | + .css.syntax; |
| 32 | + const [[file, code]] = Object.entries(input); |
| 33 | + // `process` parses once, and with `minimize` the same walk also prints the |
| 34 | + // safely minified serialization — no second parse — and always returns |
| 35 | + // `{ code, map }`. `source` / `content` name the map so the plugin can compose |
| 36 | + // it back to the original source. |
| 37 | + return new SourceProcessor().process(code, { |
| 38 | + minimize: true, |
| 39 | + source: file, |
| 40 | + content: code |
| 41 | + }); |
| 42 | +}; |
| 43 | + |
| 44 | +// Worker-safe (see the body's in-worker `require`), so it may run in the shared |
| 45 | +// worker-thread pool alongside terser. |
| 46 | +cssMinify.supportsWorkerThreads = () => true; |
| 47 | + |
| 48 | +// When several minify functions share one `minimizer-webpack-plugin` instance, |
| 49 | +// each asset is dispatched only to the ones whose `filter` accepts it — this |
| 50 | +// claims CSS, so terser (JS) and this can coexist in a single plugin / worker pool. |
| 51 | +/** |
| 52 | + * @param {string} name asset filename |
| 53 | + * @returns {boolean} true for CSS assets |
| 54 | + */ |
| 55 | +cssMinify.filter = (name) => /\.css(\?.*)?$/i.test(name); |
| 56 | + |
| 57 | +module.exports = cssMinify; |
0 commit comments