Skip to content

Commit a65a583

Browse files
fix(css,html): keep a name folded only where the engine reads it back
The case fold reached two places it must not. A value the engine keeps as the tokens it was written as until a substitution resolves now prints its function, url and unit spellings as written, and an `@font-feature-values` sub-rule's declaration names stay case-sensitive: they are `<custom-ident>` feature values, so folding made two distinct entries collide. Also from review: `optimization.minimize.css: false` now reaches the CSS inside a document, `reduceFunctions` and `mergeLonghands` govern the last rewrite each was missing, a comment selector decides the source-map pragma like any other comment, and a parser-inserted table group prints its start tag whenever its end tag prints.
1 parent fde5dd4 commit a65a583

20 files changed

Lines changed: 259 additions & 55 deletions

File tree

‎.changeset/078-css-html-minify-options.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-
Print every CSS name that matches ASCII case-insensitively in one case — a property, at-rule, function, `url()`, pseudo, media feature, media type, unit, and a keyword value on a property whose grammar takes no name of the author's — keeping the canonical spelling of the eleven transform functions and three units the spec capitalizes; and name each of the CSS and HTML minimizers' rewrites on `optimization.minimize.css` / `.html` (`comments`, `mergeLonghands`, `mergeRules`, `normalizeQuotes`, `reduceFunctions`, `removeDeadRules`, `shortenColors`, `shortenMediaQueries`, `shortenNumbers`, `shortenSelectors`, `shortenValues`; `collapseBooleanAttributes`, `comments`, `minifyJson`, `minifyStyles`, `normalizeAttributeQuotes`, `normalizeEnumeratedAttributes`, `normalizeListAttributes`, `normalizeNumericAttributes`, `removeOptionalTags`), so one a document trips over can be turned off while the rest still apply — `comments` taking the six forms terser's `format.comments` does. The tables both minimizers read are smaller by 116 KiB of retained heap.
5+
Print CSS names in one case, and name each CSS/HTML minifier rewrite an option.

‎cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@
418418
"Stensberg",
419419
"stringable",
420420
"strtok",
421+
"styleset",
421422
"stylesheet",
422423
"subdir",
423424
"subfolder",

