Skip to content

Commit 19d3cc6

Browse files
alexander-akaitclaude
authored andcommitted
feat(css): add optimization.minimize.css.assumeUtf8
A .css asset served as UTF-8 needs no @charset "UTF-8" to be read so; the option states that, so the minimizer writes none and still writes an escaped character past U+FFFF as the character.
1 parent d5d84cd commit 19d3cc6

13 files changed

Lines changed: 119 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add `optimization.minimize.css.assumeUtf8`, writing no `@charset "UTF-8"` before a `.css` asset that is served as UTF-8.

‎declarations/WebpackOptions.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,11 @@ export interface Optimization {
31083108
* @since 5.110.0
31093109
*/
31103110
export interface OptimizationMinimizeCss {
3111+
/**
3112+
* State that each `.css` asset is read as UTF-8 wherever it is served, so the minimizer writes no `@charset "UTF-8"` before one holding a non-ASCII character, and writes an escaped character past U+FFFF (an icon font's `content: "\F0A86"`) as the character itself, as it does once that `@charset` is written. Off by default, because the statement must be true: a stylesheet with no `@charset` is read in the encoding the server's `Content-Type` names, else in the encoding of the page linking it, so it holds wherever the server names UTF-8 or the page is UTF-8 — and anywhere else every such character reads as other characters.
3113+
* @since 5.112.0
3114+
*/
3115+
assumeUtf8?: boolean;
31113116
/**
31123117
* Write a color the `browserslist` target cannot read as an extra declaration before the one naming it, in a spelling it does read: `color: oklch(59.686% 0.15619 49.7694)` is written as `color: #c65d06` and then the `oklch()` itself, so an engine reading neither the Lab family nor `hwb()` is left with a color rather than with nothing. The fallback is that color clipped into the sRGB gamut and rounded, which the declaration standing after it corrects wherever it is read. Nothing is written where the author already set the property earlier in the same block, where the color holds a substitution or a relative reference this cannot fold, or where the fallback would still name a function the target cannot read. On by default, and only in effect for a `browserslist` target — any other target names no browsers to answer for.
31133118
* @since 5.111.0

‎lib/css/cssMinify.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* re-resolves in the worker — via the installed package, or the dev self-link.
3636
* @param {{ [file: string]: string | Buffer }} input a single `{ filename: code }` entry; a `Buffer` is read as UTF-8
3737
* @param {object=} sourceMap the asset's input source map — the plugin chains it onto the map returned here, so it isn't read directly
38-
* @param {{ as?: "stylesheet" | "block-contents", environment?: CssEnvironment, convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, extractComments?: MinimizeExtractComments, mergeDistantRules?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string }, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer } & CssTransformOptions=} 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`, and the per-transform switches). `renderEmbeddedSource` minifies source this stylesheet embeds, and may be asynchronous: one parse serves both it and the output. Its `extractComments` decides which comments move into a separate file, over the plugin's
38+
* @param {{ as?: "stylesheet" | "block-contents", environment?: CssEnvironment, assumeUtf8?: boolean, convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, extractComments?: MinimizeExtractComments, mergeDistantRules?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string }, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer } & CssTransformOptions=} 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`, and the per-transform switches). `renderEmbeddedSource` minifies source this stylesheet embeds, and may be asynchronous: one parse serves both it and the output. Its `extractComments` decides which comments move into a separate file, over the plugin's
3939
* @param {ExtractCommentsOption=} extractComments the plugin's `extractComments`, read where `minimize.css.extractComments` is not set
4040
* @returns {Promise<{ code: string, map?: SourceMap, extractedComments?: string[], warnings?: (Error | string)[], errors?: (Error | string)[] }>} the minified CSS, its input->output source map (one anchor for the whole of a `block-contents` print, which is emitted as one piece), the comments taken out of it, and what a renderer reported over what this embeds
4141
*/
@@ -97,6 +97,7 @@ const cssMinify = async (
9797
const {
9898
as,
9999
environment,
100+
assumeUtf8,
100101
convertLengthUnits,
101102
convertApproximateColors,
102103
dropOverriddenDeclarations,
@@ -118,6 +119,7 @@ const cssMinify = async (
118119
// A file of its own reads `@charset`; the declarations of a `style=""` are
119120
// read in the document's encoding, and have no opening bytes to hold one.
120121
declareCharset: as !== "block-contents",
122+
assumeUtf8,
121123
convertLengthUnits,
122124
convertApproximateColors,
123125
dropOverriddenDeclarations,

‎lib/css/syntax-parser.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,7 @@ const _setupParse = (input, lc) => {
21532153
_S._mathFunctionDepth = 0;
21542154
_S._steppedFunctionDepth = 0;
21552155
_S._convertLengthUnits = false;
2156+
_S._assumeUtf8 = false;
21562157
_convertApproximateColors = false;
21572158
_unusedSymbols = null;
21582159
_unusedRules = null;
@@ -4411,6 +4412,7 @@ const TOP_LEVEL_CONSUMERS = {
44114412
* @property {string=} content the input's contents for the map's `sourcesContent`; only read while printing
44124413
* @property {CssEnvironment=} environment what the target can read (the CSS entries of `output.environment`), so a spelling it would not understand is never reached for; only read while printing, and an absent entry means the modern spelling is available
44134414
* @property {boolean=} declareCharset open the stylesheet with `@charset "UTF-8"` exactly when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served, and write a string's hex escape of a character past U+FFFF as the character, which costs fewer bytes once compressed; for a stylesheet that is a file of its own, as `@charset` is read only as one's opening bytes. Off by default, and only read while printing
4415+
* @property {boolean=} assumeUtf8 the stylesheet is read as UTF-8 wherever it is served, so no `@charset` is written for it and a string's hex escape of a character past U+FFFF is written as the character, as `declareCharset` writes it. Off by default, and only read while printing
44144416
* @property {boolean=} convertLengthUnits rewrite a length into a shorter unit it is exactly equal in (`16px` -> `1pc`); off by default because it earns nothing once the asset is compressed, and only read while printing. A time is always rewritten
44154417
* @property {boolean=} convertApproximateColors write a polar or Lab color as the nearest hex even where that hex only approximates it: a channel too near a `.5` boundary for two engines to round it alike, and a color outside the sRGB gamut, which hex can only clip. Off by default, and only read while printing; a space engines read through transfers of their own (`a98-rgb`, `prophoto-rgb`) is left alone either way
44164418
* @property {boolean=} mergeDistantRules give a rule the selectors of a later one printing the same block, and an at-rule the block of a later one stating the same condition, past the rules between them; off by default, since it reorders the cascade and is sound only where nothing between declares what the block being moved does for an element it can match — a condition between counting for what its own rules declare
@@ -7641,6 +7643,7 @@ const grammar = (input, visitors, writer, options) => {
76417643
_printing = writer !== undefined;
76427644
_S._convertLengthUnits =
76437645
writer !== undefined && writer.options.convertLengthUnits === true;
7646+
_S._assumeUtf8 = writer !== undefined && writer.options.assumeUtf8 === true;
76447647
_convertApproximateColors =
76457648
writer !== undefined && writer.options.convertApproximateColors === true;
76467649
const unused =
@@ -7823,6 +7826,7 @@ const grammar = (input, visitors, writer, options) => {
78237826
writer !== undefined &&
78247827
writer.options.mode === "minify" &&
78257828
writer.options.declareCharset === true &&
7829+
writer.options.assumeUtf8 !== true &&
78267830
as === "stylesheet"
78277831
) {
78287832
_S._charsetPiece = writer.emitRetractable(CHARSET_RULE);
@@ -7995,7 +7999,7 @@ const _dropSeparatorAfter = (cc) =>
79957999
* What the CSS printer may be told, on top of the `mode` every language has —
79968000
* the printing slice of `CssProcessOptions`, named once so nothing outside CSS
79978001
* has to enumerate it.
7998-
* @typedef {Pick<CssProcessOptions, "environment" | "declareCharset" | "convertLengthUnits" | "convertApproximateColors" | "dropOverriddenDeclarations" | "mergeDistantRules" | "rewriteCustomProperties" | "renderEmbeddedSource" | "deferEmbeddedSource" | "transforms" | "unusedSymbols" | "pseudoClasses">} CssPrintOptions
8002+
* @typedef {Pick<CssProcessOptions, "environment" | "declareCharset" | "assumeUtf8" | "convertLengthUnits" | "convertApproximateColors" | "dropOverriddenDeclarations" | "mergeDistantRules" | "rewriteCustomProperties" | "renderEmbeddedSource" | "deferEmbeddedSource" | "transforms" | "unusedSymbols" | "pseudoClasses">} CssPrintOptions
79998003
*/
80008004

80018005
/** @typedef {import("../util/SourceProcessor").PrintContext<CssPath, Node, CssPrintOptions>} PrintContext */
@@ -18544,6 +18548,8 @@ const _S = {
1854418548
// The `@charset` piece a minified stylesheet starts with, taken back where
1854518549
// its output holds no non-ASCII character (-1 for none).
1854618550
_charsetPiece: -1,
18551+
// Whether the output is read as UTF-8 without a `@charset` saying so.
18552+
_assumeUtf8: false,
1854718553
// The caller's renderer for source this stylesheet embeds, set per print.
1854818554
/** @type {EmbeddedSourceRenderer | undefined} */
1854918555
_renderEmbeddedSource: undefined,

‎lib/css/syntax-printer.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5187,7 +5187,8 @@ const printer = (path, writer) => {
51875187
if (unquoted !== null) return unquoted;
51885188
}
51895189
const written = _minifyString(raw);
5190-
return _S._charsetPiece === -1 || !_S._transforms.rewriteEscapes
5190+
return (_S._charsetPiece === -1 && !_S._assumeUtf8) ||
5191+
!_S._transforms.rewriteEscapes
51915192
? written
51925193
: _unescapeAstral(written);
51935194
}

‎schemas/WebpackOptions.check.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎schemas/WebpackOptions.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,6 +4021,11 @@
40214021
"type": "object",
40224022
"additionalProperties": false,
40234023
"properties": {
4024+
"assumeUtf8": {
4025+
"description": "State that each `.css` asset is read as UTF-8 wherever it is served, so the minimizer writes no `@charset \"UTF-8\"` before one holding a non-ASCII character, and writes an escaped character past U+FFFF (an icon font's `content: \"\\F0A86\"`) as the character itself, as it does once that `@charset` is written. Off by default, because the statement must be true: a stylesheet with no `@charset` is read in the encoding the server's `Content-Type` names, else in the encoding of the page linking it, so it holds wherever the server names UTF-8 or the page is UTF-8 — and anywhere else every such character reads as other characters.",
4026+
"type": "boolean",
4027+
"added": "5.112.0"
4028+
},
40244029
"colorFallbacks": {
40254030
"description": "Write a color the `browserslist` target cannot read as an extra declaration before the one naming it, in a spelling it does read: `color: oklch(59.686% 0.15619 49.7694)` is written as `color: #c65d06` and then the `oklch()` itself, so an engine reading neither the Lab family nor `hwb()` is left with a color rather than with nothing. The fallback is that color clipped into the sRGB gamut and rounded, which the declaration standing after it corrects wherever it is read. Nothing is written where the author already set the property earlier in the same block, where the color holds a substitution or a relative reference this cannot fold, or where the fallback would still name a function the target cannot read. On by default, and only in effect for a `browserslist` target — any other target names no browsers to answer for.",
40264031
"type": "boolean",

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11261,6 +11261,19 @@ Object {
1126111261
"multiple": false,
1126211262
"simpleType": "boolean",
1126311263
},
11264+
"optimization-minimize-css-assume-utf8": Object {
11265+
"configs": Array [
11266+
Object {
11267+
"description": "State that each \`.css\` asset is read as UTF-8 wherever it is served, so the minimizer writes no \`@charset \\"UTF-8\\"\` before one holding a non-ASCII character, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as it does once that \`@charset\` is written. Off by default, because the statement must be true: a stylesheet with no \`@charset\` is read in the encoding the server's \`Content-Type\` names, else in the encoding of the page linking it, so it holds wherever the server names UTF-8 or the page is UTF-8 — and anywhere else every such character reads as other characters.",
11268+
"multiple": false,
11269+
"path": "optimization.minimize.css.assumeUtf8",
11270+
"type": "boolean",
11271+
},
11272+
],
11273+
"description": "State that each \`.css\` asset is read as UTF-8 wherever it is served, so the minimizer writes no \`@charset \\"UTF-8\\"\` before one holding a non-ASCII character, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as it does once that \`@charset\` is written. Off by default, because the statement must be true: a stylesheet with no \`@charset\` is read in the encoding the server's \`Content-Type\` names, else in the encoding of the page linking it, so it holds wherever the server names UTF-8 or the page is UTF-8 — and anywhere else every such character reads as other characters.",
11274+
"multiple": false,
11275+
"simpleType": "boolean",
11276+
},
1126411277
"optimization-minimize-css-color-fallbacks": Object {
1126511278
"configs": Array [
1126611279
Object {
@@ -12041,6 +12054,19 @@ Object {
1204112054
"multiple": false,
1204212055
"simpleType": "boolean",
1204312056
},
12057+
"optimization-minimize-options-css-assume-utf8": Object {
12058+
"configs": Array [
12059+
Object {
12060+
"description": "State that each \`.css\` asset is read as UTF-8 wherever it is served, so the minimizer writes no \`@charset \\"UTF-8\\"\` before one holding a non-ASCII character, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as it does once that \`@charset\` is written. Off by default, because the statement must be true: a stylesheet with no \`@charset\` is read in the encoding the server's \`Content-Type\` names, else in the encoding of the page linking it, so it holds wherever the server names UTF-8 or the page is UTF-8 — and anywhere else every such character reads as other characters.",
12061+
"multiple": false,
12062+
"path": "optimization.minimizeOptions.css.assumeUtf8",
12063+
"type": "boolean",
12064+
},
12065+
],
12066+
"description": "State that each \`.css\` asset is read as UTF-8 wherever it is served, so the minimizer writes no \`@charset \\"UTF-8\\"\` before one holding a non-ASCII character, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as it does once that \`@charset\` is written. Off by default, because the statement must be true: a stylesheet with no \`@charset\` is read in the encoding the server's \`Content-Type\` names, else in the encoding of the page linking it, so it holds wherever the server names UTF-8 or the page is UTF-8 — and anywhere else every such character reads as other characters.",
12067+
"multiple": false,
12068+
"simpleType": "boolean",
12069+
},
1204412070
"optimization-minimize-options-css-color-fallbacks": Object {
1204512071
"configs": Array [
1204612072
Object {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./style.css";
2+
3+
it("should emit the stylesheet the option is read for", () => {
4+
// The emitted stylesheet is checked in test.config.js (afterExecute); this
5+
// keeps a runnable entry so the chunk and its `.css` are produced.
6+
expect(true).toBe(true);
7+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.icon:before { content: "\F0A86" }
2+
.accent:before { content: "é" }

0 commit comments

Comments
 (0)