Skip to content

Commit 339d931

Browse files
style: tighten comments to the two-line limit and use precise minimizer option types
1 parent 9d813be commit 339d931

6 files changed

Lines changed: 18 additions & 33 deletions

File tree

‎.changeset/062-optimization-minimize-by-type.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Configure or exclude each asset type's minifier via `optimization.minimize` (`css.convertLengthUnits` first).
5+
Configure or exclude each asset type's minifier via `optimization.minimize`.

‎lib/config/defaults.js‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,9 +2432,8 @@ const applyOptimizationDefaults = (
24322432
F(optimization, "minimize", () => (production ? {} : false));
24332433
const { minimize } = optimization;
24342434
if (minimize) {
2435-
// Fill each asset type, so the object form is canonical and the JS
2436-
// minimizer's defaults are visible in the resolved config rather than
2437-
// hidden in the plugin wiring below.
2435+
// Canonicalize the object form: the JS minimizer's defaults become
2436+
// visible in the resolved config rather than hidden in the wiring.
24382437
F(minimize, "javascript", () => ({ compress: { passes: 2 } }));
24392438
F(minimize, "css", () => ({}));
24402439
F(minimize, "html", () => ({}));
@@ -2462,10 +2461,8 @@ const applyOptimizationDefaults = (
24622461
compiler
24632462
);
24642463

2465-
// Normalization and the defaults above canonicalized
2466-
// `optimization.minimize` into the per-asset-type object whenever this
2467-
// plugin is applied at all: `false` disables a type, an object is that
2468-
// type's minifier options.
2464+
// Normalized + defaulted `optimization.minimize` is the per-asset-type
2465+
// object here: `false` disables a type, an object is its options.
24692466
const minimizeByType = /** @type {OptimizationMinimizeOptions} */ (
24702467
compiler.options.optimization.minimize
24712468
);
@@ -2508,9 +2505,8 @@ const applyOptimizationDefaults = (
25082505
cssInsetShorthand: outputEnvironment.cssInsetShorthand !== false,
25092506
cssMediaQueryRange: outputEnvironment.cssMediaQueryRange !== false
25102507
};
2511-
// `minimize.css` reaches the CSS minifier wherever it runs — the HTML
2512-
// entry gets the same options as the `.css` one, for the same reason
2513-
// the environment is shared.
2508+
// `minimize.css` follows the CSS minifier wherever it runs: the HTML
2509+
// entry gets the same options, like the shared environment above.
25142510
const cssOptions =
25152511
typeof minimizeByType.css === "object"
25162512
? minimizeByType.css

‎lib/css/cssMinify.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* re-resolves in the worker — via the installed package, or the dev self-link.
2323
* @param {{ [file: string]: string }} input a single `{ filename: code }` entry
2424
* @param {object=} sourceMap the asset's input source map — the plugin chains it onto the map returned here, so it isn't read directly
25-
* @param {object=} minimizerOptions minimizer options — `environment` carries the target's CSS abilities (see `output.environment`), so a spelling the target cannot read is not reached for; the rest is `optimization.minimize.css` (e.g. `convertLengthUnits`)
25+
* @param {{ environment?: CssEnvironment, convertLengthUnits?: boolean }=} minimizerOptions minimizer options — `environment` carries the target's CSS abilities (see `output.environment`), so a spelling the target cannot read is not reached for; the rest is `optimization.minimize.css` (e.g. `convertLengthUnits`)
2626
* @returns {{ code: string, map: SourceMap }} the minified CSS and its input->output source map
2727
*/
2828
const cssMinify = (input, sourceMap, minimizerOptions = {}) => {
@@ -32,9 +32,7 @@ const cssMinify = (input, sourceMap, minimizerOptions = {}) => {
3232
);
3333

3434
// TODO expose the remaining declined transforms on `optimization.minimize.css`
35-
// (the way `convertLengthUnits` already is), each off by default. Every one is
36-
// something csso, cssnano, lightningcss or esbuild ships on, and every one
37-
// costs something a build may not want to pay:
35+
// like `convertLengthUnits`. Each is on in other minifiers; what each costs:
3836
// - custom-property rewrites (`--r:0.5rem` -> `.5rem`), observable through
3937
// `getComputedStyle().getPropertyValue()`, the one place a declaration's
4038
// authored text survives;
@@ -73,10 +71,7 @@ const cssMinify = (input, sourceMap, minimizerOptions = {}) => {
7371
// safely minified serialization — no second parse — and always returns
7472
// `{ code, map }`. `source` / `content` name the map so the plugin can compose
7573
// it back to the original source.
76-
const { environment, convertLengthUnits } =
77-
/** @type {{ environment?: CssEnvironment, convertLengthUnits?: boolean }} */ (
78-
minimizerOptions
79-
);
74+
const { environment, convertLengthUnits } = minimizerOptions;
8075
return new SourceProcessor().process(code, {
8176
minimize: true,
8277
source: file,

‎lib/html/htmlMinify.js‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* re-resolves in the worker — via the installed package, or the dev self-link.
2424
* @param {{ [file: string]: string }} input a single `{ filename: code }` entry
2525
* @param {(RawSourceMap | undefined)=} sourceMap input source map (unused: safe serialize keeps positions token-coarse, so no map is produced yet)
26-
* @param {object=} minimizerOptions minimizer options — `environment` carries the target's CSS abilities and `convertLengthUnits` the CSS minimizer's option of that name, both for the inline CSS this minifies
26+
* @param {{ environment?: CssEnvironment, convertLengthUnits?: boolean }=} minimizerOptions minimizer options — `environment` carries the target's CSS abilities and `convertLengthUnits` the CSS minimizer's option of that name, both for the inline CSS this minifies
2727
* @returns {{ code: string }} the minified HTML
2828
*/
2929
const htmlMinify = (input, sourceMap, minimizerOptions = {}) => {
@@ -52,13 +52,9 @@ const htmlMinify = (input, sourceMap, minimizerOptions = {}) => {
5252
// too: any boolean value rather than only the canonical spellings.
5353
const { SourceProcessor } = webpack.html.syntax;
5454
const [[, code]] = Object.entries(input);
55-
// An inline `<style>` or `style=""` runs through the CSS minifier, so it needs
56-
// the same target abilities and options a `.css` asset gets — without them the
57-
// two would disagree on a hex alpha, `inset`, a media range or a length's unit.
58-
const { environment, convertLengthUnits } =
59-
/** @type {{ environment?: CssEnvironment, convertLengthUnits?: boolean }} */ (
60-
minimizerOptions
61-
);
55+
// The nested CSS minifier gets the abilities and options a `.css` asset
56+
// gets, or the inline copy of a declaration would disagree with it.
57+
const { environment, convertLengthUnits } = minimizerOptions;
6258
// `process` parses once, and with `minimize` its walk also prints the safely
6359
// minified serialization — no second parse. It returns `{ code, map }`, but the
6460
// HTML serialize is token-coarse so the map has no mappings; only the code is used.

‎lib/html/syntax.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8550,9 +8550,8 @@ const _canOmitColgroupStart = (path, node) => {
85508550
/** @type {CssEnvironment | undefined} */
85518551
let _cssEnvironment;
85528552

8553-
// Whether that nested CSS minifier may rewrite a length's unit — forwarded like
8554-
// `_cssEnvironment`, and for the same reason: the inline copy of a declaration
8555-
// must not disagree with the `.css` asset's.
8553+
// Whether the nested CSS minifier may rewrite a length's unit — forwarded like
8554+
// `_cssEnvironment` so inline CSS agrees with the `.css` assets.
85568555
let _cssConvertLengthUnits = false;
85578556

85588557
/**

‎test/configCases/html/minimize-css-options/webpack.config.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ module.exports = {
2626
convertLengthUnits: true
2727
}
2828
},
29-
// `"..."` keeps the default minimizer, which is what hands
30-
// `optimization.minimize.css` to `htmlMinify` — an inline `<style>` or
31-
// `style=""` must not disagree with a `.css` asset about a length's unit.
29+
// `"..."` keeps the default minimizer, which hands
30+
// `optimization.minimize.css` to `htmlMinify` for the inline CSS too.
3231
minimizer: ["..."]
3332
},
3433
experiments: {

0 commit comments

Comments
 (0)