Skip to content

Commit 896ac0a

Browse files
committed
docs: plugin asset emitting
1 parent 764475d commit 896ac0a

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

docs/guide/api-plugin.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,33 @@ function outputMetadataPlugin(): Plugin {
538538
}
539539
```
540540

541+
## Referencing Emitted Assets
542+
543+
To emit an asset from a plugin, call [`this.emitFile({ type: 'asset', ... })`](https://rolldown.rs/reference/Interface.PluginContext#in-depth-type-asset). It returns a `referenceId` that you can use to generate the asset's URL, since its final file name isn't known until the bundle is generated.
544+
545+
### In JavaScript
546+
547+
Use `import.meta.ROLLDOWN_FILE_URL_<referenceId>`:
548+
549+
```js
550+
const referenceId = this.emitFile({
551+
type: 'asset',
552+
name: 'icon.png',
553+
source: fileContent,
554+
})
555+
556+
// it's a JavaScript expression, so append any query or hash with string concatenation
557+
return `export default import.meta.ROLLDOWN_FILE_URL_${referenceId} + '#frag'`
558+
```
559+
560+
### In CSS or HTML
561+
562+
`import.meta.ROLLDOWN_FILE_URL_<referenceId>` only works in JavaScript expression position. In CSS or HTML, use the `__VITE_ASSET__<referenceId>__` token instead, appending any query or hash right after it:
563+
564+
```css
565+
background: url(__VITE_ASSET__abc__#frag);
566+
```
567+
541568
## Plugin Ordering
542569

543570
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)