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
Multiple APIs from `ViteDevServer` related to module graph and modules transforms have been moved to the `DevEnvironment` instances.
12
8
13
9
Affect scope: `Vite Plugin Authors`
14
10
15
11
::: 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.
17
13
18
14
```ts
19
15
future: {
@@ -26,8 +22,12 @@ future: {
26
22
27
23
## Motivation
28
24
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.
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
+
functionCountTransformedModulesPlugin() {
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:
0 commit comments