|
8 | 8 | const querystring = require("querystring"); |
9 | 9 | const { |
10 | 10 | AsyncSeriesBailHook, |
| 11 | + AsyncSeriesWaterfallHook, |
11 | 12 | HookMap, |
12 | | - SyncHook, |
13 | | - SyncWaterfallHook |
| 13 | + SyncHook |
14 | 14 | } = require("tapable"); |
15 | 15 | const { |
16 | 16 | CachedSource, |
@@ -184,7 +184,9 @@ const recordSideEffectsBailout = (mod, moduleGraph, dep) => { |
184 | 184 | .getOptimizationBailout(mod) |
185 | 185 | .push( |
186 | 186 | () => |
187 | | - `Dependency (${dep.type}) with side effects at ${formatLocation(dep.loc)}` |
| 187 | + `Dependency (${dep.type}) with side effects at ${formatLocation( |
| 188 | + dep.loc |
| 189 | + )}` |
188 | 190 | ); |
189 | 191 | }; |
190 | 192 |
|
@@ -542,10 +544,14 @@ const createCompilationHooks = () => { |
542 | 544 | */ |
543 | 545 | beforeSnapshot: new SyncHook(["module"]), |
544 | 546 | /** |
545 | | - * @type {SyncWaterfallHook<[Result, NormalModule]>} |
| 547 | + * Called with what the loaders produced, before it is parsed. Return the |
| 548 | + * (possibly transformed) result. Async since 5.110.0, so a tap can await |
| 549 | + * work of its own — minifying an asset, say — where before only a loader |
| 550 | + * could; a `tap` written for the synchronous hook keeps working. |
| 551 | + * @type {AsyncSeriesWaterfallHook<[Result, NormalModule]>} |
546 | 552 | * @since 5.99.0 |
547 | 553 | */ |
548 | | - processResult: new SyncWaterfallHook(["result", "module"]), |
| 554 | + processResult: new AsyncSeriesWaterfallHook(["result", "module"]), |
549 | 555 | /** |
550 | 556 | * @type {AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>} |
551 | 557 | * @since 5.49.0 |
@@ -1052,7 +1058,9 @@ class NormalModule extends Module { |
1052 | 1058 | (parseJson(options)); |
1053 | 1059 | } catch (err) { |
1054 | 1060 | throw new Error( |
1055 | | - `Cannot parse string options: ${/** @type {Error} */ (err).message}`, |
| 1061 | + `Cannot parse string options: ${ |
| 1062 | + /** @type {Error} */ (err).message |
| 1063 | + }`, |
1056 | 1064 | { cause: err } |
1057 | 1065 | ); |
1058 | 1066 | } |
@@ -1305,11 +1313,37 @@ class NormalModule extends Module { |
1305 | 1313 | }); |
1306 | 1314 | return callback(error); |
1307 | 1315 | } |
1308 | | - const result = hooks.processResult.call( |
| 1316 | + // Untapped, nothing can change: stay on the synchronous path rather |
| 1317 | + // than paying a tick for every module a build has. |
| 1318 | + if (hooks.processResult.taps.length === 0) { |
| 1319 | + return processedResult(/** @type {Result} */ (result_)); |
| 1320 | + } |
| 1321 | + |
| 1322 | + hooks.processResult.callAsync( |
1309 | 1323 | /** @type {Result} */ |
1310 | 1324 | (result_), |
1311 | | - this |
| 1325 | + this, |
| 1326 | + (hookErr, result) => { |
| 1327 | + if (hookErr) { |
| 1328 | + return callback( |
| 1329 | + new (getModuleBuildError())( |
| 1330 | + hookErr instanceof Error |
| 1331 | + ? hookErr |
| 1332 | + : new (getNonErrorEmittedError())(hookErr) |
| 1333 | + ) |
| 1334 | + ); |
| 1335 | + } |
| 1336 | + |
| 1337 | + processedResult(/** @type {Result} */ (result)); |
| 1338 | + } |
1312 | 1339 | ); |
| 1340 | + }; |
| 1341 | + |
| 1342 | + /** |
| 1343 | + * @param {Result} result what the loaders produced, past the hook |
| 1344 | + * @returns {void} |
| 1345 | + */ |
| 1346 | + const processedResult = (result) => { |
1313 | 1347 | const source = result[0]; |
1314 | 1348 | const sourceMap = result.length >= 1 ? result[1] : null; |
1315 | 1349 | const extraInfo = result.length >= 2 ? result[2] : null; |
@@ -1441,7 +1475,9 @@ class NormalModule extends Module { |
1441 | 1475 | }) |
1442 | 1476 | ).catch((err) => { |
1443 | 1477 | throw new Error( |
1444 | | - `Failed to parse source map. ${/** @type {Error} */ (err).message}` |
| 1478 | + `Failed to parse source map. ${ |
| 1479 | + /** @type {Error} */ (err).message |
| 1480 | + }` |
1445 | 1481 | ); |
1446 | 1482 | }) |
1447 | 1483 | ); |
|
0 commit comments