Skip to content

Commit e94cd2d

Browse files
authored
docs(api): improved documentation for "processAssets" hook (#4812)
* api(compilation-hooks): improved documentation for "processAssets" hook Signed-off-by: Slava Fomin II <[email protected]> * api(compilation-hooks): further improved documentation for "processAssets" hook Signed-off-by: Slava Fomin II <[email protected]> * api(compilation-hooks): fixed typo in documentation for "processAssets" hook Signed-off-by: Slava Fomin II <[email protected]> * api(compilation-hooks): fixed API description in documentation for "processAssets" hook Signed-off-by: Slava Fomin II <[email protected]> * api(compilation-hooks): improved typography in documentation for "processAssets" hook Signed-off-by: Slava Fomin II <[email protected]> * api(compilation-hooks): improved documentation for "processAssets" hook Signed-off-by: Slava Fomin II <[email protected]>
1 parent 0fb2f34 commit e94cd2d

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

src/content/api/compilation-hooks.mdx

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Compilation Hooks
33
group: Plugins
44
sort: 10
55
contributors:
6+
- slavafomin
67
- byzyk
78
- madhavarshney
89
- misterdev
@@ -455,9 +456,15 @@ The assets have been optimized.
455456

456457
Asset processing.
457458

458-
- Callback Parameters: `assets`
459+
**Hook parameters:**
460+
- `name: string` — a name of the plugin
461+
- `stage: Stage` — a stage to tap into (see the [list of supported stages](#list-of-asset-processing-stages) below)
462+
- `additionalAssets?: true | (assets, [callback]) => (void | Promise<void>)` — a callback for additional assets ([see below](#additional-assets))
463+
464+
**Callback parameters:**
465+
- `assets: { [pathname: string]: Source }` — a plain object, where key is the asset's pathname, and the value is the source content of the asset
459466

460-
Here's an example:
467+
**Example:**
461468

462469
```js
463470
compilation.hooks.processAssets.tap(
@@ -466,14 +473,21 @@ compilation.hooks.processAssets.tap(
466473
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, // see below for more stages
467474
},
468475
(assets) => {
469-
// code here
476+
console.log('List of assets and their sizes:');
477+
Object.entries(assets).forEach(([pathname, source]) => {
478+
console.log(`${pathname}: ${source.size()} bytes`);
479+
});
470480
}
471481
);
472482
```
473483

474-
In addition to `name` and `stage`, you can pass a `additionalAssets` <Badge text='5.8.0+' /> option which accepts either a value of `true` or a function with `assets` as parameter:
484+
#### Additional assets
485+
486+
In addition to `name` and `stage`, you can pass an `additionalAssets` <Badge text='5.8.0+' /> option which accepts either a value of `true` or a callback function that receives `assets` as a first argument:
487+
488+
1. `true` — run the provided callback again for assets added later by plugins.
475489

476-
1. `true` - Run callback against assets added later by plugins.
490+
In this mode, the callback will be called multiple times: once for assets added prior to the specified stage, and additional times for assets added by plugins later (on this or next stages).
477491

478492
```js
479493
compilation.hooks.processAssets.tap(
@@ -483,45 +497,65 @@ In addition to `name` and `stage`, you can pass a `additionalAssets` <Badge text
483497
additionalAssets: true,
484498
},
485499
(assets) => {
486-
// this callback will run against assets added later by plugins.
500+
// this function will be called multiple times with each bulk of assets
487501
}
488502
);
489503
```
490504

491-
2. `(assets) => {}` - Run this specified callback against assets added later by plugins.
505+
2. `(assets, [callback]) => (void | Promise<void>)` — run the specified callback against assets added by plugins later (on this or next stages). The callback must respect the type of the tap method used (e.g. when used with `tapPromise()`, it should return a promise).
492506

493507
```js
494508
compilation.hooks.processAssets.tap(
495509
{
496510
name: 'MyPlugin',
497511
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
498512
additionalAssets: (assets) => {
499-
// this callback will run against assets added later by plugins.
513+
// this function potentially could be called multiple times for assets added on later stages
500514
},
501515
},
502516
(assets) => {
503-
// code
517+
// this function will be called once with assets added by plugins on prior stages
504518
}
505519
);
506520
```
507521

508-
Here's a list of stages we can use:
509-
510-
- `PROCESS_ASSETS_STAGE_ADDITIONAL` - Add additional assets to the compilation.
511-
- `PROCESS_ASSETS_STAGE_PRE_PROCESS` - Basic preprocessing of the assets.
512-
- `PROCESS_ASSETS_STAGE_DERIVED` - Derive new assets from the existing assets.
513-
- `PROCESS_ASSETS_STAGE_ADDITIONS` - Add additional sections to the existing assets e.g. banner or initialization code.
514-
- `PROCESS_ASSETS_STAGE_OPTIMIZE` - Optimize existing assets in a general way.
515-
- `PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT` - Optimize the count of existing assets, e.g. by merging them.
516-
- `PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY` - Optimize the compatibility of existing assets, e.g. add polyfills or vendor prefixes.
517-
- `PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE` - Optimize the size of existing assets, e.g. by minimizing or omitting whitespace.
518-
- `PROCESS_ASSETS_STAGE_DEV_TOOLING` - Add development tooling to the assets, e.g. by extracting a source map.
519-
- `PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE` <Badge text='5.8.0+' /> - Optimize the numbers of existing assets, e.g. by inlining assets into other assets.
520-
- `PROCESS_ASSETS_STAGE_SUMMARIZE` - Summarize the list of existing assets.
521-
- `PROCESS_ASSETS_STAGE_OPTIMIZE_HASH` - Optimize the hashes of the assets, e.g. by generating real hashes of the asset content.
522-
- `PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER` - Optimize the transfer of existing assets, e.g. by preparing a compressed (gzip) file as separate asset.
523-
- `PROCESS_ASSETS_STAGE_ANALYSE` - Analyze the existing assets.
524-
- `PROCESS_ASSETS_STAGE_REPORT` - Creating assets for the reporting purposes.
522+
#### List of asset processing stages
523+
524+
Here's a list of supported stages (in the order of processing):
525+
526+
- `PROCESS_ASSETS_STAGE_ADDITIONAL` — add additional assets to the compilation.
527+
- `PROCESS_ASSETS_STAGE_PRE_PROCESS` — basic preprocessing of the assets.
528+
- `PROCESS_ASSETS_STAGE_DERIVED` — derive new assets from the existing assets.
529+
- `PROCESS_ASSETS_STAGE_ADDITIONS` — add additional sections to the existing assets e.g. banner or initialization code.
530+
- `PROCESS_ASSETS_STAGE_OPTIMIZE` — optimize existing assets in a general way.
531+
- `PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT` — optimize the count of existing assets, e.g. by merging them.
532+
- `PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY` — optimize the compatibility of existing assets, e.g. add polyfills or vendor prefixes.
533+
- `PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE` — optimize the size of existing assets, e.g. by minimizing or omitting whitespace.
534+
- `PROCESS_ASSETS_STAGE_DEV_TOOLING` — add development tooling to the assets, e.g. by extracting a source map.
535+
- `PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE` <Badge text='5.8.0+' /> — optimize the numbers of existing assets, e.g. by inlining assets into other assets.
536+
- `PROCESS_ASSETS_STAGE_SUMMARIZE` — summarize the list of existing assets.
537+
- `PROCESS_ASSETS_STAGE_OPTIMIZE_HASH` — optimize the hashes of the assets, e.g. by generating real hashes of the asset content.
538+
- `PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER` — optimize the transfer of existing assets, e.g. by preparing a compressed (gzip) file as separate asset.
539+
- `PROCESS_ASSETS_STAGE_ANALYSE` — analyze the existing assets.
540+
- `PROCESS_ASSETS_STAGE_REPORT` — creating assets for the reporting purposes.
541+
542+
#### Assets info
543+
544+
The "asset info" metadata is not automatically provided for this hook. If needed, you will have to resolve this metadata manually using the compilation instance and the provided asset pathname. This will be improved in a future version of the webpack.
545+
546+
**Example:**
547+
548+
```js
549+
compilation.hooks.processAssets.tap(
550+
{ /***/ },
551+
(assets) => {
552+
Object.entries(assets).forEach(([pathname, source]) => {
553+
const assetInfo = compilation.assetsInfo.get(pathname);
554+
// @todo: do something with "pathname", "source" and "assetInfo"
555+
});
556+
}
557+
);
558+
```
525559

526560
### `afterProcessAssets`
527561

0 commit comments

Comments
 (0)