Skip to content

Commit f5fc884

Browse files
feat(css): replace assumeUtf8 with optimization.minimize.css.charset
`"auto"` (the default) writes `@charset "UTF-8"` only when the output holds a non-ASCII character; `false` writes none, as assumeUtf8: true did.
1 parent e441dde commit f5fc884

13 files changed

Lines changed: 49 additions & 41 deletions

File tree

‎.changeset/107-css-minify-assume-utf8.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
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.charset`; `false` drops `@charset` for UTF-8 CSS.

‎declarations/WebpackOptions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,10 +3109,10 @@ export interface Optimization {
31093109
*/
31103110
export interface OptimizationMinimizeCss {
31113111
/**
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.
3112+
* How the minimizer states a `.css` asset's encoding. `"auto"` (the default) opens it with `@charset "UTF-8"` only when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served. `false` writes no `@charset` at all, and writes an escaped character past U+FFFF (an icon font's `content: "\F0A86"`) as the character itself, as `"auto"` does once that `@charset` is written. Set `false` only where the asset is read as UTF-8 anyway: 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.
31133113
* @since 5.112.0
31143114
*/
3115-
assumeUtf8?: boolean;
3115+
charset?: "auto" | false;
31163116
/**
31173117
* 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.
31183118
* @since 5.111.0

‎lib/css/cssMinify.js‎

Lines changed: 3 additions & 3 deletions
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, 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
38+
* @param {{ as?: "stylesheet" | "block-contents", environment?: CssEnvironment, charset?: "auto" | false, 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,7 +97,7 @@ const cssMinify = async (
9797
const {
9898
as,
9999
environment,
100-
assumeUtf8,
100+
charset,
101101
convertLengthUnits,
102102
convertApproximateColors,
103103
dropOverriddenDeclarations,
@@ -119,7 +119,7 @@ const cssMinify = async (
119119
// A file of its own reads `@charset`; the declarations of a `style=""` are
120120
// read in the document's encoding, and have no opening bytes to hold one.
121121
declareCharset: as !== "block-contents",
122-
assumeUtf8,
122+
charset,
123123
convertLengthUnits,
124124
convertApproximateColors,
125125
dropOverriddenDeclarations,

‎lib/css/syntax-parser.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,7 +4412,7 @@ const TOP_LEVEL_CONSUMERS = {
44124412
* @property {string=} content the input's contents for the map's `sourcesContent`; only read while printing
44134413
* @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
44144414
* @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
4415+
* @property {("auto" | false)=} charset `false` when 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. `"auto"` by default, and only read while printing
44164416
* @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
44174417
* @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
44184418
* @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
@@ -7643,7 +7643,7 @@ const grammar = (input, visitors, writer, options) => {
76437643
_printing = writer !== undefined;
76447644
_S._convertLengthUnits =
76457645
writer !== undefined && writer.options.convertLengthUnits === true;
7646-
_S._assumeUtf8 = writer !== undefined && writer.options.assumeUtf8 === true;
7646+
_S._assumeUtf8 = writer !== undefined && writer.options.charset === false;
76477647
_convertApproximateColors =
76487648
writer !== undefined && writer.options.convertApproximateColors === true;
76497649
const unused =
@@ -7826,7 +7826,7 @@ const grammar = (input, visitors, writer, options) => {
78267826
writer !== undefined &&
78277827
writer.options.mode === "minify" &&
78287828
writer.options.declareCharset === true &&
7829-
writer.options.assumeUtf8 !== true &&
7829+
writer.options.charset !== false &&
78307830
as === "stylesheet"
78317831
) {
78327832
_S._charsetPiece = writer.emitRetractable(CHARSET_RULE);
@@ -7999,7 +7999,7 @@ const _dropSeparatorAfter = (cc) =>
79997999
* What the CSS printer may be told, on top of the `mode` every language has —
80008000
* the printing slice of `CssProcessOptions`, named once so nothing outside CSS
80018001
* has to enumerate it.
8002-
* @typedef {Pick<CssProcessOptions, "environment" | "declareCharset" | "assumeUtf8" | "convertLengthUnits" | "convertApproximateColors" | "dropOverriddenDeclarations" | "mergeDistantRules" | "rewriteCustomProperties" | "renderEmbeddedSource" | "deferEmbeddedSource" | "transforms" | "unusedSymbols" | "pseudoClasses">} CssPrintOptions
8002+
* @typedef {Pick<CssProcessOptions, "environment" | "declareCharset" | "charset" | "convertLengthUnits" | "convertApproximateColors" | "dropOverriddenDeclarations" | "mergeDistantRules" | "rewriteCustomProperties" | "renderEmbeddedSource" | "deferEmbeddedSource" | "transforms" | "unusedSymbols" | "pseudoClasses">} CssPrintOptions
80038003
*/
80048004

80058005
/** @typedef {import("../util/SourceProcessor").PrintContext<CssPath, Node, CssPrintOptions>} PrintContext */

‎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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,9 +4021,9 @@
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",
4024+
"charset": {
4025+
"description": "How the minimizer states a `.css` asset's encoding. `\"auto\"` (the default) opens it with `@charset \"UTF-8\"` only when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served. `false` writes no `@charset` at all, and writes an escaped character past U+FFFF (an icon font's `content: \"\\F0A86\"`) as the character itself, as `\"auto\"` does once that `@charset` is written. Set `false` only where the asset is read as UTF-8 anyway: 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+
"enum": ["auto", false],
40274027
"added": "5.112.0"
40284028
},
40294029
"colorFallbacks": {

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11261,18 +11261,22 @@ Object {
1126111261
"multiple": false,
1126211262
"simpleType": "boolean",
1126311263
},
11264-
"optimization-minimize-css-assume-utf8": Object {
11264+
"optimization-minimize-css-charset": Object {
1126511265
"configs": Array [
1126611266
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.",
11267+
"description": "How the minimizer states a \`.css\` asset's encoding. \`\\"auto\\"\` (the default) opens it with \`@charset \\"UTF-8\\"\` only when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served. \`false\` writes no \`@charset\` at all, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as \`\\"auto\\"\` does once that \`@charset\` is written. Set \`false\` only where the asset is read as UTF-8 anyway: 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.",
1126811268
"multiple": false,
11269-
"path": "optimization.minimize.css.assumeUtf8",
11270-
"type": "boolean",
11269+
"path": "optimization.minimize.css.charset",
11270+
"type": "enum",
11271+
"values": Array [
11272+
"auto",
11273+
false,
11274+
],
1127111275
},
1127211276
],
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.",
11277+
"description": "How the minimizer states a \`.css\` asset's encoding. \`\\"auto\\"\` (the default) opens it with \`@charset \\"UTF-8\\"\` only when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served. \`false\` writes no \`@charset\` at all, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as \`\\"auto\\"\` does once that \`@charset\` is written. Set \`false\` only where the asset is read as UTF-8 anyway: 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.",
1127411278
"multiple": false,
11275-
"simpleType": "boolean",
11279+
"simpleType": "string",
1127611280
},
1127711281
"optimization-minimize-css-color-fallbacks": Object {
1127811282
"configs": Array [
@@ -12054,18 +12058,22 @@ Object {
1205412058
"multiple": false,
1205512059
"simpleType": "boolean",
1205612060
},
12057-
"optimization-minimize-options-css-assume-utf8": Object {
12061+
"optimization-minimize-options-css-charset": Object {
1205812062
"configs": Array [
1205912063
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.",
12064+
"description": "How the minimizer states a \`.css\` asset's encoding. \`\\"auto\\"\` (the default) opens it with \`@charset \\"UTF-8\\"\` only when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served. \`false\` writes no \`@charset\` at all, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as \`\\"auto\\"\` does once that \`@charset\` is written. Set \`false\` only where the asset is read as UTF-8 anyway: 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.",
1206112065
"multiple": false,
12062-
"path": "optimization.minimizeOptions.css.assumeUtf8",
12063-
"type": "boolean",
12066+
"path": "optimization.minimizeOptions.css.charset",
12067+
"type": "enum",
12068+
"values": Array [
12069+
"auto",
12070+
false,
12071+
],
1206412072
},
1206512073
],
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.",
12074+
"description": "How the minimizer states a \`.css\` asset's encoding. \`\\"auto\\"\` (the default) opens it with \`@charset \\"UTF-8\\"\` only when its output holds a non-ASCII character, so it is read as UTF-8 wherever it is served. \`false\` writes no \`@charset\` at all, and writes an escaped character past U+FFFF (an icon font's \`content: \\"\\\\F0A86\\"\`) as the character itself, as \`\\"auto\\"\` does once that \`@charset\` is written. Set \`false\` only where the asset is read as UTF-8 anyway: 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.",
1206712075
"multiple": false,
12068-
"simpleType": "boolean",
12076+
"simpleType": "string",
1206912077
},
1207012078
"optimization-minimize-options-css-color-fallbacks": Object {
1207112079
"configs": Array [
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)