You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/api-plugin.md
+30-22Lines changed: 30 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -36,9 +36,7 @@ If your plugin is only going to work for a particular framework, its name should
36
36
-`vite-plugin-react-` prefix for React Plugins
37
37
-`vite-plugin-svelte-` prefix for Svelte Plugins
38
38
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).
42
40
43
41
## Plugins config
44
42
@@ -84,8 +82,35 @@ export default defineConfig({
84
82
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.
85
83
:::
86
84
85
+
### Transforming Custom File Types
86
+
87
+
```js
88
+
constfileRegex=/\.(my-file-ext)$/
89
+
90
+
exportdefaultfunctionmyPlugin() {
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
+
87
106
### Importing a Virtual File
88
107
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
+
89
114
```js
90
115
exportdefaultfunctionmyPlugin() {
91
116
constvirtualModuleId='@my-virtual-module'
@@ -115,26 +140,9 @@ import { msg } from '@my-virtual-module'
115
140
console.log(msg)
116
141
```
117
142
118
-
### Transforming Custom File Types
119
-
120
-
```js
121
-
constfileRegex=/\.(my-file-ext)$/
122
-
123
-
exportdefaultfunctionmyPlugin() {
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.
126
144
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.
0 commit comments