From 071f79be10dadd1d1969fa5134cfd663946d9361 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Mon, 28 Oct 2024 10:17:40 +0100 Subject: [PATCH 1/3] docs: missing changes guides --- docs/changes/per-environment-apis.md | 16 ++--- docs/changes/shared-plugins-during-build.md | 65 ++++++++++++++++++++- 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/docs/changes/per-environment-apis.md b/docs/changes/per-environment-apis.md index 93ec216a6459e3..4ff27380b1b96a 100644 --- a/docs/changes/per-environment-apis.md +++ b/docs/changes/per-environment-apis.md @@ -4,16 +4,12 @@ Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358) ::: -Multiple APIs from ViteDevServer related to module graph has replaced with more isolated Environment APIs. - -- `server.moduleGraph` -> [`environment.moduleGraph`](/guide/api-environment#separate-module-graphs) -- `server.transformRequest` -> `environment.transformRequest` -- `server.warmupRequest` -> `environment.warmupRequest` +Multiple APIs from `ViteDevServer` related to module graph and modules transforms have been moved to the `DevEnvironment` instances. Affect scope: `Vite Plugin Authors` ::: warning Future Deprecation -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. +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. ```ts future: { @@ -26,8 +22,12 @@ future: { ## Motivation -// TODO: +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 })`. + +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. ## Migration Guide -// TODO: +- `server.moduleGraph` -> [`environment.moduleGraph`](/guide/api-environment#separate-module-graphs) +- `server.transformRequest(url, ssr)` -> `environment.transformRequest(url)` +- `server.warmupRequest(url, ssr)` -> `environment.warmupRequest(url)` diff --git a/docs/changes/shared-plugins-during-build.md b/docs/changes/shared-plugins-during-build.md index 3c9fc99f698a61..8cbad33de57ef4 100644 --- a/docs/changes/shared-plugins-during-build.md +++ b/docs/changes/shared-plugins-during-build.md @@ -4,7 +4,6 @@ Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358) ::: -// TODO: See [Shared plugins during build](/guide/api-environment.md#shared-plugins-during-build). Affect scope: `Vite Plugin Authors` @@ -15,8 +14,68 @@ Affect scope: `Vite Plugin Authors` ## Motivation -// TODO: +Align dev and build plugin pipelines. ## Migration Guide -// TODO: +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. + +```js +function CountTransformedModulesPlugin() { + let transformedModules + return { + name: 'count-transformed-modules', + buildStart() { + transformedModules = 0 + }, + transform(id) { + transformedModules++ + }, + buildEnd() { + console.log(transformedModules) + }, + } +} +``` + +If we instead want to count the number of transformed modules for each environment, we need to keep a map: + +```js +function PerEnvironmentCountTransformedModulesPlugin() { + const state = new Map() + return { + name: 'count-transformed-modules', + perEnvironmentBuildStartEnd: true, + buildStart() { + state.set(this.environment, { count: 0 }) + } + transform(id) { + state.get(this.environment).count++ + }, + buildEnd() { + console.log(this.environment.name, state.get(this.environment).count) + } + } +} +``` + +To simplify this pattern, internally in Vite, we use a `useEnvironmentState` helper: + +```js +function PerEnvironmentCountTransformedModulesPlugin() { + const state = usePerEnvironmentState<{ count: number }>(() => ({ count: 0 })) + return { + name: 'count-transformed-modules', + perEnvironmentBuildStartEnd: true, + buildStart() { + state(this).count = 0 + } + transform(id) { + state(this).count++ + }, + buildEnd() { + console.log(this.environment.name, state(this).count) + } + } +} +``` From 278b19191e43b47141248424c270d8aecbc98192 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Mon, 28 Oct 2024 12:36:38 +0100 Subject: [PATCH 2/3] docs: rename perEnvironmentStartEndDuringDev --- docs/changes/shared-plugins-during-build.md | 4 ++-- packages/vite/src/node/server/index.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/changes/shared-plugins-during-build.md b/docs/changes/shared-plugins-during-build.md index 8cbad33de57ef4..5701ad5519b97c 100644 --- a/docs/changes/shared-plugins-during-build.md +++ b/docs/changes/shared-plugins-during-build.md @@ -45,7 +45,7 @@ function PerEnvironmentCountTransformedModulesPlugin() { const state = new Map() return { name: 'count-transformed-modules', - perEnvironmentBuildStartEnd: true, + perEnvironmentStartEndDuringDev: true, buildStart() { state.set(this.environment, { count: 0 }) } @@ -66,7 +66,7 @@ function PerEnvironmentCountTransformedModulesPlugin() { const state = usePerEnvironmentState<{ count: number }>(() => ({ count: 0 })) return { name: 'count-transformed-modules', - perEnvironmentBuildStartEnd: true, + perEnvironmentStartEndDuringDev: true, buildStart() { state(this).count = 0 } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 29244d0b54b8df..ac5bbb44619838 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -170,7 +170,7 @@ export interface ServerOptions extends CommonServerOptions { * @default false * @experimental */ - perEnvironmentBuildStartEnd?: boolean + perEnvironmentStartEndDuringDev?: boolean /** * Run HMR tasks, by default the HMR propagation is done in parallel for all environments * @experimental @@ -1026,7 +1026,7 @@ export function resolveServerOptions( ): ResolvedServerOptions { const server: ResolvedServerOptions = { preTransformRequests: true, - perEnvironmentBuildStartEnd: false, + perEnvironmentStartEndDuringDev: false, ...(raw as Omit), sourcemapIgnoreList: raw?.sourcemapIgnoreList === false From 27c3ccf66e88c6f8fbcaf03c04a13ddbf9e7220e Mon Sep 17 00:00:00 2001 From: patak-dev Date: Mon, 28 Oct 2024 12:37:07 +0100 Subject: [PATCH 3/3] docs: usePerEnvironmentState --- docs/changes/shared-plugins-during-build.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes/shared-plugins-during-build.md b/docs/changes/shared-plugins-during-build.md index 5701ad5519b97c..7cebea65df2c7b 100644 --- a/docs/changes/shared-plugins-during-build.md +++ b/docs/changes/shared-plugins-during-build.md @@ -59,7 +59,7 @@ function PerEnvironmentCountTransformedModulesPlugin() { } ``` -To simplify this pattern, internally in Vite, we use a `useEnvironmentState` helper: +To simplify this pattern, internally in Vite, we use a `usePerEnvironmentState` helper: ```js function PerEnvironmentCountTransformedModulesPlugin() {