Skip to content

Commit fb956c9

Browse files
authored
docs: reorder virtual modules convention (#6541)
1 parent 4bf3d51 commit fb956c9

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

docs/guide/api-plugin.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ If your plugin is only going to work for a particular framework, its name should
3636
- `vite-plugin-react-` prefix for React Plugins
3737
- `vite-plugin-svelte-` prefix for Svelte Plugins
3838

39-
Vite convention for virtual modules is to prefix the user-facing path with `virtual:`. If possible the plugin name should be used as a namespace to avoid collisions with other plugins in the ecosystem. For example, a `vite-plugin-posts` could ask users to import a `virtual:posts` or `virtual:posts/helpers` virtual modules to get build time information. Internally, plugins that use virtual modules should prefix the module ID with `\0` while resolving the id, a convention from the rollup ecosystem. This prevents other plugins from trying to process the id (like node resolution), and core features like sourcemaps can use this info to differentiate between virtual modules and regular files. `\0` is not a permitted char in import URLs so we have to replace them during import analysis. A `\0{id}` virtual id ends up encoded as `/@id/__x00__{id}` during dev in the browser. The id will be decoded back before entering the plugins pipeline, so this is not seen by plugins hooks code.
40-
41-
Note that modules directly derived from a real file, as in the case of a script module in a Single File Component (like a .vue or .svelte SFC) don't need to follow this convention. SFCs generally generate a set of submodules when processed but the code in these can be mapped back to the filesystem. Using `\0` for these submodules would prevent sourcemaps from working correctly.
39+
See also [Virtual Modules Convention](#virtual-modules-convention).
4240

4341
## Plugins config
4442

@@ -84,8 +82,35 @@ export default defineConfig({
8482
It is common convention to author a Vite/Rollup plugin as a factory function that returns the actual plugin object. The function can accept options which allows users to customize the behavior of the plugin.
8583
:::
8684

85+
### Transforming Custom File Types
86+
87+
```js
88+
const fileRegex = /\.(my-file-ext)$/
89+
90+
export default function myPlugin() {
91+
return {
92+
name: 'transform-file',
93+
94+
transform(src, id) {
95+
if (fileRegex.test(id)) {
96+
return {
97+
code: compileFileToJS(src),
98+
map: null // provide source map if available
99+
}
100+
}
101+
}
102+
}
103+
}
104+
```
105+
87106
### Importing a Virtual File
88107

108+
See the example in the [next section](#virtual-modules-convention).
109+
110+
## Virtual Modules Convention
111+
112+
Virtual modules are a useful scheme that allows you to pass build time information to the source files using normal ESM import syntax.
113+
89114
```js
90115
export default function myPlugin() {
91116
const virtualModuleId = '@my-virtual-module'
@@ -115,26 +140,9 @@ import { msg } from '@my-virtual-module'
115140
console.log(msg)
116141
```
117142

118-
### Transforming Custom File Types
119-
120-
```js
121-
const fileRegex = /\.(my-file-ext)$/
122-
123-
export default function myPlugin() {
124-
return {
125-
name: 'transform-file',
143+
Virtual modules in Vite (and Rollup) are prefixed with `virtual:` for the user-facing path by convention. If possible the plugin name should be used as a namespace to avoid collisions with other plugins in the ecosystem. For example, a `vite-plugin-posts` could ask users to import a `virtual:posts` or `virtual:posts/helpers` virtual modules to get build time information. Internally, plugins that use virtual modules should prefix the module ID with `\0` while resolving the id, a convention from the rollup ecosystem. This prevents other plugins from trying to process the id (like node resolution), and core features like sourcemaps can use this info to differentiate between virtual modules and regular files. `\0` is not a permitted char in import URLs so we have to replace them during import analysis. A `\0{id}` virtual id ends up encoded as `/@id/__x00__{id}` during dev in the browser. The id will be decoded back before entering the plugins pipeline, so this is not seen by plugins hooks code.
126144

127-
transform(src, id) {
128-
if (fileRegex.test(id)) {
129-
return {
130-
code: compileFileToJS(src),
131-
map: null // provide source map if available
132-
}
133-
}
134-
}
135-
}
136-
}
137-
```
145+
Note that modules directly derived from a real file, as in the case of a script module in a Single File Component (like a .vue or .svelte SFC) don't need to follow this convention. SFCs generally generate a set of submodules when processed but the code in these can be mapped back to the filesystem. Using `\0` for these submodules would prevent sourcemaps from working correctly.
138146

139147
## Universal Hooks
140148

0 commit comments

Comments
 (0)