diff --git a/packages/vite/src/node/server/environment.ts b/packages/vite/src/node/server/environment.ts index fff966e44491bb..281cec3b5de1b8 100644 --- a/packages/vite/src/node/server/environment.ts +++ b/packages/vite/src/node/server/environment.ts @@ -22,7 +22,10 @@ import { EnvironmentModuleGraph } from './moduleGraph' import type { EnvironmentModuleNode } from './moduleGraph' import type { HotChannel, NormalizedHotChannel } from './hmr' import { getShortName, normalizeHotChannel, updateModules } from './hmr' -import type { TransformResult } from './transformRequest' +import type { + TransformOptionsInternal, + TransformResult, +} from './transformRequest' import { transformRequest } from './transformRequest' import type { EnvironmentPluginContainer } from './pluginContainer' import { @@ -206,8 +209,12 @@ export class DevEnvironment extends BaseEnvironment { } } - transformRequest(url: string): Promise { - return transformRequest(this, url) + transformRequest( + url: string, + /** @internal */ + options?: TransformOptionsInternal, + ): Promise { + return transformRequest(this, url, options) } async warmupRequest(url: string): Promise { diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 2fb649ae1711e7..cbed9dc8445b2f 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -45,7 +45,6 @@ import { ssrTransform } from '../ssr/ssrTransform' import { reloadOnTsconfigChange } from '../plugins/esbuild' import { bindCLIShortcuts } from '../shortcuts' import type { BindCLIShortcutsOptions } from '../shortcuts' -import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants' import { CLIENT_DIR, DEFAULT_DEV_PORT, @@ -67,7 +66,6 @@ import type { MinimalPluginContextWithoutEnvironment } from '../plugin' import type { PluginContainer } from './pluginContainer' import { BasicMinimalPluginContext, - ERR_CLOSED_SERVER, basePluginContextMeta, createPluginContainer, } from './pluginContainer' @@ -93,12 +91,11 @@ import { timeMiddleware } from './middlewares/time' import { ModuleGraph } from './mixedModuleGraph' import type { ModuleNode } from './mixedModuleGraph' import { notFoundMiddleware } from './middlewares/notFound' -import { buildErrorMessage, errorMiddleware } from './middlewares/error' +import { errorMiddleware } from './middlewares/error' import type { HmrOptions, NormalizedHotChannel } from './hmr' import { handleHMRUpdate, updateModules } from './hmr' import { openBrowser as _openBrowser } from './openBrowser' import type { TransformOptions, TransformResult } from './transformRequest' -import { transformRequest } from './transformRequest' import { searchForPackageRoot, searchForWorkspaceRoot } from './searchRoot' import type { DevEnvironment } from './environment' import { hostValidationMiddleware } from './middlewares/hostCheck' @@ -594,11 +591,6 @@ export async function _createServer( }, }) }, - // environment.transformRequest and .warmupRequest don't take an options param for now, - // so the logic and error handling needs to be duplicated here. - // The only param in options that could be important is `html`, but we may remove it as - // that is part of the internal control flow for the vite dev server to be able to bail - // out and do the html fallback transformRequest(url, options) { warnFutureDeprecation( config, @@ -606,29 +598,11 @@ export async function _createServer( 'server.transformRequest() is deprecated. Use environment.transformRequest() instead.', ) const environment = server.environments[options?.ssr ? 'ssr' : 'client'] - return transformRequest(environment, url, options) + return environment.transformRequest(url) }, - async warmupRequest(url, options) { - try { - const environment = server.environments[options?.ssr ? 'ssr' : 'client'] - await transformRequest(environment, url, options) - } catch (e) { - if ( - e?.code === ERR_OUTDATED_OPTIMIZED_DEP || - e?.code === ERR_CLOSED_SERVER - ) { - // these are expected errors - return - } - // Unexpected error, log the issue but avoid an unhandled exception - server.config.logger.error( - buildErrorMessage(e, [`Pre-transform error: ${e.message}`], false), - { - error: e, - timestamp: true, - }, - ) - } + warmupRequest(url, options) { + const environment = server.environments[options?.ssr ? 'ssr' : 'client'] + return environment.warmupRequest(url) }, transformIndexHtml(url, html, originalUrl) { return devHtmlTransformFn(server, url, html, originalUrl) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index 4ad9f9dba4034f..180bb31f26a98b 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -18,11 +18,7 @@ import { removeTimestampQuery, } from '../../utils' import { send } from '../send' -import { - ERR_DENIED_ID, - ERR_LOAD_URL, - transformRequest, -} from '../transformRequest' +import { ERR_DENIED_ID, ERR_LOAD_URL } from '../transformRequest' import { applySourcemapIgnoreList } from '../sourcemap' import { isHTMLProxy } from '../../plugins/html' import { @@ -262,7 +258,7 @@ export function transformMiddleware( } // resolve, load and transform using the plugin container - const result = await transformRequest(environment, url, { + const result = await environment.transformRequest(url, { allowId(id) { return ( id.startsWith('\0') || diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index e3dcc4eb55379d..9d15a49eab097e 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -53,6 +53,9 @@ export interface TransformOptions { * @deprecated inferred from environment */ ssr?: boolean +} + +export interface TransformOptionsInternal { /** * @internal */ @@ -69,14 +72,8 @@ export interface TransformOptions { export function transformRequest( environment: DevEnvironment, url: string, - options: TransformOptions = {}, + options: TransformOptionsInternal = {}, ): Promise { - // Backward compatibility when only `ssr` is passed - if (!options.ssr) { - // Backward compatibility - options = { ...options, ssr: environment.config.consumer === 'server' } - } - if (environment._closing && environment.config.dev.recoverable) throwClosedServerError() @@ -147,7 +144,7 @@ export function transformRequest( async function doTransform( environment: DevEnvironment, url: string, - options: TransformOptions, + options: TransformOptionsInternal, timestamp: number, ) { url = removeTimestampQuery(url) @@ -237,7 +234,7 @@ async function loadAndTransform( environment: DevEnvironment, id: string, url: string, - options: TransformOptions, + options: TransformOptionsInternal, timestamp: number, mod?: EnvironmentModuleNode, resolved?: PartialResolvedId,