Skip to content

Commit 5ad2a62

Browse files
authored
feat: Environment API 1/4 (vitejs#1544)
* 差分取り込み * Using environments in the Vite server * Environment agnostic SSR - Environment Configuration * ModuleEvaluator まで * 残り * 用語統一 * docs: update Environment API guide after beta * dead links 対応
1 parent 4e57564 commit 5ad2a62

File tree

12 files changed

+1327
-238
lines changed

12 files changed

+1327
-238
lines changed

.vitepress/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ export default defineConfig({
290290
link: '/guide/api-javascript',
291291
},
292292
{
293-
text: 'Vite ランタイム API',
294-
link: '/guide/api-vite-runtime',
293+
text: 'Environment API',
294+
link: '/guide/api-environment',
295295
},
296296
{
297297
text: '設定リファレンス',

blog/announcing-vite5-1.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ The new API brings many benefits:
5656

5757
The initial idea [was proposed by Pooya Parsa](https://github.com/nuxt/vite/pull/201) and implemented by [Anthony Fu](https://github.com/antfu) as the [vite-node](https://github.com/vitest-dev/vitest/tree/main/packages/vite-node#readme) package to [power Nuxt 3 Dev SSR](https://antfu.me/posts/dev-ssr-on-nuxt) and later also used as the base for [Vitest](https://vitest.dev). So the general idea of vite-node has been battle-tested for quite some time now. This is a new iteration of the API by [Vladimir Sheremet](https://github.com/sheremet-va), who had already re-implemented vite-node in Vitest and took the learnings to make the API even more powerful and flexible when adding it to Vite Core. The PR was one year in the makings, you can see the evolution and discussions with ecosystem maintainers [here](https://github.com/vitejs/vite/issues/12165).
5858

59-
Read more in the [Vite Runtime API guide](/guide/api-vite-runtime) and [give us feedback](https://github.com/vitejs/vite/discussions/15774).
59+
::: info
60+
The Vite Runtime API evolved into the Module Runner API, released in Vite 6 as part of the [Environment API](/guide/api-environment).
61+
:::
6062

6163
## Features
6264

changes/hotupdate-hook.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# HMR `hotUpdate` Plugin Hook
2+
3+
::: tip Feedback
4+
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
5+
:::
6+
7+
We're planning to deprecate the `handleHotUpdate` plugin hook in favor of [`hotUpdate` hook](/guide/api-environment#the-hotupdate-hook) to be [Environment API](/guide/api-environment.md) aware, and handle additional watch events with `create` and `delete`.
8+
9+
Affected scope: `Vite Plugin Authors`
10+
11+
::: warning Future Deprecation
12+
`hotUpdate` was first introduced in `v6.0`. The deprecation of `handleHotUpdate` is planned for `v7.0`. We don't yet recommend moving away from `handleHotUpdate` yet. If you want to experiment and give us feedback, you can use the `future.removePluginHookHandleHotUpdate` to `"warn"` in your vite config.
13+
:::
14+
15+
## Motivation
16+
17+
The [`handleHotUpdate` hook](/guide/api-plugin.md#handlehotupdate) allows to perform custom HMR update handling. A list of modules to be updated is passed in the `HmrContext`
18+
19+
```ts
20+
interface HmrContext {
21+
file: string
22+
timestamp: number
23+
modules: Array<ModuleNode>
24+
read: () => string | Promise<string>
25+
server: ViteDevServer
26+
}
27+
```
28+
29+
This hook is called once for all environments, and the passed modules have mixed information from the Client and SSR environments only. Once frameworks move to custom environments, a new hook that is called for each of them is needed.
30+
31+
The new `hotUpdate` hook works in the same way as `handleHotUpdate` but it is called for each environment and receives a new `HotUpdateContext` instance:
32+
33+
```ts
34+
interface HotUpdateContext {
35+
type: 'create' | 'update' | 'delete'
36+
file: string
37+
timestamp: number
38+
modules: Array<EnvironmentModuleNode>
39+
read: () => string | Promise<string>
40+
server: ViteDevServer
41+
}
42+
```
43+
44+
The current dev environment can be accessed like in other Plugin hooks with `this.environment`. The `modules` list will now be module nodes from the current environment only. Each environment update can define different update strategies.
45+
46+
This hook is also now called for additional watch events and not only for `'update'`. Use `type` to differentiate between them.
47+
48+
## Migration Guide
49+
50+
Filter and narrow down the affected module list so that the HMR is more accurate.
51+
52+
```js
53+
handleHotUpdate({ modules }) {
54+
return modules.filter(condition)
55+
}
56+
57+
// Migrate to:
58+
59+
hotUpdate({ modules }) {
60+
return modules.filter(condition)
61+
}
62+
```
63+
64+
Return an empty array and perform a full reload:
65+
66+
```js
67+
handleHotUpdate({ server, modules, timestamp }) {
68+
// Invalidate modules manually
69+
const invalidatedModules = new Set()
70+
for (const mod of modules) {
71+
server.moduleGraph.invalidateModule(
72+
mod,
73+
invalidatedModules,
74+
timestamp,
75+
true
76+
)
77+
}
78+
server.ws.send({ type: 'full-reload' })
79+
return []
80+
}
81+
82+
// Migrate to:
83+
84+
hotUpdate({ modules, timestamp }) {
85+
// Invalidate modules manually
86+
const invalidatedModules = new Set()
87+
for (const mod of modules) {
88+
this.environment.moduleGraph.invalidateModule(
89+
mod,
90+
invalidatedModules,
91+
timestamp,
92+
true
93+
)
94+
}
95+
this.environment.hot.send({ type: 'full-reload' })
96+
return []
97+
}
98+
```
99+
100+
Return an empty array and perform complete custom HMR handling by sending custom events to the client:
101+
102+
```js
103+
handleHotUpdate({ server }) {
104+
server.ws.send({
105+
type: 'custom',
106+
event: 'special-update',
107+
data: {}
108+
})
109+
return []
110+
}
111+
112+
// Migrate to...
113+
114+
hotUpdate() {
115+
this.environment.hot.send({
116+
type: 'custom',
117+
event: 'special-update',
118+
data: {}
119+
})
120+
return []
121+
}
122+
```

changes/index.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Breaking Changes
2+
3+
List of breaking changes in Vite including API deprecations, removals, and changes. Most of the changes below can be opt-in using the [`future` option](/config/shared-options.html#future) in your Vite config.
4+
5+
## Planned
6+
7+
These changes are planned for the next major version of Vite. The deprecation or usage warnings will guide you where possible, and we're reaching out to framework, plugin authors, and users to apply these changes.
8+
9+
- _No planned changes yet_
10+
11+
## Considering
12+
13+
These changes are being considered and are often experimental APIs that intend to improve upon current usage patterns. As not all changes are listed here, please check out the [Experimental Label in Vite GitHub Discussions](https://github.com/vitejs/vite/discussions/categories/feedback?discussions_q=label%3Aexperimental+category%3AFeedback) for the full list.
14+
15+
We don't recommend switching to these APIs yet. They are included in Vite to help us gather feedback. Please check these proposals and let us know how they work in your use case in each's linked GitHub Discussions.
16+
17+
- [`this.environment` in Hooks](/changes/this-environment-in-hooks)
18+
- [HMR `hotUpdate` Plugin Hook](/changes/hotupdate-hook)
19+
- [Move to per-environment APIs](/changes/per-environment-apis)
20+
- [SSR using `ModuleRunner` API](/changes/ssr-using-modulerunner)
21+
- [Shared plugins during build](/changes/shared-plugins-during-build)
22+
23+
## Past
24+
25+
The changes below has been done or reverted. They are no longer relevant in the current major version.
26+
27+
- _No past changes yet_

changes/per-environment-apis.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Move to per-environment APIs
2+
3+
::: tip Feedback
4+
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
5+
:::
6+
7+
Multiple APIs from ViteDevServer related to module graph has replaced with more isolated Environment APIs.
8+
9+
- `server.moduleGraph` -> [`environment.moduleGraph`](/guide/api-environment#separate-module-graphs)
10+
- `server.transformRequest` -> `environment.transformRequest`
11+
- `server.warmupRequest` -> `environment.warmupRequest`
12+
13+
Affect scope: `Vite Plugin Authors`
14+
15+
::: warning Future Deprecation
16+
The Environment instance was first introduced at `v6.0`. The deprecation of `server.moduleGraph` and other methods that are now in environments is planned for `v7.0`. We don't recommend moving away from server methods yet. To identify your usage, set these in your vite config.
17+
18+
```ts
19+
future: {
20+
removeServerModuleGraph: 'warn',
21+
removeServerTransformRequest: 'warn',
22+
}
23+
```
24+
25+
:::
26+
27+
## Motivation
28+
29+
// TODO:
30+
31+
## Migration Guide
32+
33+
// TODO:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Shared Plugins during Build
2+
3+
::: tip Feedback
4+
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
5+
:::
6+
7+
// TODO:
8+
See [Shared plugins during build](/guide/api-environment.md#shared-plugins-during-build).
9+
10+
Affect scope: `Vite Plugin Authors`
11+
12+
::: warning Future Default Change
13+
`builder.sharedConfigBuild` was first introduce in `v6.0`. You can set it true to check how your plugins work with a shared config. We're looking for feedback about changing the default in a future major once the plugin ecosystem is ready.
14+
:::
15+
16+
## Motivation
17+
18+
// TODO:
19+
20+
## Migration Guide
21+
22+
// TODO:

changes/ssr-using-modulerunner.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SSR using `ModuleRunner` API
2+
3+
::: tip Feedback
4+
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
5+
:::
6+
7+
`server.ssrLoadModule` has been replaced by [Module Runner](/guide/api-environment#modulerunner).
8+
9+
Affect scope: `Vite Plugin Authors`
10+
11+
::: warning Future Deprecation
12+
`ModuleRunner` was first introduce in `v6.0`. The deprecation of `server.ssrLoadModule` is planned for a future major. To identify your usage, set `future.removeSrLoadModule` to `"warn"` in your vite config.
13+
:::
14+
15+
## Motivation
16+
17+
// TODO:
18+
19+
## Migration Guide
20+
21+
// TODO:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# `this.environment` in Hooks
2+
3+
::: tip Feedback
4+
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
5+
:::
6+
7+
Before Vite 6, only two environments were available: `client` and `ssr`. A single `options.ssr` plugin hook argument in `resolveId`, `load` and `transform` allowed plugin authors to differentiate between these two environments when processing modules in plugin hooks. In Vite 6, a Vite application can define any number of named environments as needed. We're introducing `this.environment` in the plugin context to interact with the environment of the current module in hooks.
8+
9+
Affect scope: `Vite Plugin Authors`
10+
11+
::: warning Future Deprecation
12+
`this.environment` was introduced in `v6.0`. The deprecation of `options.ssr` is planned for `v7.0`. At that point we'll start recommending migrating your plugins to use the new API. To identify your usage, set `future.removePluginHookSsrArgument` to `"warn"` in your vite config.
13+
:::
14+
15+
## Motivation
16+
17+
`this.environment` not only allow the plugin hook implementation to know the current environment name, it also gives access to the environment config options, module graph information, and transform pipeline (`environment.config`, `environment.moduleGraph`, `environment.transformRequest()`). Having the environment instance available in the context allows plugin authors to avoid the dependency of the whole dev server (typically cached at startup through the `configureServer` hook).
18+
19+
## Migration Guide
20+
21+
For the existing plugin to do a quick migration, replace the `options.ssr` argument with `this.environment.name !== 'client'` in the `resolveId`, `load` and `transform` hooks:
22+
23+
```ts
24+
import { Plugin } from 'vite'
25+
26+
export function myPlugin(): Plugin {
27+
return {
28+
name: 'my-plugin',
29+
resolveId(id, importer, options) {
30+
const isSSR = options.ssr // [!code --]
31+
const isSSR = this.environment.name !== 'client' // [!code ++]
32+
33+
if (isSSR) {
34+
// SSR specific logic
35+
} else {
36+
// Client specific logic
37+
}
38+
},
39+
}
40+
}
41+
```
42+
43+
For a more robust long term implementation, the plugin hook should handle for [multiple environments](/guide/api-environment.html#accessing-the-current-environment-in-hooks) using fine-grained environment options instead of relying on the environment name.

0 commit comments

Comments
 (0)