From 96f4e599e2ae1425683c70e761b87c023bd7c1d6 Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 3 Sep 2024 17:27:13 +0800 Subject: [PATCH 1/2] refactor: update future options --- docs/changes/hotupdate-hook.md | 2 +- docs/changes/per-environment-apis.md | 10 ++++- docs/changes/ssr-using-modulerunner.md | 2 +- docs/changes/this-environment-in-hooks.md | 2 +- packages/vite/src/node/config.ts | 15 ++++--- packages/vite/src/node/deprecations.ts | 52 +++++++++-------------- packages/vite/src/node/server/hmr.ts | 2 +- packages/vite/src/node/server/index.ts | 6 +-- 8 files changed, 43 insertions(+), 48 deletions(-) diff --git a/docs/changes/hotupdate-hook.md b/docs/changes/hotupdate-hook.md index c99ae08f15255d..9a660c2d18fdf1 100644 --- a/docs/changes/hotupdate-hook.md +++ b/docs/changes/hotupdate-hook.md @@ -9,7 +9,7 @@ We're planning to deprecate the `handleHotUpdate` plugin hook in favor of [`hotU Affected scope: `Vite Plugin Authors` ::: warning Future Deprecation -`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.deprecationWarnings.pluginHookHandleHotUpdate` to `true` in your vite config. +`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. ::: ## Motivation diff --git a/docs/changes/per-environment-apis.md b/docs/changes/per-environment-apis.md index 7d0a24e329add8..93ec216a6459e3 100644 --- a/docs/changes/per-environment-apis.md +++ b/docs/changes/per-environment-apis.md @@ -13,7 +13,15 @@ Multiple APIs from ViteDevServer related to module graph has replaced with more 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 `future.deprecationWarnings` 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: { + removeServerModuleGraph: 'warn', + removeServerTransformRequest: 'warn', +} +``` + ::: ## Motivation diff --git a/docs/changes/ssr-using-modulerunner.md b/docs/changes/ssr-using-modulerunner.md index e014aa4e48909e..130c20f63a198b 100644 --- a/docs/changes/ssr-using-modulerunner.md +++ b/docs/changes/ssr-using-modulerunner.md @@ -9,7 +9,7 @@ Give us feedback at [Environment API feedback discussion](https://github.com/vit Affect scope: `Vite Plugin Authors` ::: warning Future Deprecation -`ModuleRunner` was first introduce in `v6.0`. The deprecation of `server.ssrLoadModule` is planned for a future major. To identify your usage, set `future.deprecationWarnings.ssrLoadModule` to `true` in your vite config. +`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. ::: ## Motivation diff --git a/docs/changes/this-environment-in-hooks.md b/docs/changes/this-environment-in-hooks.md index 30e10b0aa0a09f..2577ef3c306694 100644 --- a/docs/changes/this-environment-in-hooks.md +++ b/docs/changes/this-environment-in-hooks.md @@ -9,7 +9,7 @@ Before Vite 6, only two environments were available: `client` and `ssr`. A singl Affect scope: `Vite Plugin Authors` ::: warning Future Deprecation -`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.deprecationWarnings.pluginHookSsrArgument` to `true` in your vite config. +`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. ::: ## Motivation diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 1d13e581a252f9..d573e1a744760f 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -86,7 +86,6 @@ import type { ResolvedSSROptions, SSROptions } from './ssr' import { resolveSSROptions } from './ssr' import { PartialEnvironment } from './baseEnvironment' import { createIdResolver } from './idResolver' -import type { FutureDeprecationWarningsOptions } from './deprecations' const debug = createDebugger('vite:config') const promisifiedRealpath = promisify(fs.realpath) @@ -453,12 +452,14 @@ export interface HTMLOptions { } export interface FutureOptions { - /** - * Emit warning messages for deprecated/will-deprecated features at runtime. - * - * Setting to `true` to enable all warnings - */ - deprecationWarnings?: boolean | FutureDeprecationWarningsOptions + removePluginHookHandleHotUpdate?: 'warn' | false + removePluginHookSsrArgument?: 'warn' | false + + removeServerModuleGraph?: 'warn' | false + removeServerHot?: 'warn' | false + removeServerTransformRequest?: 'warn' | false + + removeSsrLoadModule?: 'warn' | false } export interface ExperimentalOptions { diff --git a/packages/vite/src/node/deprecations.ts b/packages/vite/src/node/deprecations.ts index f0517a7482d5c1..568240c5545b65 100644 --- a/packages/vite/src/node/deprecations.ts +++ b/packages/vite/src/node/deprecations.ts @@ -1,46 +1,35 @@ import colors from 'picocolors' -import type { ResolvedConfig } from './config' +import type { FutureOptions, ResolvedConfig } from './config' // TODO: switch to production docs URL const docsURL = 'https://deploy-preview-16471--vite-docs-main.netlify.app' -export interface FutureDeprecationWarningsOptions { - pluginHookHandleHotUpdate?: boolean - pluginHookSsrArgument?: boolean - - serverModuleGraph?: boolean - serverHot?: boolean - serverTransformRequest?: boolean - - ssrLoadModule?: boolean -} - const deprecationCode = { - pluginHookSsrArgument: 'changes/this-environment-in-hooks', - pluginHookHandleHotUpdate: 'changes/hotupdate-hook', + removePluginHookSsrArgument: 'changes/this-environment-in-hooks', + removePluginHookHandleHotUpdate: 'changes/hotupdate-hook', - serverModuleGraph: 'changes/per-environment-apis', - serverHot: 'changes/per-environment-apis', - serverTransformRequest: 'changes/per-environment-apis', + removeServerModuleGraph: 'changes/per-environment-apis', + removeServerHot: 'changes/per-environment-apis', + removeServerTransformRequest: 'changes/per-environment-apis', - ssrLoadModule: 'changes/ssr-using-modulerunner', -} satisfies Record + removeSsrLoadModule: 'changes/ssr-using-modulerunner', +} satisfies Record const deprecationMessages = { - pluginHookSsrArgument: + removePluginHookSsrArgument: "Plugin hook `options.ssr` is replaced with `this.environment.config.consumer === 'server'`.", - pluginHookHandleHotUpdate: + removePluginHookHandleHotUpdate: 'Plugin hook `handleHotUpdate()` is replaced with `hotUpdate()`.', - serverModuleGraph: + removeServerModuleGraph: 'The `server.moduleGraph` is replaced with `this.environment.moduleGraph`.', - serverHot: 'The `server.hot` is replaced with `this.environment.hot`.', - serverTransformRequest: + removeServerHot: 'The `server.hot` is replaced with `this.environment.hot`.', + removeServerTransformRequest: 'The `server.transformRequest` is replaced with `this.environment.transformRequest`.', - ssrLoadModule: + removeSsrLoadModule: 'The `server.ssrLoadModule` is replaced with Environment Runner.', -} satisfies Record +} satisfies Record let _ignoreDeprecationWarnings = false @@ -50,17 +39,14 @@ let _ignoreDeprecationWarnings = false */ export function warnFutureDeprecation( config: ResolvedConfig, - type: keyof FutureDeprecationWarningsOptions, + type: keyof FutureOptions, extraMessage?: string, stacktrace = true, ): void { - if (_ignoreDeprecationWarnings) return - - if (!config.future?.deprecationWarnings) return - if ( - config.future.deprecationWarnings !== true && - !config.future.deprecationWarnings[type] + _ignoreDeprecationWarnings || + !config.future || + config.future[type] !== 'warn' ) return diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 1450e27e46546a..d0d2dfe1b43736 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -293,7 +293,7 @@ export async function handleHMRUpdate( } else if (type === 'update') { warnFutureDeprecation( config, - 'pluginHookHandleHotUpdate', + 'removePluginHookHandleHotUpdate', `Used in plugin "${plugin.name}".`, false, ) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index dd3ce79cd30ce2..d823d1d07c6dfc 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -528,7 +528,7 @@ export async function _createServer( environments, pluginContainer, get moduleGraph() { - warnFutureDeprecation(config, 'serverModuleGraph') + warnFutureDeprecation(config, 'removeServerModuleGraph') return moduleGraph }, set moduleGraph(graph) { @@ -552,7 +552,7 @@ export async function _createServer( transformRequest(url, options) { warnFutureDeprecation( config, - 'serverTransformRequest', + 'removeServerTransformRequest', 'server.transformRequest() is deprecated. Use environment.transformRequest() instead.', ) const environment = server.environments[options?.ssr ? 'ssr' : 'client'] @@ -581,7 +581,7 @@ export async function _createServer( return devHtmlTransformFn(server, url, html, originalUrl) }, async ssrLoadModule(url, opts?: { fixStacktrace?: boolean }) { - warnFutureDeprecation(config, 'ssrLoadModule') + warnFutureDeprecation(config, 'removeSsrLoadModule') return ssrLoadModule(url, server, opts?.fixStacktrace) }, ssrFixStacktrace(e) { From 5e0e5be01190312d70e06cd821fb37772628864b Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 3 Sep 2024 17:42:48 +0800 Subject: [PATCH 2/2] chore: remove false --- packages/vite/src/node/config.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index d573e1a744760f..382d3e701985bb 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -452,14 +452,14 @@ export interface HTMLOptions { } export interface FutureOptions { - removePluginHookHandleHotUpdate?: 'warn' | false - removePluginHookSsrArgument?: 'warn' | false + removePluginHookHandleHotUpdate?: 'warn' + removePluginHookSsrArgument?: 'warn' - removeServerModuleGraph?: 'warn' | false - removeServerHot?: 'warn' | false - removeServerTransformRequest?: 'warn' | false + removeServerModuleGraph?: 'warn' + removeServerHot?: 'warn' + removeServerTransformRequest?: 'warn' - removeSsrLoadModule?: 'warn' | false + removeSsrLoadModule?: 'warn' } export interface ExperimentalOptions {