Skip to content

Commit 5da78a6

Browse files
authored
docs: missing changes guides (#18491)
1 parent 1fcc83d commit 5da78a6

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

docs/changes/per-environment-apis.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
55
:::
66

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`
7+
Multiple APIs from `ViteDevServer` related to module graph and modules transforms have been moved to the `DevEnvironment` instances.
128

139
Affect scope: `Vite Plugin Authors`
1410

1511
::: 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.
12+
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.
1713

1814
```ts
1915
future: {
@@ -26,8 +22,12 @@ future: {
2622

2723
## Motivation
2824

29-
// TODO:
25+
In Vite v5 and before, a single Vite dev server always had two environments (`client` and `ssr`). The `server.moduleGraph` had mixed modules from both of these environments. Nodes were connected through `clientImportedModules` and `ssrImportedModules` lists (but a single `importers` list was maintained for each). A transformed module was represented by an `id` and a `ssr` boolean. This boolean needed to be passed to APIs, for example `server.moduleGraph.getModuleByUrl(url, ssr)` and `server.transformRequest(url, { ssr })`.
26+
27+
In Vite v6, it is now possible to create any number of custom environments (`client`, `ssr`, `edge`, etc). A single `ssr` boolean isn't enough anymore. Instead of changing the APIs to be of the form `server.transformRequest(url, { environment })`, we moved these methods to the environment instance allowing them to be called without a Vite dev server.
3028

3129
## Migration Guide
3230

33-
// TODO:
31+
- `server.moduleGraph` -> [`environment.moduleGraph`](/guide/api-environment#separate-module-graphs)
32+
- `server.transformRequest(url, ssr)` -> `environment.transformRequest(url)`
33+
- `server.warmupRequest(url, ssr)` -> `environment.warmupRequest(url)`

docs/changes/shared-plugins-during-build.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358)
55
:::
66

7-
// TODO:
87
See [Shared plugins during build](/guide/api-environment.md#shared-plugins-during-build).
98

109
Affect scope: `Vite Plugin Authors`
@@ -15,8 +14,68 @@ Affect scope: `Vite Plugin Authors`
1514

1615
## Motivation
1716

18-
// TODO:
17+
Align dev and build plugin pipelines.
1918

2019
## Migration Guide
2120

22-
// TODO:
21+
To be able to share plugins across environments, plugin state must be keyed by the current environment. A plugin of the following form will count the number of transformed modules across all environments.
22+
23+
```js
24+
function CountTransformedModulesPlugin() {
25+
let transformedModules
26+
return {
27+
name: 'count-transformed-modules',
28+
buildStart() {
29+
transformedModules = 0
30+
},
31+
transform(id) {
32+
transformedModules++
33+
},
34+
buildEnd() {
35+
console.log(transformedModules)
36+
},
37+
}
38+
}
39+
```
40+
41+
If we instead want to count the number of transformed modules for each environment, we need to keep a map:
42+
43+
```js
44+
function PerEnvironmentCountTransformedModulesPlugin() {
45+
const state = new Map<Environment, { count: number }>()
46+
return {
47+
name: 'count-transformed-modules',
48+
perEnvironmentStartEndDuringDev: true,
49+
buildStart() {
50+
state.set(this.environment, { count: 0 })
51+
}
52+
transform(id) {
53+
state.get(this.environment).count++
54+
},
55+
buildEnd() {
56+
console.log(this.environment.name, state.get(this.environment).count)
57+
}
58+
}
59+
}
60+
```
61+
62+
To simplify this pattern, internally in Vite, we use a `usePerEnvironmentState` helper:
63+
64+
```js
65+
function PerEnvironmentCountTransformedModulesPlugin() {
66+
const state = usePerEnvironmentState<{ count: number }>(() => ({ count: 0 }))
67+
return {
68+
name: 'count-transformed-modules',
69+
perEnvironmentStartEndDuringDev: true,
70+
buildStart() {
71+
state(this).count = 0
72+
}
73+
transform(id) {
74+
state(this).count++
75+
},
76+
buildEnd() {
77+
console.log(this.environment.name, state(this).count)
78+
}
79+
}
80+
}
81+
```

packages/vite/src/node/server/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export interface ServerOptions extends CommonServerOptions {
169169
* @default false
170170
* @experimental
171171
*/
172-
perEnvironmentBuildStartEnd?: boolean
172+
perEnvironmentStartEndDuringDev?: boolean
173173
/**
174174
* Run HMR tasks, by default the HMR propagation is done in parallel for all environments
175175
* @experimental
@@ -1033,7 +1033,7 @@ export function resolveServerOptions(
10331033
): ResolvedServerOptions {
10341034
const server: ResolvedServerOptions = {
10351035
preTransformRequests: true,
1036-
perEnvironmentBuildStartEnd: false,
1036+
perEnvironmentStartEndDuringDev: false,
10371037
...(raw as Omit<ResolvedServerOptions, 'sourcemapIgnoreList'>),
10381038
sourcemapIgnoreList:
10391039
raw?.sourcemapIgnoreList === false

0 commit comments

Comments
 (0)