Repository navigation
Commit 0cde758
authored
feat(css,html): fold every case-insensitive CSS name, and name each minifier rewrite as an option (#21834)
* feat(css): print every case-insensitive name in one case
CSS matches a property, at-keyword, function, `url()`, pseudo, media
feature, media type and unit ASCII case-insensitively, but the printer
wrote each back as authored — so one document could spell the same name
several ways and a compressor saw each as its own token.
Each is now printed lowercase, and the names whose canonical spelling
carries a capital keep it: `translateX` and the ten other transforms
`mdn-data`'s function table capitalizes, and `Hz` / `kHz` / `Q` from its
unit table, derived in the generator rather than listed.
What stays as authored: a type selector (`linearGradient` is its own SVG
element), an id, a class, an attribute name and its value, a custom
property, a `@keyframes` / `@container` / custom-media name, a name
carrying an escape (`\G` is not `\g`), and `@charset`, which the engine
reads as literal bytes rather than matching.
* feat(css): lowercase a keyword value where the property takes no name
271 properties spell their whole value out as keywords — `display`,
`position`, `float`, `overflow`, every `*-style`, `white-space` — and
CSS matches each ASCII case-insensitively, so an identifier standing
directly in one of their values is a keyword rather than a name of the
author's and can be printed the one way it matches.
The set is derived in the generator from the same grammar walk the other
property tables use: a property qualifies when its top-level value
grammar reaches no value class at all. A call's arguments are read
against the function's grammar, not the property's, so the printer
lowercases only an identifier that is a direct child of the declaration
— which is also what keeps `stylistic(Foo)` and `var(--Foo)` untouched.
The property lookup runs only once the identifier is known to carry a
capital, so a value already written lowercase costs one scan it was
paying for anyway.
* feat(css): name each minifier rewrite on optimization.minimize.css
Every CSS rewrite was unconditional, so a document one of them tripped
over could only be minified by turning minification off altogether.
Ten switches now name them — `colors`, `escapes`, `functions`,
`lowercase`, `mediaQueries`, `numbers`, `quotes`, `rules`, `selectors`,
`shorthands` — each on unless it is `false`, and each read off the same
`optimization.minimize.css` object `convertLengthUnits` already sits in.
The HTML minimizer hands them to the CSS minifier it runs over an inline
`<style>` and every `style=""`, so an inline declaration is rewritten by
the same switches its `.css` twin is.
Inside the printer they are one fixed-shape object rather than a flag
apiece, read once per print and asked at the head of the helper each
rewrite already lived in, so a switch left alone costs one property read
on the path that was going to run anyway.
A keyword value on a property that takes a color is lowercased with the
rest now: `<color>` is keywords and numbers, so `CanvasText` there is
the `canvastext` the CSSOM hands back.
* feat(html): name each minifier rewrite on optimization.minimize.html
The HTML side had the same gap the CSS side did: a page one rewrite
tripped over could only be minified by turning minification off. Ten
switches now name them — `booleanAttributes`, `comments`,
`enumeratedAttributes`, `listAttributes`, `minifyJson`, `minifyStyles`,
`numericAttributes`, `optionalTags`, `quotes`, `urlAttributes` — each on
unless it is `false`, which is what the value-rewrite TODO asked for.
`optionalTags` covers the tags §13.1.2.4 lets the parser imply other
than the `<html>` / `<head>` / `<body>` shell; `removeImpliedTags` keeps
that shell to itself, so the two do not overlap.
The CSS switches the HTML minimizer forwards to the nested CSS minifier
are now `cssTransforms`, leaving `transforms` for HTML's own — each
language picks its own out of the flat options object with its own
`pickTransforms`.
* perf(css): stop restating what the minifier's data tables can derive
Three things the generated tables said twice, and one that cost a
thousand objects to say at all:
- `PREFIX_BROWSERS` was `SUPPORT_BROWSERS` name for name. There is one
list of browsers the tables answer for, so a browser is now named by
its place in it — which is also the slot a parsed selection holds its
versions in, so no lookup goes through a name any more.
- `PREFIX_WINDOWS` held 710 three-element arrays inside 154 lists, each
repeating one of thirteen browser names; `SUPPORT_PROFILES` held 73
rows of thirteen. Both are now one `Float64Array` of numbers laid end
to end, the windows indexed by a `PREFIX_WINDOW_STARTS` companion.
- `Infinity` marked a version that never arrives, which prints as an
identifier and cost the emitted file six times its lines; a finite
`NEVER` past every real version does the same job as a number.
- `SVG_TAG_ADJUST` and `SVG_ATTR_ADJUST` keyed 95 camelCase names by the
same names lowercased. The generator states the names alone now and
the key is derived, so a key cannot drift from its value.
`lib/css/data.js` retains 747 KiB where it retained 863 KiB, measured
with `--expose-gc` over six collections; the file itself is 152 KiB
where it was 159 KiB, and `lib/html/data.js` 46 KiB where it was 47 KiB.
* refactor(css): say each of the printers' repeated blocks once
Five blocks the CSS and HTML printers stated twice, each now one
definition — same code, same call profile, none of it on a per-token
path:
- resolving and picking the per-transform switches was verbatim in both
languages, so it moves to `util/SourceProcessor`, which is where the
rest of what the two share already lives;
- `_releaseColumns` gives the node columns back, so a new column is
added in one place rather than in two that have to agree;
- `_enterAtRulePrelude` sets the flags an at-rule's name decides, which
the collected and streaming walks were spelling out separately;
- `_emitHeld` emits the top-level rule held back for a join, from the
four sites that each wrote its seven arguments out;
- `_fosterPlace` is §13.2.6.1's foster-parenting place, which the
adoption agency's copy carried a "reuse appropriatePlace logic but
rooted differently: emulate" note over.
Minifying `test/configCases/css/large/tailwind.module.css` takes the
same time either side — medians 233.8/228.5 ms before, 234.8/224.3 ms
after, against a 15 ms run-to-run spread — for byte-identical output,
and the 20330 html5lib tree-construction cases still pass.
* test: refresh the CLI flags the minifier switches add
Every schema property becomes a CLI flag, so the twenty switches
`optimization.minimize.css` and `.html` now name add twenty entries to
the snapshot the flags are read back from.
* feat(css): name the minimizer's comment handling, and part it from HTML's
Dropping comments was the one CSS rewrite no option reached: csso and
cssnano both name it, and turning it off meant turning minification off.
`optimization.minimize.css.comments` now names the three levels csso
does — `true` keeps a `/*!` banner, an `@license` / `@preserve`
annotation and the source-map pragma, `false` keeps every comment, and
`"all"` keeps nothing but the pragma — with `preserveComments` patterns
outranking all three, as the HTML minimizer's already do.
Two things this turned up:
- both languages name a `quotes` and a `preserveComments`, and the HTML
minimizer was handed one flat object holding both sets, so
`minimize.css.quotes` decided HTML's attribute quoting and
`minimize.html.preserveComments` decided what an inline `<style>`
kept. `optimization.minimize.css` now rides under its own key, and
each language picks its switches out of its own object;
- a quoted `url()` decoded its data-URI percent-escapes only when
`quotes` was on, though `escapes` is what documents that rewrite. The
two now answer for one thing each, so a data URI can shed its escapes
and keep its quotes.
Probing every transform `cssnano-preset-default` runs against the switch
set: each one webpack implements is now reachable by exactly one switch,
save collapsing whitespace, which is what minifying is.
* fix(css): let the transform switches and renderEmbeddedSource compose
Rebasing the switches onto #21830 put three of its lines inside a
`url()` that now asks two switches about its own spelling, and text that
merges cleanly is not the same as meaning that does:
- the renderer reads a data URL's payload, which is what the
percent-escapes hold, so it is offered the decoded one whatever
`escapes` says — that switch decides how the escapes are printed, not
whether a data URL holds a document;
- what the renderer hands back is no longer what the quotes were written
around, so `_serializeUrl` picks its spelling afresh — and the two
spellings below the quoted one are the unquoting `quotes` names, so
with it off the payload keeps its quotes;
- a rendered bare `url()` writes the lowercased `url(` the rest of them
do.
* feat(css,html): one comments option per language, in terser's six forms
`comments` and `preserveComments` were two options answering one
question, and neither answered it the way the JavaScript side already
does. They fold into `comments`, which takes what terser's
`format.comments` takes: `"some"` (the default) keeps the ones that
carry something, `true` / `"all"` keeps every comment, `false` keeps
none, and a string, a `RegExp` or a `(comment) => boolean` is asked
about each comment's own text — standing in for the default rule rather
than beside it, as terser's does, so a pattern that names nothing else
drops what `"some"` would have kept.
Each language keeps a floor no level reaches past, because the two mean
different things by "a comment that carries something": CSS keeps the
`/*#` source-map pragma, which is a link; HTML keeps a downlevel
conditional comment, a server-side include and a `<?…?>` directive,
which are code.
A predicate reaches the minimizer's worker pool through the same
serialize-and-evaluate path terser's does, so it works under parallel
minification and must not close over anything.
The seven changesets this branch had collapse into the one entry the
whole of it is.
* feat(css,html): name each minifier switch for what it does to what
`functions` said what a switch was about and nothing about what it did
with it, and it was not alone: `colors`, `escapes`, `numbers`, `quotes`,
`rules`, `selectors`, `shorthands` and HTML's `booleanAttributes`,
`optionalTags`, `quotes`, `urlAttributes` and the three `*Attributes`
read the same way. Every one is now verb-first, which is what
`cssnano-preset-default` (`discardComments`, `mergeRules`,
`minifySelectors`, `reduceTransforms`), html-minifier-terser
(`collapseBooleanAttributes`, `removeOptionalTags`,
`removeAttributeQuotes`) and webpack's own older HTML options
(`collapseWhitespace`, `removeEmptyAttributes`) already do:
colors -> shortenColors booleanAttributes -> collapseBooleanAttributes
escapes -> shortenEscapes optionalTags -> removeOptionalTags
functions -> reduceFunctions quotes -> normalizeAttributeQuotes
lowercase -> lowercaseNames urlAttributes -> trimUrlAttributes
mediaQueries -> shortenMediaQueries enumeratedAttributes -> normalizeEnumeratedAttributes
numbers -> shortenNumbers listAttributes -> normalizeListAttributes
quotes -> normalizeQuotes numericAttributes -> normalizeNumericAttributes
selectors -> shortenSelectors
Two CSS switches did two things each, which no single verb could say
honestly, so each splits the way cssnano splits it: `rules` into
`removeDeadRules` and `mergeRules`, `shorthands` into `shortenValues`
and `mergeLonghands`.
Probing every transform against the new names turned up one gate that
was missing rather than misnamed: writing an empty attribute value as
the bare name is the `=""` going, which is a quoting decision, so it
answers to `normalizeAttributeQuotes` like the rest of them.
* perf(css): stop folding a name twice, and read the tables once
Proving the re-encoded version tables cost nothing turned up what does.
Measured against a null test — the same build under both names, which
this machine calls ±0.6% — the branch was 5-9% slower than main on real
stylesheets, and every bit of it was the case folding walking a name a
second time:
- a value identifier was folded by `toLowerCaseIfNeeded` for the named
colour lookup and again by `asciiLowerCaseName` for the keyword one.
One fold now answers both, and the second walk is the shouted names'
to pay;
- a declaration's name was folded for the value rules and again to be
printed. Same fix;
- `asciiLowerCaseName` walked the characters to ask whether anything
folds at all. `toLowerCase` answers that natively and hands back the
very string it was given where nothing does, so it is a pointer
compare and no allocation;
- the canonical-spelling table was read for every number and every call,
where only a name that carried a capital can have an entry;
- a `url()` cut its own name out to fold it, on every url;
- `_inKeywordOnlyValue` read, folded and looked up the property once per
identifier rather than once per declaration.
And one that was neither: `_transforms` was the frozen table where no
switch was set and a copy where one was, and `Object.freeze` gives an
object a hidden class of its own — so every one of the ~45 reads, one
per token, saw two shapes. It is always a copy now, made the one way.
What is left, against the same null test: the branch is +1.3% on
tailwind and +0.5% on bootstrap with the folding off — where its output
is byte-identical to main's — and +2.3% to +6.6% with it on, which is
the feature, and `lowercaseNames: false` is the switch that returns it.
The re-encoded tables are themselves 1.4% *faster* on tailwind, and
`lib/css/data.js` retains 753 KiB where main's retains 842.
* refactor(css,html): drop the three switches nothing needs to turn off
`lowercaseNames`, `shortenEscapes` and `trimUrlAttributes` named rewrites
the spec makes safe by construction, and none of the three is a place a
bug could hide that the others do not already cover:
- an escape and the character it names are one token (CSS Syntax 3 §4.3.7),
so shortening one cannot change what parses. Both escape rewrites
predate this branch and had no switch before it;
- so does trimming a URL attribute: the URL parser strips the leading and
trailing whitespace before it resolves anything (URL §4.4);
- and every name CSS matches ASCII case-insensitively means the same
thing in either case, which is the whole premise of the folding.
They join the normalizations that were already unconditional for the
same reason — collapsing whitespace, the `<!doctype>` keyword, HTML tag
and attribute names, character references.
The one thing a switch was still buying on `lowercaseNames` was a way to
decline its cost, so that is gone first: the unit is folded once for both
the printed spelling and the angle test rather than twice, and folding a
selector's pseudo names now shares the pass that drops a CSS2
pseudo-element's second colon — the two read the same part after the same
`:`, and the fold is what the legacy name is then matched against.
Against a null test of ±1.1%, the branch is +2.0% on bootstrap, +2.1% on
bulma and +2.3% on tailwind, where it was +2.3% to +6.6% before.
* refactor(css,html): keep each printer's options in its own file
`util/SourceProcessor.js` is the visitor coordinator every language binds
its own grammar to, and it had no business knowing what a minifier
switch or a comment is. It grew three helpers because the two syntaxes
held identical copies and a de-duplication pass moved them to the nearest
shared file — which optimized for repeating no lines over each module
owning its own concern, and put language detail in the one file that is
meant to stay language-agnostic and is the one most likely to be
standardized or made public.
The file is byte-identical to `main` again. Each syntax now resolves its
own `transforms`, picks its own switches out of a wider options object
and reads its own `comments` level, over its own table — and the two are
free to part, which is already true: CSS's `"some"` keeps a `/*!` banner
and HTML's keeps a server-side directive.
The two copies are near-identical today and that is the point: the
duplication is between two files that describe different languages,
where the shared helper was a coupling between them.
What had to survive the move is the hidden-class discipline — a resolved
object is built by spreading the frozen table and writing over names it
already has, so every per-token read of `_transforms` sees one shape.
Both copies do, and it measures the same: byte-identical output either
side of the move, and against a null test of ±0.2% the branch is -0.0%
on bootstrap and +2.5% on tailwind, where it stood before it.
* 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.
* fix(config): leave minimize.css to the css assets, and refresh the cli snapshot
`minimize.css: false` selects which asset types are minimized, not whether
CSS is rewritten anywhere: `configCases/optimization/minimize-html-only`
states that a disabled type carries no options to forward, and its inline
css still minifies. `minimize.html.minifyStyles: false` is the switch for
that, which this branch is what adds.
The cli flags are derived from the schema, so the `comments` description
moves `Cli.basictest`'s snapshot with it.
* test(html): drive the second table group's kept start tag1 parent 0b5c4e9 commit 0cde758
53 files changed
Lines changed: 3656 additions & 2534 deletions
File tree
- .changeset
- declarations
- lib
- config
- css
- html
- schemas
- test
- __snapshots__
- configCases
- css
- minimize-colors/__snapshots__
- minimize-comments
- __snapshots__
- minimize-lightningcss-values/__snapshots__
- minimize-selectors
- __snapshots__
- minimize-shorthands
- __snapshots__
- minimize-transforms-off
- __snapshots__
- minimize-urls/__snapshots__
- minimize-values
- __snapshots__
- html
- minimize-comments-and-whitespace
- minimize-transforms-off
- __snapshots__
- tooling
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
379 | 380 | | |
380 | 381 | | |
381 | 382 | | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
382 | 386 | | |
383 | 387 | | |
384 | 388 | | |
| |||
414 | 418 | | |
415 | 419 | | |
416 | 420 | | |
| 421 | + | |
417 | 422 | | |
418 | 423 | | |
419 | 424 | | |
| |||
427 | 432 | | |
428 | 433 | | |
429 | 434 | | |
| 435 | + | |
430 | 436 | | |
431 | 437 | | |
432 | 438 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2405 | 2405 | | |
2406 | 2406 | | |
2407 | 2407 | | |
2408 | | - | |
| 2408 | + | |
2409 | 2409 | | |
2410 | 2410 | | |
2411 | 2411 | | |
| 2412 | + | |
| 2413 | + | |
| 2414 | + | |
| 2415 | + | |
| 2416 | + | |
| 2417 | + | |
| 2418 | + | |
| 2419 | + | |
| 2420 | + | |
| 2421 | + | |
2412 | 2422 | | |
2413 | 2423 | | |
2414 | 2424 | | |
2415 | 2425 | | |
2416 | 2426 | | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
2417 | 2452 | | |
2418 | 2453 | | |
2419 | 2454 | | |
2420 | 2455 | | |
2421 | 2456 | | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
| 2467 | + | |
| 2468 | + | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
2422 | 2482 | | |
2423 | 2483 | | |
2424 | 2484 | | |
2425 | 2485 | | |
2426 | 2486 | | |
2427 | 2487 | | |
2428 | 2488 | | |
2429 | | - | |
| 2489 | + | |
2430 | 2490 | | |
2431 | 2491 | | |
2432 | 2492 | | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
2433 | 2498 | | |
2434 | 2499 | | |
2435 | 2500 | | |
2436 | 2501 | | |
2437 | 2502 | | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
2438 | 2513 | | |
2439 | 2514 | | |
2440 | 2515 | | |
| |||
2445 | 2520 | | |
2446 | 2521 | | |
2447 | 2522 | | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
2448 | 2528 | | |
2449 | 2529 | | |
2450 | 2530 | | |
2451 | 2531 | | |
2452 | 2532 | | |
2453 | 2533 | | |
2454 | | - | |
| 2534 | + | |
2455 | 2535 | | |
2456 | 2536 | | |
2457 | | - | |
| 2537 | + | |
| 2538 | + | |
| 2539 | + | |
| 2540 | + | |
| 2541 | + | |
| 2542 | + | |
| 2543 | + | |
| 2544 | + | |
| 2545 | + | |
| 2546 | + | |
| 2547 | + | |
| 2548 | + | |
| 2549 | + | |
| 2550 | + | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | + | |
| 2556 | + | |
| 2557 | + | |
2458 | 2558 | | |
2459 | 2559 | | |
2460 | 2560 | | |
| |||
2470 | 2570 | | |
2471 | 2571 | | |
2472 | 2572 | | |
| 2573 | + | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
2473 | 2578 | | |
2474 | 2579 | | |
2475 | 2580 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2561 | 2561 | | |
2562 | 2562 | | |
2563 | 2563 | | |
| 2564 | + | |
| 2565 | + | |
| 2566 | + | |
| 2567 | + | |
2564 | 2568 | | |
2565 | 2569 | | |
2566 | | - | |
| 2570 | + | |
2567 | 2571 | | |
2568 | 2572 | | |
2569 | 2573 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
79 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
80 | 85 | | |
81 | 86 | | |
82 | 87 | | |
| |||
0 commit comments