‎declarations/WebpackOptions.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,7 @@ export interface OptimizationMinimizeOptions {
24102410
*/
24112411
export interface OptimizationMinimizeCss {
24122412
/**
2413-
* Which comments survive. `"some"`, the default, keeps a `/*!` banner and a comment annotated `@license` or `@preserve`; `true` (or `"all"`) keeps every comment and `false` keeps none; a string is read as a regular expression source, and it, a `RegExp` or a `(comment) => boolean` predicate is asked about each comment's own text and keeps the ones it accepts — standing in for the default rule rather than beside it, as terser's `format.comments` does, so a pattern that names nothing else drops the ones `"some"` would have kept. A `/*#` source-map pragma is a link rather than a comment and stays whatever this says. A predicate is handed to the minimizer's worker pool as source, so it must not close over anything.
2413+
* Which comments survive. `"some"`, the default, keeps a `/*!` banner and a comment annotated `@license` or `@preserve`; `true` (or `"all"`) keeps every comment and `false` keeps none; a string is read as a regular expression source, and it, a `RegExp` or a `(comment) => boolean` predicate is asked about each comment's own text and keeps the ones it accepts — standing in for the default rule rather than beside it, as terser's `format.comments` does, so a pattern that names nothing else drops the ones `"some"` would have kept. A `/*#` source-map pragma is a link rather than a comment, so `"some"` and `"all"` keep it; `false`, a pattern or a predicate decides it like any other. A predicate is handed to the minimizer's worker pool as source, so it must not close over anything.
24142414
* @since 5.110.0
24152415
*/
24162416
comments?:

‎lib/config/defaults.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2565,10 +2565,16 @@ const applyOptimizationDefaults = (
25652565
// beside the HTML options: the two name some of the same switches
25662566
// (`quotes`, `comments`), and one flat object would hand
25672567
// each language the other's answer.
2568+
// An inline `<style>` and a `style=""` are CSS, so `minimize.css`
2569+
// turning the CSS minifier off has to reach them too.
2570+
const minifyStyles =
2571+
minimizeByType.css !== false &&
2572+
(htmlOptions === undefined || htmlOptions.minifyStyles !== false);
25682573
minimizerOptions.push({
25692574
environment: cssEnvironment,
25702575
css: cssOptions,
2571-
...htmlOptions
2576+
...htmlOptions,
2577+
minifyStyles
25722578
});
25732579
extensions.push("html");
25742580
}

‎lib/css/syntax.js‎

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,7 @@ const _setupParse = (input, lc) => {
19881988
_inMediaConditionPrelude = false;
19891989
_inPropertyRule = false;
19901990
_inFunctionRule = false;
1991+
_inFeatureValuesRule = false;
19911992
_inCustomProperty = false;
19921993
_inSubstitutedValue = false;
19931994
_inGradient = false;
@@ -4202,8 +4203,13 @@ const _gradientName = (i0) => _input.slice(_starts[i0], _aux0[i0]);
42024203
*/
42034204
const _enterAtRulePrelude = (i0) => {
42044205
const atName = _input.slice(_starts[i0] + 1, _aux0[i0]);
4205-
if (equalsLowerCase(atName, "property")) _inPropertyRule = true;
4206-
else if (equalsLowerCase(atName, "function")) _inFunctionRule = true;
4206+
if (equalsLowerCase(atName, "property")) {
4207+
_inPropertyRule = true;
4208+
} else if (equalsLowerCase(atName, "function")) {
4209+
_inFunctionRule = true;
4210+
} else if (equalsLowerCase(atName, "font-feature-values")) {
4211+
_inFeatureValuesRule = true;
4212+
}
42074213
if (equalsLowerCase(atName, "supports")) {
42084214
_inSupportsPrelude = true;
42094215
}
@@ -4253,6 +4259,7 @@ const _walkRule = (node, parent, index, writer) => {
42534259
const prevMedia = _inMediaConditionPrelude;
42544260
const prevProperty = _inPropertyRule;
42554261
const prevFunction = _inFunctionRule;
4262+
const prevFeatureValues = _inFeatureValuesRule;
42564263
if (ty === T_AT_RULE) _enterAtRulePrelude(i0);
42574264
for (let i = ps; i < pe; i++) {
42584265
_walkValue(_nodeRef(_flat[i]), node, i - ps, writer);
@@ -4273,6 +4280,7 @@ const _walkRule = (node, parent, index, writer) => {
42734280
}
42744281
_inPropertyRule = prevProperty;
42754282
_inFunctionRule = prevFunction;
4283+
_inFeatureValuesRule = prevFeatureValues;
42764284
} else if (ty === T_DECLARATION) {
42774285
const vs = _listStarts[i0];
42784286
const ve = vs + _listLens[i0];
@@ -4387,10 +4395,12 @@ const _frameDecls = Array.from({ length: _STREAM_MAX_DEPTH }, () => null);
43874395
/** @type {(Rule[] | null)[]} */
43884396
const _frameRules = Array.from({ length: _STREAM_MAX_DEPTH }, () => null);
43894397
const _frameParentIndex = new Int32Array(_STREAM_MAX_DEPTH);
4390-
// The `@property` / `@function` state this frame was entered under. A streamed
4391-
// block outlives the call that set it, so it is restored when the frame closes.
4398+
// The `@property` / `@function` / `@font-feature-values` state this frame was
4399+
// entered under. A streamed block outlives the call that set it, so it is
4400+
// restored when the frame closes.
43924401
const _framePrevProperty = new Uint8Array(_STREAM_MAX_DEPTH);
43934402
const _framePrevFunction = new Uint8Array(_STREAM_MAX_DEPTH);
4403+
const _framePrevFeatureValues = new Uint8Array(_STREAM_MAX_DEPTH);
43944404
// Printing a streamed block: where its first item landed in the output, how deep
43954405
// its opener sits in the writer's pending stack, and the direct declarations it
43964406
// has emitted so far — the last of a set of identical ones is the only one that
@@ -4828,6 +4838,7 @@ const _streamEnterRule = (rule, parent, index, d) => {
48284838
// so `_streamConsumeBlock` puts them back once its `}` is reached.
48294839
_framePrevProperty[d] = _inPropertyRule ? 1 : 0;
48304840
_framePrevFunction[d] = _inFunctionRule ? 1 : 0;
4841+
_framePrevFeatureValues[d] = _inFeatureValuesRule ? 1 : 0;
48314842
if (ty === T_AT_RULE) _enterAtRulePrelude(i0);
48324843
for (let i = ps; i < pe; i++) {
48334844
_walkValue(_nodeRef(_flat[i]), rule, i - ps, _streamWriter);
@@ -5012,6 +5023,7 @@ const _streamConsumeBlock = (ts, rule) => {
50125023
if (_streamWriter !== undefined) _streamClose(d, rule);
50135024
_inPropertyRule = _framePrevProperty[d] === 1;
50145025
_inFunctionRule = _framePrevFunction[d] === 1;
5026+
_inFeatureValuesRule = _framePrevFeatureValues[d] === 1;
50155027
_streamWalked = rule;
50165028
};
50175029

@@ -5471,10 +5483,10 @@ const _KEEP_COMMENT_RE = /@(?:license|preserve)/i;
54715483
*/
54725484
const _isKeptComment = (src, start, end) => {
54735485
const marker = src.charCodeAt(start + 2);
5474-
// A `/*#` pragma is a link to a source map rather than a comment, so no
5475-
// level drops it.
5476-
if (marker === CC_NUMBER_SIGN) return true;
54775486
const kept = _commentsKept;
5487+
// A `/*#` pragma is a link to a source map rather than a comment, so the
5488+
// banner level keeps it; a selector of the author's still decides it.
5489+
if (marker === CC_NUMBER_SIGN && kept === "some") return true;
54785490
if (typeof kept === "boolean") return kept;
54795491
if (kept === "some") {
54805492
return (
@@ -6282,7 +6294,9 @@ const _normalizeNumericToken = (text) => {
62826294
// it is an angle. `toLowerCase` hands back the string it was given where
62836295
// nothing folds, so a unit already lowercase is the same object.
62846296
const lowered = unit.toLowerCase();
6285-
if (lowered !== unit) {
6297+
// A substituted value is handed back as the tokens it was written as, so the
6298+
// spelling there is the author's; `lowered` still answers the angle question.
6299+
if (lowered !== unit && !_inSubstitutedValue) {
62866300
// Only a unit that carried a capital can have a canonical spelling to look
62876301
// up, so the table is read for the shouted units alone.
62886302
const folded = asciiLowerCaseName(unit);
@@ -10187,7 +10201,9 @@ const _collapseGridTemplateAreas = (path, node, writer) => {
1018710201
*/
1018810202
const _valueFragment = (fragment, property, minify) => {
1018910203
let out = fragment;
10190-
if (minify && !_inSupportsPrelude) out = _unwrapCalc(out, property);
10204+
if (minify && !_inSupportsPrelude && _transforms.reduceFunctions) {
10205+
out = _unwrapCalc(out, property);
10206+
}
1019110207
if (minify && !ZERO_UNIT_KEEPING_PROPERTIES.has(property)) {
1019210208
out = _dropZeroLengthUnit(out);
1019310209
out = _dropZeroLengthUnitInCall(out);
@@ -11619,8 +11635,13 @@ const printer = (path, writer) => {
1161911635
// with a capital are printed the way everything else writes them.
1162011636
const fn = asciiLowerCaseName(name);
1162111637
// As with a unit: a name already lowercase is its own answer, and only a
11622-
// shouted one is looked up.
11623-
const out = fn === name ? fn : CANONICAL_NAMES.get(fn) || fn;
11638+
// shouted one is looked up. The lookup key still folds inside a
11639+
// substituted value; only the printed spelling stays as written.
11640+
const out = _inSubstitutedValue
11641+
? name
11642+
: fn === name
11643+
? fn
11644+
: CANONICAL_NAMES.get(fn) || fn;
1162411645
// An+B in a selector: `2n+1` is what `odd` names, and shorter written so.
1162511646
if (!path.inValue() && NTH_PSEUDO_FUNCTIONS.has(fn)) {
1162611647
return _minifyAnPlusB(out, inner);
@@ -11748,13 +11769,16 @@ const printer = (path, writer) => {
1174811769
const open = raw.indexOf("(");
1174911770
// `url` matches ASCII case-insensitively like any other function name.
1175011771
// Read in place: cutting the name out to fold it would allocate on every
11751-
// url, and every url but a shouted one folds to itself.
11772+
// url, and every url but a shouted one folds to itself. Inside a
11773+
// substituted value the spelling is the author's, so nothing is read.
1175211774
let head = null;
11753-
for (let i = 0; i < open; i++) {
11754-
const c = raw.charCodeAt(i);
11755-
if (c >= CC_UPPER_A && c <= CC_UPPER_Z) {
11756-
head = asciiLowerCaseName(raw.slice(0, open + 1));
11757-
break;
11775+
if (!_inSubstitutedValue) {
11776+
for (let i = 0; i < open; i++) {
11777+
const c = raw.charCodeAt(i);
11778+
if (c >= CC_UPPER_A && c <= CC_UPPER_Z) {
11779+
head = asciiLowerCaseName(raw.slice(0, open + 1));
11780+
break;
11781+
}
1175811782
}
1175911783
}
1176011784
const percent = raw.includes("%");
@@ -12028,9 +12052,12 @@ const printer = (path, writer) => {
1202812052
}
1202912053
const important = path.important() ? `${soft}!important` : "";
1203012054
// Property names match ASCII case-insensitively; a custom property's
12031-
// does not, which is what `custom` already stands for.
12055+
// does not, which is what `custom` already stands for — nor does an
12056+
// `@font-feature-values` sub-rule's, which names a feature value.
1203212057
const printedName =
12033-
minify && !custom && lowered !== name ? asciiLowerCaseName(name) : name;
12058+
minify && !custom && !_inFeatureValuesRule && lowered !== name
12059+
? asciiLowerCaseName(name)
12060+
: name;
1203412061
return `${printedName}:${soft}${value}${important};`;
1203512062
}
1203612063
case T_AT_RULE:
@@ -12228,7 +12255,9 @@ const printer = (path, writer) => {
1222812255
}
1222912256
// After the supersedes: a declaration a later one already made dead is
1223012257
// no shorthand to fold into, and folding never makes one dead.
12231-
_foldFollowingLonghands(path, items, texts, superseded);
12258+
if (_transforms.mergeLonghands) {
12259+
_foldFollowingLonghands(path, items, texts, superseded);
12260+
}
1223212261
}
1223312262
// Vendor prefixes: drop a prefixed declaration no target needs (its
1223412263
// unprefixed sibling already covers them), and before an unprefixed one
@@ -12711,6 +12740,10 @@ let _inPropertyRule = false;
1271112740
// token stream the call substitutes — as opaque as a custom property's value,
1271212741
// and empty on purpose when the function returns the guaranteed-invalid one.
1271312742
let _inFunctionRule = false;
12743+
// Set by the walk while inside an `@font-feature-values` body, where each
12744+
// sub-rule's declaration names are `<custom-ident>` feature values — so they are
12745+
// case-sensitive, and two spellings of one name are two distinct entries.
12746+
let _inFeatureValuesRule = false;
1271412747
// Set by the walk while inside a custom property's value. Its tokens are
1271512748
// squeezed, but a rewrite that restates a value in another notation is held
1271612749
// back: `getPropertyValue()` hands this text back, so a color stays as written.

‎lib/html/syntax.js‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9499,6 +9499,24 @@ const _shellEndTagOmittable = (path, name, node) => {
94999499
const _canOmitShellEnd = (path, name, node) =>
95009500
_removeImpliedTags === "all" && _shellEndTagOmittable(path, name, node);
95019501

9502+
/**
9503+
* Whether a parser-inserted table group has to print a start tag the source
9504+
* never spelled. With `removeOptionalTags` off its end tag prints, and a
9505+
* `</tbody>` with no `<tbody>` is not the document that was parsed.
9506+
* @param {HtmlPath} path the accessor positioned on the element
9507+
* @param {string} name its lowercased tag name
9508+
* @param {HtmlNodeRef} node the element
9509+
* @returns {boolean} true when the start tag has to be materialized
9510+
*/
9511+
const _impliedTableGroupStartNeeded = (path, name, node) => {
9512+
if (!_transforms.removeOptionalTags) {
9513+
return name === "tbody" || name === "colgroup";
9514+
}
9515+
// §4.13 lets the start tag go only when the group before it kept its end
9516+
// tag, or that one would take these columns over.
9517+
return name === "colgroup" && !_canOmitColgroupStart(path, node);
9518+
};
9519+
95029520
/**
95039521
* Whether an element's start tag may be left out: one the parser re-implies, or
95049522
* a shell tag the option lets go. A shell element the source never spelled falls
@@ -10956,8 +10974,7 @@ const _streamTags = (path, minify) => {
1095610974
const shell = name === "body" || name === "html";
1095710975
const opensOutside = shell && _contentOpensOutside(path.node);
1095810976
_streamOpen =
10959-
opensOutside ||
10960-
(name === "colgroup" && !_canOmitColgroupStart(path, path.node))
10977+
opensOutside || _impliedTableGroupStartNeeded(path, name, path.node)
1096110978
? `<${name}>`
1096210979
: "";
1096310980
_streamImplied = shell && !opensOutside ? name : "";
@@ -11575,8 +11592,8 @@ const printer = (path, writer) => {
1157511592
}
1157611593
// §4.13 lets the start tag go only when the group before it kept
1157711594
// its end tag, or that one would take these columns over.
11578-
if (name === "colgroup" && !_canOmitColgroupStart(path, path.node)) {
11579-
return `<colgroup>${inner}`;
11595+
if (_impliedTableGroupStartNeeded(path, name, path.node)) {
11596+
return `<${name}>${inner}`;
1158011597
}
1158111598
// The parser re-implies the start tag, but not the end tag the
1158211599
// source carried: without it whatever follows moves inside.

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3724,7 +3724,7 @@
37243724
"additionalProperties": false,
37253725
"properties": {
37263726
"comments": {
3727-
"description": "Which comments survive. `\"some\"`, the default, keeps a `/*!` banner and a comment annotated `@license` or `@preserve`; `true` (or `\"all\"`) keeps every comment and `false` keeps none; a string is read as a regular expression source, and it, a `RegExp` or a `(comment) => boolean` predicate is asked about each comment's own text and keeps the ones it accepts — standing in for the default rule rather than beside it, as terser's `format.comments` does, so a pattern that names nothing else drops the ones `\"some\"` would have kept. A `/*#` source-map pragma is a link rather than a comment and stays whatever this says. A predicate is handed to the minimizer's worker pool as source, so it must not close over anything.",
3727+
"description": "Which comments survive. `\"some\"`, the default, keeps a `/*!` banner and a comment annotated `@license` or `@preserve`; `true` (or `\"all\"`) keeps every comment and `false` keeps none; a string is read as a regular expression source, and it, a `RegExp` or a `(comment) => boolean` predicate is asked about each comment's own text and keeps the ones it accepts — standing in for the default rule rather than beside it, as terser's `format.comments` does, so a pattern that names nothing else drops the ones `\"some\"` would have kept. A `/*#` source-map pragma is a link rather than a comment, so `\"some\"` and `\"all\"` keep it; `false`, a pattern or a predicate decides it like any other. A predicate is handed to the minimizer's worker pool as source, so it must not close over anything.",
37283728
"anyOf": [
37293729
{
37303730
"enum": ["all", "some"]

0 commit comments

Comments
 (0)