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/config/build-options.md
+11-2
Original file line number
Diff line number
Diff line change
@@ -48,13 +48,22 @@ type ResolveModulePreloadDependenciesFn = (
48
48
49
49
The `resolveDependencies` function will be called for each dynamic import with a list of the chunks it depends on, and it will also be called for each chunk imported in entry HTML files. A new dependencies array can be returned with these filtered or more dependencies injected, and their paths modified. The `deps` paths are relative to the `build.outDir`. Returning a relative path to the `hostId` for `hostType === 'js'` is allowed, in which case `newURL(dep, import.meta.url)` is used to get an absolute path when injecting this module preload in the HTML head.
Copy file name to clipboardexpand all lines: docs/config/dep-optimization-options.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,9 @@ Dependencies to exclude from pre-bundling.
19
19
:::warning CommonJS
20
20
CommonJS dependencies should not be excluded from optimization. If an ESM dependency is excluded from optimization, but has a nested CommonJS dependency, the CommonJS dependency should be added to `optimizeDeps.include`. Example:
21
21
22
-
```js
22
+
```js twoslash
23
+
import { defineConfig } from'vite'
24
+
// ---cut---
23
25
exportdefaultdefineConfig({
24
26
optimizeDeps: {
25
27
include: ['esm-dep > cjs-dep'],
@@ -37,7 +39,9 @@ By default, linked packages not inside `node_modules` are not pre-bundled. Use t
37
39
38
40
**Experimental:** If you're using a library with many deep imports, you can also specify a trailing glob pattern to pre-bundle all deep imports at once. This will avoid constantly pre-bundling whenever a new deep import is used. [Give Feedback](https://github.com/vitejs/vite/discussions/15833). For example:
Copy file name to clipboardexpand all lines: docs/config/index.md
+7-3
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,9 @@ Vite also directly supports TS config files. You can use `vite.config.ts` with t
50
50
51
51
If the config needs to conditionally determine options based on the command (`serve` or `build`), the [mode](/guide/env-and-mode) being used, if it's an SSR build (`isSsrBuild`), or is previewing the build (`isPreview`), it can export a function instead:
@@ -73,7 +75,9 @@ It is important to note that in Vite's API the `command` value is `serve` during
73
75
74
76
If the config needs to call async functions, it can export an async function instead. And this async function can also be passed through `defineConfig` for improved intellisense support:
@@ -88,7 +92,7 @@ Environmental Variables can be obtained from `process.env` as usual.
88
92
89
93
Note that Vite doesn't load `.env` files by default as the files to load can only be determined after evaluating the Vite config, for example, the `root` and `envDir` options affect the loading behaviour. However, you can use the exported `loadEnv` helper to load the specific `.env` file if needed.
Copy file name to clipboardexpand all lines: docs/config/server-options.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -18,10 +18,10 @@ The first case is when `localhost` is used. Node.js under v17 reorders the resul
18
18
19
19
You can set [`dns.setDefaultResultOrder('verbatim')`](https://nodejs.org/api/dns.html#dns_dns_setdefaultresultorder_order) to disable the reordering behavior. Vite will then print the address as `localhost`.
20
20
21
-
```js
21
+
```js twoslash
22
22
// vite.config.js
23
23
import { defineConfig } from'vite'
24
-
importdnsfrom'dns'
24
+
importdnsfrom'node:dns'
25
25
26
26
dns.setDefaultResultOrder('verbatim')
27
27
@@ -238,7 +238,7 @@ Create Vite server in middleware mode.
Use a custom logger to log messages. You can use Vite's `createLogger` API to get the default logger and customize it to, for example, change the message or filter out certain warnings.
// `InferCustomEventPayload` provides types for built-in Vite events
36
35
on<Textendsstring>(
37
36
event:T,
38
37
cb: (payload:InferCustomEventPayload<T>) =>void,
@@ -67,7 +66,9 @@ Vite provides type definitions for `import.meta.hot` in [`vite/client.d.ts`](htt
67
66
68
67
For a module to self-accept, use `import.meta.hot.accept` with a callback which receives the updated module:
69
68
70
-
```js
69
+
```js twoslash
70
+
import'vite/client'
71
+
// ---cut---
71
72
exportconstcount=1
72
73
73
74
if (import.meta.hot) {
@@ -90,7 +91,13 @@ Vite requires that the call to this function appears as `import.meta.hot.accept(
90
91
91
92
A module can also accept updates from direct dependencies without reloading itself:
92
93
93
-
```js
94
+
```js twoslash
95
+
// @filename: /foo.d.ts
96
+
exportdeclareconst foo: () =>void
97
+
98
+
// @filename: /example.js
99
+
import'vite/client'
100
+
// ---cut---
94
101
import { foo } from'./foo.js'
95
102
96
103
foo()
@@ -117,7 +124,9 @@ if (import.meta.hot) {
117
124
118
125
A self-accepting module or a module that expects to be accepted by others can use `hot.dispose` to clean-up any persistent side effects created by its updated copy:
119
126
120
-
```js
127
+
```js twoslash
128
+
import'vite/client'
129
+
// ---cut---
121
130
functionsetupSideEffect() {}
122
131
123
132
setupSideEffect()
@@ -133,7 +142,9 @@ if (import.meta.hot) {
133
142
134
143
Register a callback that will call when the module is no longer imported on the page. Compared to `hot.dispose`, this can be used if the source code cleans up side-effects by itself on updates and you only need to clean-up when it's removed from the page. Vite currently uses this for `.css` imports.
135
144
136
-
```js
145
+
```js twoslash
146
+
import'vite/client'
147
+
// ---cut---
137
148
functionsetupOrReuseSideEffect() {}
138
149
139
150
setupOrReuseSideEffect()
@@ -151,7 +162,9 @@ The `import.meta.hot.data` object is persisted across different instances of the
151
162
152
163
Note that re-assignment of `data` itself is not supported. Instead, you should mutate properties of the `data` object so information added from other handlers are preserved.
153
164
154
-
```js
165
+
```js twoslash
166
+
import'vite/client'
167
+
// ---cut---
155
168
// ok
156
169
import.meta.hot.data.someValue = 'hello'
157
170
@@ -169,7 +182,9 @@ A self-accepting module may realize during runtime that it can't handle a HMR up
169
182
170
183
Note that you should always call `import.meta.hot.accept` even if you plan to call `invalidate` immediately afterwards, or else the HMR client won't listen for future changes to the self-accepting module. To communicate your intent clearly, we recommend calling `invalidate` within the `accept` callback like so:
171
184
172
-
```js
185
+
```js twoslash
186
+
import'vite/client'
187
+
// ---cut---
173
188
import.meta.hot.accept((module) => {
174
189
// You may use the new module instance to decide whether to invalidate.
0 commit comments