Skip to content

Commit d44361d

Browse files
feat: add a processResultAsync hook, and rename the asset it rewrote
Rewriting an asset's bytes and emitting it under a new name was only possible from a loader. A plugin could not do it: minification is async and processResult is synchronous, and by the time an asset reaches processAssets its URL is already written into the bundle, so renaming there moves the file and leaves every reference pointing at the old one — with no error. processResultAsync runs before processResult, over the loaders' result, and can await. It is skipped when nothing uses it, so a build without it keeps the synchronous path and the stack depth anything thrown below is reported at — making processResult itself async would have put tapable's frames into every parse error, since CssModulesPlugin taps it and experiments.css is on by default. What the hook throws is reported as this module's build error, naming the hook so it is not read as a loader's. Renaming needs a channel that outlives the build. matchResource says the right thing but belongs to resolution and is not serialized, so a rename set while building is lost on a cache hit and the restored module's sourceFilename then conflicts with the cached codegen. buildInfo is written with the module, so the new assetResource lives there. The emitted name and the media type an inlined asset is written as both read that resource, and a data: URI's own media type is only trusted while nothing has re-encoded it — otherwise webp bytes ship labelled image/png.
1 parent 1f3264e commit d44361d

15 files changed

Lines changed: 339 additions & 8 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 a `processResultAsync` hook and let a tap rename the asset it rewrote.

