Skip to content

Commit

Permalink
refactor: update future options (#18011)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Sep 3, 2024
1 parent 0f26342 commit 6443e48
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/changes/hotupdate-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion docs/changes/per-environment-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/changes/ssr-using-modulerunner.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/changes/this-environment-in-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'
removePluginHookSsrArgument?: 'warn'

removeServerModuleGraph?: 'warn'
removeServerHot?: 'warn'
removeServerTransformRequest?: 'warn'

removeSsrLoadModule?: 'warn'
}

export interface ExperimentalOptions {
Expand Down
52 changes: 19 additions & 33 deletions packages/vite/src/node/deprecations.ts
Original file line number Diff line number Diff line change
@@ -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<keyof FutureDeprecationWarningsOptions, string>
removeSsrLoadModule: 'changes/ssr-using-modulerunner',
} satisfies Record<keyof FutureOptions, string>

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<keyof FutureDeprecationWarningsOptions, string>
} satisfies Record<keyof FutureOptions, string>

let _ignoreDeprecationWarnings = false

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export async function handleHMRUpdate(
} else if (type === 'update') {
warnFutureDeprecation(
config,
'pluginHookHandleHotUpdate',
'removePluginHookHandleHotUpdate',
`Used in plugin "${plugin.name}".`,
false,
)
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ export async function _createServer(
environments,
pluginContainer,
get moduleGraph() {
warnFutureDeprecation(config, 'serverModuleGraph')
warnFutureDeprecation(config, 'removeServerModuleGraph')
return moduleGraph
},
set moduleGraph(graph) {
Expand All @@ -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']
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6443e48

Please sign in to comment.