|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author sheo13666q @sheo13666q |
| 4 | +*/ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +/** @import { CssEnvironment } from "../css/syntax-parser" */ |
| 9 | +/** @import { RawSourceMap } from "webpack-sources" */ |
| 10 | + |
| 11 | +/** |
| 12 | + * A `minify` function for `minimizer-webpack-plugin`: prints one `.svg` or `.xml` |
| 13 | + * asset minified, reading it as XML 1.0 and minifying each SVG or XHTML stylesheet |
| 14 | + * it holds with the CSS minifier. |
| 15 | + * @param {{ [file: string]: string | Buffer }} input a single `{ filename: code }` entry; a `Buffer` is read as UTF-8 |
| 16 | + * @param {(RawSourceMap | undefined)=} sourceMap input source map (unused: the printer is token-coarse, so no map is produced) |
| 17 | + * @param {{ environment?: CssEnvironment, css?: { convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string } } }=} minimizerOptions `environment` and `css` (`optimization.minimizeOptions.css`, whole) reach the CSS minifier this runs over each stylesheet |
| 18 | + * @returns {Promise<{ code: string }>} the minified document |
| 19 | + */ |
| 20 | +const xmlMinify = async (input, sourceMap, minimizerOptions = {}) => { |
| 21 | + const webpack = /** @type {typeof import("../index")} */ ( |
| 22 | + // eslint-disable-next-line import/no-extraneous-dependencies -- webpack self-require, re-resolved inside the worker |
| 23 | + require(/** @type {string} */ ("webpack")) |
| 24 | + ); |
| 25 | + |
| 26 | + const [[, given]] = Object.entries(input); |
| 27 | + const code = typeof given === "string" ? given : given.toString("utf8"); |
| 28 | + const { environment, css = {} } = minimizerOptions; |
| 29 | + // The same renderer and options HTML hands an inline `<style>`, so a |
| 30 | + // stylesheet an SVG holds agrees with the `.css` asset for the same target. |
| 31 | + const renderEmbeddedSource = webpack.html.builtinEmbeddedRenderer({ |
| 32 | + environment, |
| 33 | + convertLengthUnits: css.convertLengthUnits, |
| 34 | + convertApproximateColors: css.convertApproximateColors, |
| 35 | + dropOverriddenDeclarations: css.dropOverriddenDeclarations, |
| 36 | + rewriteCustomProperties: css.rewriteCustomProperties, |
| 37 | + unusedSymbols: css.unusedSymbols, |
| 38 | + pseudoClasses: css.pseudoClasses, |
| 39 | + transforms: webpack.css.syntax.parser.pickTransforms(css) |
| 40 | + }); |
| 41 | + return { |
| 42 | + code: new webpack.html.syntax.SourceProcessor().process(code, { |
| 43 | + xml: true, |
| 44 | + mode: "minify", |
| 45 | + renderEmbeddedSource |
| 46 | + }).code |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +// Worker-safe (see the body's in-worker `require`), so it may share the pool. |
| 51 | +xmlMinify.supportsWorkerThreads = () => true; |
| 52 | + |
| 53 | +/** |
| 54 | + * The language this minifies, for a caller dispatching source with no filename. |
| 55 | + * @returns {string[]} the languages |
| 56 | + */ |
| 57 | +xmlMinify.getTypes = () => ["xml"]; |
| 58 | + |
| 59 | +/** |
| 60 | + * @param {string} name asset filename |
| 61 | + * @returns {boolean} true for SVG and XML assets |
| 62 | + */ |
| 63 | +xmlMinify.filter = (name) => /^[^?#]*\.(?:svg|xml)(?:[?#].*)?$/i.test(name); |
| 64 | + |
| 65 | +module.exports = xmlMinify; |
0 commit comments