‎lib/NormalModule.js‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const querystring = require("querystring");
99
const {
1010
AsyncSeriesBailHook,
11+
AsyncSeriesWaterfallHook,
1112
HookMap,
1213
SyncHook,
1314
SyncWaterfallHook
@@ -184,7 +185,9 @@ const recordSideEffectsBailout = (mod, moduleGraph, dep) => {
184185
.getOptimizationBailout(mod)
185186
.push(
186187
() =>
187-
`Dependency (${dep.type}) with side effects at ${formatLocation(dep.loc)}`
188+
`Dependency (${dep.type}) with side effects at ${formatLocation(
189+
dep.loc
190+
)}`
188191
);
189192
};
190193

@@ -546,6 +549,13 @@ const createCompilationHooks = () => {
546549
* @since 5.99.0
547550
*/
548551
processResult: new SyncWaterfallHook(["result", "module"]),
552+
/**
553+
* The same, for a tap that has to await work of its own — minifying an
554+
* asset, say, which before only a loader could do. Skipped when unused.
555+
* @type {AsyncSeriesWaterfallHook<[Result, NormalModule]>}
556+
* @since 5.110.0
557+
*/
558+
processResultAsync: new AsyncSeriesWaterfallHook(["result", "module"]),
549559
/**
550560
* @type {AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>}
551561
* @since 5.49.0
@@ -1052,7 +1062,9 @@ class NormalModule extends Module {
10521062
(parseJson(options));
10531063
} catch (err) {
10541064
throw new Error(
1055-
`Cannot parse string options: ${/** @type {Error} */ (err).message}`,
1065+
`Cannot parse string options: ${
1066+
/** @type {Error} */ (err).message
1067+
}`,
10561068
{ cause: err }
10571069
);
10581070
}
@@ -1441,7 +1453,9 @@ class NormalModule extends Module {
14411453
})
14421454
).catch((err) => {
14431455
throw new Error(
1444-
`Failed to parse source map. ${/** @type {Error} */ (err).message}`
1456+
`Failed to parse source map. ${
1457+
/** @type {Error} */ (err).message
1458+
}`
14451459
);
14461460
})
14471461
);
@@ -1503,7 +1517,32 @@ class NormalModule extends Module {
15031517
if (result.notCacheableReasons.length > 0) {
15041518
buildInfo.notCacheableReasons = result.notCacheableReasons;
15051519
}
1506-
processResult(err, result.result);
1520+
// Skipped when unused, so the synchronous path and the stack depth
1521+
// below it are unchanged; `isUsed` so an interceptor alone runs.
1522+
if (!hooks.processResultAsync.isUsed()) {
1523+
return processResult(err, result.result);
1524+
}
1525+
1526+
hooks.processResultAsync.callAsync(
1527+
/** @type {Result} */
1528+
(result.result),
1529+
this,
1530+
(hookErr, asyncResult) => {
1531+
if (hookErr) {
1532+
// Named, so a failure here is not read as a loader's.
1533+
return callback(
1534+
new (getModuleBuildError())(
1535+
hookErr instanceof Error
1536+
? hookErr
1537+
: new (getNonErrorEmittedError())(hookErr),
1538+
{ from: "processResultAsync hook" }
1539+
)
1540+
);
1541+
}
1542+
1543+
processResult(null, /** @type {Result} */ (asyncResult));
1544+
}
1545+
);
15071546
}
15081547
);
15091548
}

‎lib/asset/AssetGenerator.js‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,27 @@ class AssetGenerator extends Generator {
250250
static getSourceFileName(module, runtimeTemplate) {
251251
return makePathsRelative(
252252
runtimeTemplate.compilation.compiler.context,
253-
/** @type {string} */
254-
(module.getResource()),
253+
AssetGenerator.getAssetResource(module),
255254
runtimeTemplate.compilation.compiler.root
256255
).replace(/^\.\//, "");
257256
}
258257

258+
/**
259+
* The resource this asset is named after, which something that re-encoded it
260+
* may have changed. Its emitted name and inlined media type both read this.
261+
* @param {NormalModule} module module
262+
* @returns {string} the resource
263+
*/
264+
static getAssetResource(module) {
265+
const buildInfo = /** @type {AssetModuleBuildInfo | undefined} */ (
266+
module.buildInfo
267+
);
268+
269+
return /** @type {string} */ (
270+
(buildInfo && buildInfo.assetResource) || module.getResource()
271+
);
272+
}
273+
259274
/**
260275
* Gets the source file name as seen by a filename template. With
261276
* `experiments.futureDefaults` (webpack 6 default) `[path]` and `[file]` of a
@@ -504,11 +519,17 @@ class AssetGenerator extends Generator {
504519
/** @type {AssetGeneratorDataUrlOptions} */
505520
(this.dataUrlOptions).mimetype;
506521
if (mimeType === undefined) {
522+
// The resource as it now is, minus a query, the way `nameForCondition`
523+
// reads the original.
524+
const resource = AssetGenerator.getAssetResource(module);
525+
const queryIndex = resource.indexOf("?");
507526
const ext = path.extname(
508-
/** @type {NameForCondition} */
509-
(module.nameForCondition())
527+
queryIndex >= 0 ? resource.slice(0, queryIndex) : resource
510528
);
529+
// `resourceResolveData` describes what was read; once something
530+
// re-encoded it, only the new extension says what the bytes are.
511531
if (
532+
resource === module.getResource() &&
512533
module.resourceResolveData &&
513534
module.resourceResolveData.mimetype !== undefined
514535
) {

‎lib/asset/AssetModule.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const makeSerializable = require("../util/makeSerializable");
2222
* @property {string=} filename
2323
* @property {AssetInfo=} assetInfo
2424
* @property {string=} fullContentHash
25+
* @property {string=} assetResource the resource this asset is named after, when
26+
* something re-encoded it. Unlike `matchResource` this is serialized, so a
27+
* rename set while building survives the persistent cache
2528
*/
2629

2730
/** @typedef {NormalModuleBuildInfo & KnownAssetModuleBuildInfo} AssetModuleBuildInfo */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[
5+
{ moduleName: /file\.png$/ },
6+
/^Module build failed \(from processResultAsync hook\):\nError: re-encoding failed/
7+
]
8+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// `var` so the parser cannot fold the branch away and drop the dependency —
2+
// the module must be built, only never executed.
3+
var never = false;
4+
5+
it("should build the failing module without crashing", () => {
6+
if (never) {
7+
require("../_images/file.png");
8+
}
9+
});
10+
11+
it("should report what the hook threw as this module's build error", () => {
12+
expect(() => require("../_images/file.png")).toThrow("re-encoding failed");
13+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
// A module carrying a build error is rebuilt on every run by design.
4+
module.exports = (options) =>
5+
options.cache && options.cache.type === "filesystem"
6+
? [/Pack got invalid because of write to/]
7+
: [];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
const NormalModule = require("../../../../lib/NormalModule");
4+
5+
// What a failing minimizer looks like: the rejection has to surface as this
6+
// module's build error, not as an unhandled one.
7+
class FailingTap {
8+
/**
9+
* @param {import("../../../../lib/Compiler")} compiler compiler
10+
*/
11+
apply(compiler) {
12+
compiler.hooks.compilation.tap("FailingTap", (compilation) => {
13+
NormalModule.getCompilationHooks(
14+
compilation
15+
).processResultAsync.tapPromise("FailingTap", async (result, module) => {
16+
if (!/\.png$/.test(module.resource)) return result;
17+
throw new Error("re-encoding failed");
18+
});
19+
});
20+
}
21+
}
22+
23+
/** @type {import("../../../../").Configuration} */
24+
module.exports = {
25+
mode: "development",
26+
module: {
27+
rules: [
28+
{
29+
test: /\.png$/i,
30+
type: "asset/resource"
31+
}
32+
]
33+
},
34+
plugins: [new FailingTap()]
35+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import url from "data:image/png;base64,iVBORw0KGgo=";
2+
3+
it("should inline a rewritten data-uri asset as what it now is", () => {
4+
// The media type the module was read with no longer describes the bytes.
5+
expect(url).toMatch(/^data:image\/webp;base64,/);
6+
expect(Buffer.from(url.split(",")[1], "base64").toString()).toBe(
7+
"webp-bytes"
8+
);
9+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const NormalModule = require("../../../../lib/NormalModule");
4+
5+
// A module read from a `data:` URI carries its own media type. Once a tap
6+
// re-encodes it, that type describes what was read, not what is emitted.
7+
class RenameOnProcessResult {
8+
/**
9+
* @param {import("../../../../lib/Compiler")} compiler compiler
10+
*/
11+
apply(compiler) {
12+
compiler.hooks.compilation.tap("RenameOnProcessResult", (compilation) => {
13+
NormalModule.getCompilationHooks(
14+
compilation
15+
).processResultAsync.tapPromise(
16+
"RenameOnProcessResult",
17+
async (result, module) => {
18+
if (!/^data:image\/png/.test(module.resource)) return result;
19+
await Promise.resolve();
20+
/** @type {import("../../../../lib/asset/AssetModule").AssetModuleBuildInfo} */
21+
(module.buildInfo).assetResource = "rewritten.webp";
22+
return [Buffer.from("webp-bytes"), result[1], result[2]];
23+
}
24+
);
25+
});
26+
}
27+
}
28+
29+
/** @type {import("../../../../").Configuration} */
30+
module.exports = {
31+
mode: "development",
32+
module: {
33+
rules: [
34+
{
35+
mimetype: "image/png",
36+
type: "asset/inline"
37+
}
38+
]
39+
},
40+
plugins: [new RenameOnProcessResult()]
41+
};

0 commit comments

Comments
 (0)