Skip to content

Commit c068a73

Browse files
fix: minify data: URL payloads in HTML attributes and new URL
Offer a URL attribute's data: payload to the HTML printer's renderer, regenerate modules that copy a rendered asset url into an analyzable new URL, write a rendered text payload back without percent-encoding all of it, decline payloads a raw # cuts short, and dispatch .webmanifest assets to the JSON minifier.
1 parent 55d6530 commit c068a73

29 files changed

Lines changed: 558 additions & 37 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Minify data: URL payloads in HTML attributes and `new URL`, and web manifests.

‎lib/Compilation.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4161,7 +4161,24 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
41614161
(err) => {
41624162
if (err) return callback(/** @type {WebpackError} */ (err));
41634163
const affected = new Set(pending.map(({ jobModule }) => jobModule));
4164-
const redo = jobs.filter(({ module }) => affected.has(module));
4164+
// A module referencing one of these may have copied what it generated
4165+
// into its own code (an analyzable `new URL`), so it goes again after.
4166+
/** @type {Set<Module>} */
4167+
const referencing = new Set();
4168+
for (const { info } of pending) {
4169+
for (const connection of this.moduleGraph.getIncomingConnections(
4170+
info.module
4171+
)) {
4172+
const { originModule } = connection;
4173+
if (originModule !== null && !affected.has(originModule)) {
4174+
referencing.add(originModule);
4175+
}
4176+
}
4177+
}
4178+
const redo = [
4179+
...jobs.filter(({ module }) => affected.has(module)),
4180+
...jobs.filter(({ module }) => referencing.has(module))
4181+
];
41654182
// Generated directly rather than through the codegen cache: the module
41664183
// hash is unchanged, so the cache would hand back the first pass again.
41674184
const results =

‎lib/asset/AssetGenerator.js‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const RuntimeGlobals = require("../runtime/RuntimeGlobals");
2727
const { toTemplateSourceFileName } = require("../template/TemplatedPathPlugin");
2828
const { getScheme } = require("../util/URLAbsoluteSpecifier");
2929
const createHash = require("../util/createHash");
30-
const { languageOfMediaType } = require("../util/dataURL");
30+
const {
31+
encodeDataURIPayload,
32+
languageOfMediaType
33+
} = require("../util/dataURL");
3134
const { makePathsRelative, parseResource } = require("../util/identifier");
3235
const memoize = require("../util/memoize");
3336
const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
@@ -635,6 +638,17 @@ class AssetGenerator extends Generator {
635638
encodedContent =
636639
/** @type {string} */
637640
(module.resourceResolveData.encodedContent);
641+
} else if (
642+
renderedSource !== undefined &&
643+
encoding === false &&
644+
module.resourceResolveData &&
645+
module.resourceResolveData.encoding === false
646+
) {
647+
// A rendered payload of a `data:` URI written as text is written back
648+
// as text: percent-encoding all of it outgrows what minifying saved.
649+
encodedContent = encodeDataURIPayload(
650+
/** @type {string} */ (source.source())
651+
);
638652
} else {
639653
encodedContent = encodeDataUri(
640654
/** @type {"base64" | false} */ (encoding),
@@ -676,13 +690,12 @@ class AssetGenerator extends Generator {
676690
(module.buildInfo).dataUrl &&
677691
needContent
678692
) {
679-
// A payload bound for CSS or HTML is offered by their serializers, which
680-
// see the `url()` it ends up in; only the JavaScript one is ours.
693+
// WHY: CSS and HTML serializers offer the payload in the `url()` or
694+
// attribute it lands in, but the `asset-url` value is also what an
695+
// analyzable `new URL` copies into JavaScript, which nothing re-reads.
681696
const encodedSource = this.generateDataUri(
682697
module,
683-
type === JAVASCRIPT_TYPE
684-
? this._renderEmbedded(module, runtimeTemplate)
685-
: undefined
698+
this._renderEmbedded(module, runtimeTemplate)
686699
);
687700
content =
688701
type === JAVASCRIPT_TYPE

‎lib/config/defaults.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,8 @@ const applyOptimizationDefaults = (
26372637
if (minifyJson) {
26382638
minify.push(require("../json/jsonMinify"));
26392639
minimizerOptions.push({});
2640-
extensions.push("json");
2640+
// A web app manifest is JSON under its own extension.
2641+
extensions.push("json", "webmanifest");
26412642
}
26422643
new TerserPlugin({
26432644
test: assetNameTest(extensions),

‎lib/css/syntax-printer.js‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ const { CSS_TYPE } = require("../module/ModuleSourceTypeConstants");
99

1010
const GenericSourceProcessor = require("../util/SourceProcessor");
1111

12-
const {
13-
buildDataURI,
14-
decodeDataURIPayload,
15-
languageOfMediaType,
16-
parseDataURI
17-
} = require("../util/dataURL");
12+
const { buildDataURI, readEmbeddedDataURI } = require("../util/dataURL");
1813

1914
/**
2015
* Renders source this stylesheet embeds — a `data:` URL's payload today.
@@ -4797,13 +4792,7 @@ const _readDataUrl = (url) => {
47974792
// A CSS escape is still written here — the quoted form is percent-decoded,
47984793
// not unescaped — so the payload would be read with its backslashes in it.
47994794
if (url.includes("\\")) return null;
4800-
const parsed = parseDataURI(url);
4801-
if (parsed === null) return null;
4802-
const type = languageOfMediaType(parsed.mediaType);
4803-
if (type === undefined) return null;
4804-
const payload = decodeDataURIPayload(parsed);
4805-
if (payload === null || payload === "") return null;
4806-
return { parsed, type, payload };
4795+
return readEmbeddedDataURI(url);
48074796
};
48084797

48094798
/**

‎lib/html/htmlMinify.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ const htmlMinify = async (input, sourceMap, minimizerOptions = {}) => {
131131
*/
132132
const printDocument = async (text) => {
133133
// The built-in renderer is synchronous, so with no caller's renderer only
134-
// a nested document needs the async pass — and no `srcdoc`, none is there.
135-
if (renderEmbeddedSource === undefined && !/srcdoc/i.test(text)) {
134+
// a nested document needs the async pass — an `<iframe srcdoc>` or a
135+
// `data:` URL naming HTML; with neither spelled, none is there.
136+
if (
137+
renderEmbeddedSource === undefined &&
138+
!/srcdoc|data:[^,]*html/i.test(text)
139+
) {
136140
return new SourceProcessor().process(text, {
137141
...printOptions,
138142
renderEmbeddedSource: builtin

‎lib/html/syntax-parser.js‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
} = require("../module/ModuleSourceTypeConstants");
1313

1414
const GenericSourceProcessor = require("../util/SourceProcessor");
15+
const { buildDataURI, readEmbeddedDataURI } = require("../util/dataURL");
1516

1617
const {
1718
ADDRESS_DIV_P,
@@ -10098,6 +10099,26 @@ const _isEventHandlerAttribute = (tagName, name) => {
1009810099
return elements === null || elements.has(tagName);
1009910100
};
1010010101

10102+
/**
10103+
* The `data:` URL a URL attribute holds, read for the renderer, or null where
10104+
* the attribute is no URL on this element or the URL offers nothing.
10105+
* @param {string} tagName lowercased element name
10106+
* @param {string} name lowercased attribute name
10107+
* @param {string} value the attribute's decoded value
10108+
* @returns {ReturnType<typeof readEmbeddedDataURI>} the payload and its language, or null
10109+
*/
10110+
const _attributeDataUrl = (tagName, name, value) => {
10111+
const elements = URL_ATTRIBUTES.get(name);
10112+
if (elements === undefined || (elements !== null && !elements.has(tagName))) {
10113+
return null;
10114+
}
10115+
const url = _asciiTrim(value);
10116+
// The URL parser drops every tab and newline, so a payload holding one is
10117+
// not the text the browser reads.
10118+
if ((url.charCodeAt(0) | 0x20) !== 0x64 || /[\t\n\r]/.test(url)) return null;
10119+
return readEmbeddedDataURI(url);
10120+
};
10121+
1010110122
// One list's token spans, reused: ordering a list allocates only its result.
1010210123
/** @type {number[]} */
1010310124
const _tokenSpanStarts = [];
@@ -11122,6 +11143,78 @@ const _minifyOpenTag = (path, open) => {
1112211143
continue;
1112311144
}
1112411145

11146+
// A URL attribute's `data:` payload, offered decoded as its media type's
11147+
// language and written back only where the rebuilt URL spells shorter.
11148+
const dataUrl =
11149+
html &&
11150+
!isStyle &&
11151+
!verbatim &&
11152+
(_S._renderEmbeddedSource !== undefined ||
11153+
_S._deferEmbeddedSource !== undefined)
11154+
? _attributeDataUrl(
11155+
elementName,
11156+
attributeName,
11157+
hasReference ? /** @type {string} */ (spellable) : rawValue
11158+
)
11159+
: null;
11160+
11161+
if (dataUrl !== null) {
11162+
const delimiter = sourceDelimiter;
11163+
const holdQuoted = tailIsRead && _S._deferEmbeddedSource !== undefined;
11164+
const asWritten =
11165+
written === null || holdQuoted
11166+
? ` ${attributeName}=${delimiter}${rawValue}${delimiter}`
11167+
: written.text;
11168+
const declinedTail =
11169+
!holdQuoted && written !== null && written.unquotedTail;
11170+
/**
11171+
* @param {string | null | undefined} rendered what the renderer answered
11172+
* @returns {{ text: string, unquotedTail: boolean } | null} what to emit, or null where nothing it answered is written
11173+
*/
11174+
const writeDataUrl = (rendered) => {
11175+
if (typeof rendered !== "string" || rendered === dataUrl.payload) {
11176+
return null;
11177+
}
11178+
const url = buildDataURI(dataUrl.parsed, rendered);
11179+
const answered = holdQuoted
11180+
? {
11181+
text: _writeQuotedAttribute(attributeName, url, delimiter),
11182+
unquotedTail: false
11183+
}
11184+
: _writeAttribute(
11185+
attributeName,
11186+
escapeAttribute(url, CC_QUOTATION_MARK, true),
11187+
rawValue,
11188+
url,
11189+
quoted,
11190+
sourceDelimiter
11191+
);
11192+
11193+
return answered !== null && answered.text.length < asWritten.length
11194+
? answered
11195+
: null;
11196+
};
11197+
11198+
if (_S._deferEmbeddedSource !== undefined) {
11199+
out += _deferEmbedded(dataUrl.payload, dataUrl.type, (rendered) => {
11200+
const answered = writeDataUrl(rendered);
11201+
11202+
return answered === null ? asWritten : answered.text;
11203+
});
11204+
unquotedTail = declinedTail;
11205+
} else {
11206+
const answered = writeDataUrl(
11207+
_renderEmbeddedOrNull(dataUrl.payload, dataUrl.type)
11208+
);
11209+
11210+
out += answered === null ? asWritten : answered.text;
11211+
unquotedTail =
11212+
answered === null ? declinedTail : answered.unquotedTail;
11213+
}
11214+
consumedEnd = valueEnd;
11215+
continue;
11216+
}
11217+
1112511218
// Offered whatever the built-in made of it: a list already at its
1112611219
// shortest is one `_writeAttribute` declines, not one to ask nobody about.
1112711220
if (

‎lib/json/jsonMinify.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ jsonMinify.getTypes = () => ["json"];
6161

6262
/**
6363
* @param {string} name asset filename
64-
* @returns {boolean} true for JSON assets
64+
* @returns {boolean} true for JSON assets, a web app manifest included
6565
*/
66-
jsonMinify.filter = (name) => /^[^?#]*\.json(?:[?#].*)?$/i.test(name);
66+
jsonMinify.filter = (name) =>
67+
/^[^?#]*\.(?:json|webmanifest)(?:[?#].*)?$/i.test(name);
6768

6869
module.exports = jsonMinify;

‎lib/util/dataURL.js‎

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ const parseDataURI = (uri) => {
128128
* The payload as text, or `null` when reading it would not round-trip. A
129129
* percent-escaped payload is declined rather than decoded: how much of it the
130130
* author escaped is not recorded anywhere in the URI, so re-escaping would
131-
* rewrite bytes nothing asked to change.
131+
* rewrite bytes nothing asked to change. So is one holding a `#`, where the
132+
* payload a browser reads ends and a fragment starts.
132133
* @param {ParsedDataURI} parsed the split URI
133134
* @returns {string | null} the payload as text
134135
*/
135136
const decodeDataURIPayload = (parsed) => {
136137
if (!parsed.base64) {
137-
return parsed.payload.includes("%") ? null : parsed.payload;
138+
const { payload } = parsed;
139+
return payload.includes("%") || payload.includes("#") ? null : payload;
138140
}
139141
// Decoding is lenient rather than throwing, so garbage decodes to something
140142
// that does not round-trip, which is the same answer: leave it alone.
@@ -144,6 +146,50 @@ const decodeDataURIPayload = (parsed) => {
144146
: null;
145147
};
146148

149+
/**
150+
* A `data:` URI's payload as text, for a caller that will render it, with the
151+
* language its media type names. `null` when there is nothing to offer: not a
152+
* `data:` URI, a media type naming no language, or a payload that would not
153+
* round-trip.
154+
* @param {string} uri the URI, unquoted and unescaped
155+
* @returns {{ parsed: ParsedDataURI, type: string, payload: string } | null} what it holds, or null
156+
*/
157+
const readEmbeddedDataURI = (uri) => {
158+
const parsed = parseDataURI(uri);
159+
if (parsed === null) return null;
160+
const type = languageOfMediaType(parsed.mediaType);
161+
if (type === undefined) return null;
162+
const payload = decodeDataURIPayload(parsed);
163+
if (payload === null || payload === "") return null;
164+
return { parsed, type, payload };
165+
};
166+
167+
/**
168+
* Escape a payload for a `data:` URI written without `base64`, touching only
169+
* what would change what the URI means. The host's own quoting is the caller's.
170+
* @param {string} text the payload as text
171+
* @returns {string} the escaped payload
172+
*/
173+
const encodeDataURIPayload = (text) => {
174+
// WHY: `%` starts an escape and `#` a fragment. The URL parser drops every
175+
// tab and newline and strips C0 controls and spaces off the end, so those
176+
// are escaped too, or the payload read back is not the one written.
177+
let trailing = text.length;
178+
while (trailing > 0 && text.charCodeAt(trailing - 1) === 0x20) trailing--;
179+
let out = "";
180+
let start = 0;
181+
for (let i = 0; i < text.length; i++) {
182+
const c = text.charCodeAt(i);
183+
if (c > 0x20 && c !== 0x7f && c !== 0x25 && c !== 0x23) continue;
184+
if (c === 0x20 && i < trailing) continue;
185+
out += `${text.slice(start, i)}%${c < 0x10 ? "0" : ""}${c
186+
.toString(16)
187+
.toUpperCase()}`;
188+
start = i + 1;
189+
}
190+
return start === 0 ? text : out + text.slice(start);
191+
};
192+
147193
/**
148194
* Rebuild a `data:` URI around a new payload, in the form it was written in.
149195
* @param {ParsedDataURI} parsed the split URI
@@ -155,10 +201,7 @@ const buildDataURI = (parsed, text) => {
155201
if (parsed.base64) {
156202
return head + Buffer.from(text, "utf8").toString("base64");
157203
}
158-
// Only the two that would change what the URI means: `%` starts an escape and
159-
// `#` starts a fragment. Everything else stays as the renderer wrote it, and
160-
// the url token's own quoting is the caller's to apply.
161-
return head + text.replace(/%/g, "%25").replace(/#/g, "%23");
204+
return head + encodeDataURIPayload(text);
162205
};
163206

164207
/**
@@ -257,7 +300,9 @@ module.exports = {
257300
decodeDataURI,
258301
decodeDataURIPayload,
259302
embeddedText,
303+
encodeDataURIPayload,
260304
languageOfFilename,
261305
languageOfMediaType,
262-
parseDataURI
306+
parseDataURI,
307+
readEmbeddedDataURI
263308
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const url = new URL('data:application/json,{ "a" : "b c#" }', import.meta.url);
2+
3+
it("should hand `new URL` the rendered payload, escaped only where it must be", () => {
4+
// Only the `#` is escaped: it would start a fragment. The spaces inside the
5+
// string stay as written rather than every byte being percent-encoded.
6+
expect(url.href).toBe('data:application/json,{"a":"b c%23"}');
7+
});

0 commit comments

Comments
 (0)