Skip to content

Commit cf30751

Browse files
authored
docs: document viteMetadata API for build outputs (#21650)
1 parent 85544d7 commit cf30751

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

docs/config/build-options.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ Whether to generate a manifest file that contains a mapping of non-hashed asset
251251

252252
When the value is a string, it will be used as the manifest file path relative to `build.outDir`. When set to `true`, the path would be `.vite/manifest.json`.
253253

254+
If you are writing a plugin and need to inspect each output chunk or asset's related CSS and static assets during the build, you can also use [`viteMetadata` output bundle metadata API](/guide/api-plugin#output-bundle-metadata).
255+
254256
## build.ssrManifest
255257

256258
- **Type:** `boolean | string`

docs/guide/api-plugin.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,44 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
470470
}
471471
```
472472
473+
## Output Bundle Metadata
474+
475+
During build, Vite augments Rolldown's build output objects with a Vite-specific `viteMetadata` field.
476+
477+
This is available through:
478+
479+
- `RenderedChunk` (for example in `renderChunk` and `augmentChunkHash`)
480+
- `OutputChunk` and `OutputAsset` (for example in `generateBundle` and `writeBundle`)
481+
482+
`viteMetadata` provides:
483+
484+
- `viteMetadata.importedCss: Set<string>`
485+
- `viteMetadata.importedAssets: Set<string>`
486+
487+
This is useful when writing plugins that need to inspect emitted CSS and static assets without relying on [`build.manifest`](/config/build-options#build-manifest).
488+
489+
Example:
490+
491+
```ts [vite.config.ts]
492+
function outputMetadataPlugin(): Plugin {
493+
return {
494+
name: 'output-metadata-plugin',
495+
generateBundle(_, bundle) {
496+
for (const output of Object.values(bundle)) {
497+
const css = output.viteMetadata?.importedCss
498+
const assets = output.viteMetadata?.importedAssets
499+
if (!css?.size && !assets?.size) continue
500+
501+
console.log(output.fileName, {
502+
css: css ? [...css] : [],
503+
assets: assets ? [...assets] : [],
504+
})
505+
}
506+
},
507+
}
508+
}
509+
```
510+
473511
## Plugin Ordering
474512

475513
A Vite plugin can additionally specify an `enforce` property (similar to webpack loaders) to adjust its application order. The value of `enforce` can be either `"pre"` or `"post"`. The resolved plugins will be in the following order:

0 commit comments

Comments
 (0)