diff --git a/docs/guide/api-vite-environment.md b/docs/guide/api-vite-environment.md index 88278ed492e234..fac53cbc67b397 100644 --- a/docs/guide/api-vite-environment.md +++ b/docs/guide/api-vite-environment.md @@ -58,12 +58,12 @@ class DevEnvironment { * Resolved plugins for this environment, including the ones * created using the per-environment `create` hook */ - plugins: IsolatedPlugin[] + plugins: EnvironmentPlugin[] /** * Allows to resolve, load, and transform code through the * environment plugins pipeline */ - pluginContainer: IsolatedPluginContatiner + pluginContainer: EnvironmentPluginContainer /** * Resolved config options for this environment. Options at the server * global scope are taken as defaults for all environments, and can @@ -888,19 +888,19 @@ function myPlugin() { // Share state among all environments in dev and build const sharedState = ... - const plugin = (environment) => { - // Isolated state for each environment during dev and build - const isolatedState = ... - - return { - name: 'isolated-plugin', - transform(code, id) { ... } - } + return { + name: 'with-environment-plugins', + environmentPlugins(environment) { + // Isolated state for each environment during dev and build + const isolatedState = ... + return { + name: 'per-environment-plugin', + transform(code, id) { ... }, + } + }, + // Opt-in into a single instance for all environments + sharedDuringBuild: true } - - // Opt-in into a single instance for all environments - plugin.sharedDuringBuild = true - return plugin } ``` diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index c4aef25912d0bf..6c310528a3e4e7 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -47,7 +47,7 @@ import { partialEncodeURIPath, requireResolveFromRootWithFallback, } from './utils' -import { resolveIsolatedPlugins } from './plugin' +import { resolveEnvironmentPlugins } from './plugin' import { manifestPlugin } from './plugins/manifest' import type { Logger } from './logger' import { dataURIPlugin } from './plugins/dataUri' @@ -66,7 +66,7 @@ import { mergeConfig } from './publicUtils' import { webWorkerPostPlugin } from './plugins/worker' import { getHookHandler } from './plugins' import { Environment } from './environment' -import type { IsolatedPluginConstructor, Plugin, PluginContext } from './plugin' +import type { Plugin, PluginContext } from './plugin' export interface BuildEnvironmentOptions { /** @@ -534,9 +534,7 @@ export async function build( function resolveConfigToBuild( inlineConfig: InlineConfig = {}, patchConfig?: (config: ResolvedConfig) => void, - patchPlugins?: ( - resolvedPlugins: (Plugin | IsolatedPluginConstructor)[], - ) => void, + patchPlugins?: (resolvedPlugins: Plugin[]) => void, ) { return resolveConfig( inlineConfig, @@ -610,11 +608,9 @@ export async function buildEnvironment( // inject environment and ssr arg to plugin load/transform hooks // TODO: rework lib mode - const plugins = ( - libOptions - ? (config.plugins.filter((p) => typeof p !== 'function') as Plugin[]) - : environment.plugins - ).map((p) => injectEnvironmentToHooks(p, environment)) + const plugins = (libOptions ? config : environment).plugins.map((p) => + injectEnvironmentToHooks(p, environment), + ) const rollupOptions: RollupOptions = { preserveEntrySignatures: ssr @@ -1443,7 +1439,7 @@ export class BuildEnvironment extends Environment { return } this._inited = true - this._plugins = await resolveIsolatedPlugins(this) + this._plugins = await resolveEnvironmentPlugins(this) } } @@ -1529,9 +1525,7 @@ export async function createBuilder( lib: false, } } - const patchPlugins = ( - resolvedPlugins: (Plugin | IsolatedPluginConstructor)[], - ) => { + const patchPlugins = (resolvedPlugins: Plugin[]) => { // Force opt-in shared plugins const environmentPlugins = [...resolvedPlugins] let validMixedPlugins = true diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 5c7b6bbd4a92de..ef75e4b1536f55 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -22,7 +22,6 @@ import { } from './constants' import type { HookHandler, - IsolatedPluginConstructor, Plugin, PluginEnvironment, PluginOption, @@ -83,8 +82,8 @@ import type { LogLevel, Logger } from './logger' import { createLogger } from './logger' import type { DepOptimizationConfig, DepOptimizationOptions } from './optimizer' import type { JsonOptions } from './plugins/json' -import type { IsolatedPluginContainer } from './server/pluginContainer' -import { createIsolatedPluginContainer } from './server/pluginContainer' +import type { EnvironmentPluginContainer } from './server/pluginContainer' +import { createEnvironmentPluginContainer } from './server/pluginContainer' import type { PackageCache } from './packages' import { findNearestPackageData } from './packages' import { loadEnv, resolveEnvPrefix } from './env' @@ -538,7 +537,7 @@ export type ResolvedConfig = Readonly< resolve: Required & { alias: Alias[] } - plugins: readonly (Plugin | IsolatedPluginConstructor)[] + plugins: readonly Plugin[] css: ResolvedCSSOptions esbuild: ESBuildOptions | false server: ResolvedServerOptions @@ -762,9 +761,7 @@ export async function resolveConfig( defaultNodeEnv = 'development', isPreview = false, patchConfig: ((config: ResolvedConfig) => void) | undefined = undefined, - patchPlugins: - | ((resolvedPlugins: (Plugin | IsolatedPluginConstructor)[]) => void) - | undefined = undefined, + patchPlugins: ((resolvedPlugins: Plugin[]) => void) | undefined = undefined, ): Promise { let config = inlineConfig let configFileDependencies: string[] = [] @@ -805,7 +802,7 @@ export async function resolveConfig( mode = inlineConfig.mode || config.mode || mode configEnv.mode = mode - const filterPlugin = (p: Plugin | IsolatedPluginConstructor) => { + const filterPlugin = (p: Plugin) => { if (!p) { return false } else if (typeof p === 'function' || !p.apply) { @@ -819,18 +816,10 @@ export async function resolveConfig( // resolve plugins const rawPlugins = ( - (await asyncFlatten(config.plugins || [])) as ( - | Plugin - | IsolatedPluginConstructor - )[] + (await asyncFlatten(config.plugins || [])) as Plugin[] ).filter(filterPlugin) - const sharedPlugins = rawPlugins.filter( - (plugin) => typeof plugin !== 'function', - ) as Plugin[] - - const [prePlugins, normalPlugins, postPlugins] = - sortUserPlugins(sharedPlugins) + const [prePlugins, normalPlugins, postPlugins] = sortUserPlugins(rawPlugins) const isBuild = command === 'build' @@ -1226,12 +1215,12 @@ export async function resolveConfig( */ createResolver(options) { const alias: { - client?: IsolatedPluginContainer - ssr?: IsolatedPluginContainer + client?: EnvironmentPluginContainer + ssr?: EnvironmentPluginContainer } = {} const resolver: { - client?: IsolatedPluginContainer - ssr?: IsolatedPluginContainer + client?: EnvironmentPluginContainer + ssr?: EnvironmentPluginContainer } = {} const environments = this.environments ?? resolvedEnvironments const createPluginContainer = async ( @@ -1242,7 +1231,7 @@ export async function resolveConfig( // environment so we can safely cast to a base Environment instance to a // PluginEnvironment here const environment = new Environment(environmentName, this) - const pluginContainer = await createIsolatedPluginContainer( + const pluginContainer = await createEnvironmentPluginContainer( environment as PluginEnvironment, plugins, ) @@ -1256,7 +1245,7 @@ export async function resolveConfig( ssr?: boolean, ): Promise { const environmentName = ssr ? 'ssr' : 'client' - let container: IsolatedPluginContainer + let container: EnvironmentPluginContainer if (aliasOnly) { let aliasContainer = alias[environmentName] if (!aliasContainer) { @@ -1318,18 +1307,10 @@ export async function resolveConfig( // Backward compatibility hook used in builder, opt-in to shared plugins during build patchPlugins?.(resolvedPlugins) - ;(resolved.plugins as (Plugin | IsolatedPluginConstructor)[]) = - resolvedPlugins + ;(resolved.plugins as Plugin[]) = resolvedPlugins // TODO: Deprecate config.getSortedPlugins and config.getSortedPluginHooks - Object.assign( - resolved, - createPluginHookUtils( - resolved.plugins.filter( - (plugin) => typeof plugin !== 'function', - ) as Plugin[], - ), - ) + Object.assign(resolved, createPluginHookUtils(resolved.plugins)) // call configResolved hooks await Promise.all( diff --git a/packages/vite/src/node/environment.ts b/packages/vite/src/node/environment.ts index 0148f1874e0d3e..8df034a721760a 100644 --- a/packages/vite/src/node/environment.ts +++ b/packages/vite/src/node/environment.ts @@ -1,7 +1,7 @@ import colors from 'picocolors' import type { Logger } from './logger' import type { ResolvedConfig, ResolvedEnvironmentOptions } from './config' -import type { IsolatedPlugin } from './plugin' +import type { EnvironmentPlugin } from './plugin' export class Environment { name: string @@ -9,7 +9,7 @@ export class Environment { config: ResolvedConfig options: ResolvedEnvironmentOptions - get plugins(): IsolatedPlugin[] { + get plugins(): EnvironmentPlugin[] { if (!this._plugins) throw new Error( `${this.name} environment.plugins called before initialized`, @@ -19,7 +19,7 @@ export class Environment { /** * @internal */ - _plugins: IsolatedPlugin[] | undefined + _plugins: EnvironmentPlugin[] | undefined /** * @internal */ diff --git a/packages/vite/src/node/idResolver.ts b/packages/vite/src/node/idResolver.ts index 8f0b4d1a30d3d6..bcf0f124a2677d 100644 --- a/packages/vite/src/node/idResolver.ts +++ b/packages/vite/src/node/idResolver.ts @@ -3,8 +3,8 @@ import aliasPlugin from '@rollup/plugin-alias' import type { ResolvedConfig } from './config' import type { Environment } from './environment' import type { PluginEnvironment } from './plugin' -import type { IsolatedPluginContainer } from './server/pluginContainer' -import { createIsolatedPluginContainer } from './server/pluginContainer' +import type { EnvironmentPluginContainer } from './server/pluginContainer' +import { createEnvironmentPluginContainer } from './server/pluginContainer' import { resolvePlugin } from './plugins/resolve' import type { InternalResolveOptions } from './plugins/resolve' import { getFsUtils } from './fsUtils' @@ -26,7 +26,7 @@ export function createIdResolver( ): ResolveIdFn { const scan = options?.scan - const pluginContainerMap = new Map() + const pluginContainerMap = new Map() async function resolve( environment: PluginEnvironment, id: string, @@ -34,7 +34,7 @@ export function createIdResolver( ): Promise { let pluginContainer = pluginContainerMap.get(environment) if (!pluginContainer) { - pluginContainer = await createIsolatedPluginContainer(environment, [ + pluginContainer = await createEnvironmentPluginContainer(environment, [ aliasPlugin({ entries: config.resolve.alias }), // TODO: resolve.alias per environment? resolvePlugin({ root: config.root, @@ -56,7 +56,7 @@ export function createIdResolver( const aliasOnlyPluginContainerMap = new Map< Environment, - IsolatedPluginContainer + EnvironmentPluginContainer >() async function resolveAlias( environment: PluginEnvironment, @@ -65,7 +65,7 @@ export function createIdResolver( ): Promise { let pluginContainer = aliasOnlyPluginContainerMap.get(environment) if (!pluginContainer) { - pluginContainer = await createIsolatedPluginContainer(environment, [ + pluginContainer = await createEnvironmentPluginContainer(environment, [ aliasPlugin({ entries: config.resolve.alias }), // TODO: resolve.alias per environment? ]) aliasOnlyPluginContainerMap.set(environment, pluginContainer) diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 92b6a0acd6e7db..a1a652f33ca3c3 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -96,12 +96,7 @@ export type { SSROptions, SSRTarget, } from './ssr' -export type { - IsolatedPlugin, - IsolatedPluginConstructor, - Plugin, - HookHandler, -} from './plugin' +export type { EnvironmentPlugin, Plugin, HookHandler } from './plugin' export type { Logger, LogOptions, diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 5691a34726285d..18fc7497b6dcbe 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -35,9 +35,9 @@ import { virtualModulePrefix, virtualModuleRE, } from '../utils' -import { resolveIsolatedPlugins } from '../plugin' -import type { IsolatedPluginContainer } from '../server/pluginContainer' -import { createIsolatedPluginContainer } from '../server/pluginContainer' +import { resolveEnvironmentPlugins } from '../plugin' +import type { EnvironmentPluginContainer } from '../server/pluginContainer' +import { createEnvironmentPluginContainer } from '../server/pluginContainer' import { Environment } from '../environment' import type { DevEnvironment } from '../server/environment' import { transformGlobImport } from '../plugins/importMetaGlob' @@ -47,7 +47,7 @@ import { loadTsconfigJsonForFile } from '../plugins/esbuild' export class ScanEnvironment extends Environment { mode = 'scan' as const - get pluginContainer(): IsolatedPluginContainer { + get pluginContainer(): EnvironmentPluginContainer { if (!this._pluginContainer) throw new Error( `${this.name} environment.pluginContainer called before initialized`, @@ -57,15 +57,15 @@ export class ScanEnvironment extends Environment { /** * @internal */ - _pluginContainer: IsolatedPluginContainer | undefined + _pluginContainer: EnvironmentPluginContainer | undefined async init(): Promise { if (this._inited) { return } this._inited = true - this._plugins = await resolveIsolatedPlugins(this) - this._pluginContainer = await createIsolatedPluginContainer( + this._plugins = await resolveEnvironmentPlugins(this) + this._pluginContainer = await createEnvironmentPluginContainer( this, this.plugins, ) @@ -101,7 +101,7 @@ export function devToScanEnvironment( } type ResolveIdOptions = Omit< - Parameters[2], + Parameters[2], 'environment' > diff --git a/packages/vite/src/node/plugin.ts b/packages/vite/src/node/plugin.ts index 4684f3291cb90b..6d6edd63cea265 100644 --- a/packages/vite/src/node/plugin.ts +++ b/packages/vite/src/node/plugin.ts @@ -77,7 +77,7 @@ export interface TransformPluginContext extends RollupTransformPluginContext { * once per each environment allowing users to have completely different plugins * for each of them. The constructor gets the resolved environment after the server * and builder has already been created simplifying config access and cache - * managementfor for environment specific plugins. + * management for for environment specific plugins. * Environment Plugins are closer to regular rollup plugins. They can't define * app level hooks (like config, configResolved, configureServer, etc). */ @@ -103,21 +103,7 @@ type ModifyHookContext = Hook extends { ? ModifyObjectHookContext : ModifyFunctionContext -export interface BasePlugin extends RollupPlugin { - /** - * Enforce plugin invocation tier similar to webpack loaders. Hooks ordering - * is still subject to the `order` property in the hook object. - * - * Plugin invocation order: - * - alias resolution - * - `enforce: 'pre'` plugins - * - vite core plugins - * - normal plugins - * - vite build plugins - * - `enforce: 'post'` plugins - * - vite build post plugins - */ - enforce?: 'pre' | 'post' +export interface EnvironmentPlugin extends RollupPlugin { /** * Perform custom handling of HMR updates. * The handler receives a context containing changed filename, timestamp, a @@ -205,9 +191,7 @@ export interface BasePlugin extends RollupPlugin { renderChunk?: ModifyHookContext['renderChunk'], PluginContext> } -export type IsolatedPlugin = BasePlugin - -export interface Plugin extends BasePlugin { +export interface Plugin extends EnvironmentPlugin { /** * Opt-in this plugin into the shared plugins pipeline. * For backward-compatibility, plugins are re-recreated for each environment @@ -217,6 +201,20 @@ export interface Plugin extends BasePlugin { * @experimental */ sharedDuringBuild?: boolean + /** + * Enforce plugin invocation tier similar to webpack loaders. Hooks ordering + * is still subject to the `order` property in the hook object. + * + * Plugin invocation order: + * - alias resolution + * - `enforce: 'pre'` plugins + * - vite core plugins + * - normal plugins + * - vite build plugins + * - `enforce: 'post'` plugins + * - vite build post plugins + */ + enforce?: 'pre' | 'post' /** * Apply the plugin only for serve or build, or on certain conditions. */ @@ -307,6 +305,12 @@ export interface Plugin extends BasePlugin { * `{ order: 'pre', handler: hook }` */ transformIndexHtml?: IndexHtmlTransform + /** + * Inject per environment plugins after the shared plugin + */ + environmentPlugins?: ( + environment: PluginEnvironment, + ) => PluginOption[] | undefined /** * @deprecated @@ -327,43 +331,35 @@ export type PluginWithRequiredHook = Plugin & { [P in K]: NonNullable } -export type IsolatedPluginConstructor = { - (environment: PluginEnvironment): IsolatedPluginOption - sharedDuringBuild?: boolean -} - -export type MaybeIsolatedPlugin = IsolatedPlugin | false | null | undefined - -export type IsolatedPluginOption = - | MaybeIsolatedPlugin - | IsolatedPluginOption[] - | Promise - -export type MaybePlugin = - | Plugin - | IsolatedPluginConstructor +export type MaybeEnvironmentPlugin = + | EnvironmentPlugin | false | null | undefined +export type EnvironmentPluginOption = + | MaybeEnvironmentPlugin + | EnvironmentPluginOption[] + | Promise + +export type MaybePlugin = Plugin | false | null | undefined + export type PluginOption = | MaybePlugin | PluginOption[] | Promise -export async function resolveIsolatedPlugins( +export async function resolveEnvironmentPlugins( environment: PluginEnvironment, -): Promise { - const resolvedPlugins: IsolatedPlugin[] = [] +): Promise { + const resolvedPlugins: EnvironmentPlugin[] = [] for (const plugin of environment.config.plugins) { - if (typeof plugin === 'function') { - const isolatedPlugin = await plugin(environment) - if (isolatedPlugin) { - const flatPlugins = await asyncFlattenIsolatedPlugin( - environment, - isolatedPlugin, - ) - resolvedPlugins.push(...flatPlugins) + if (plugin.environmentPlugins) { + const environmentPlugins = await plugin.environmentPlugins(environment) + if (environmentPlugins) { + const newPlugins = + await asyncFlattenEnvironmentPlugins(environmentPlugins) + resolvedPlugins.push(...newPlugins) } } else { resolvedPlugins.push(plugin) @@ -372,21 +368,13 @@ export async function resolveIsolatedPlugins( return resolvedPlugins } -async function asyncFlattenIsolatedPlugin( - environment: PluginEnvironment, - plugins: IsolatedPluginOption, -): Promise { - if (!Array.isArray(plugins)) { - plugins = [plugins] - } +async function asyncFlattenEnvironmentPlugins( + plugins: EnvironmentPluginOption[], +): Promise { do { - plugins = ( - await Promise.all( - plugins.map((p: any) => (typeof p === 'function' ? p(environment) : p)), - ) - ) + plugins = ((await Promise.all(plugins)) as any[]) .flat(Infinity) - .filter(Boolean) as IsolatedPluginOption[] - } while (plugins.some((v: any) => v?.then || v?.split)) - return plugins as IsolatedPlugin[] + .filter(Boolean) as EnvironmentPluginOption[] + } while (plugins.some((v: any) => v?.then)) + return plugins as EnvironmentPlugin[] } diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 0ac974a9a175fe..46088a802eed48 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -307,7 +307,7 @@ function handleParseError( */ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms( - config.plugins.filter((plugin) => typeof plugin !== 'function') as Plugin[], + config.plugins, config.logger, ) preHooks.unshift(injectCspNonceMetaTagHook(config)) diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 36d9aff996e06d..6e325118133164 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -2,12 +2,7 @@ import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias' import type { ObjectHook } from 'rollup' import type { PluginHookUtils, ResolvedConfig } from '../config' import { isDepsOptimizerEnabled } from '../config' -import type { - HookHandler, - IsolatedPluginConstructor, - Plugin, - PluginWithRequiredHook, -} from '../plugin' +import type { HookHandler, Plugin, PluginWithRequiredHook } from '../plugin' import { watchPackageDataPlugin } from '../packages' import { getFsUtils } from '../fsUtils' import { jsonPlugin } from './json' @@ -36,7 +31,7 @@ export async function resolvePlugins( prePlugins: Plugin[], normalPlugins: Plugin[], postPlugins: Plugin[], -): Promise<(Plugin | IsolatedPluginConstructor)[]> { +): Promise { const isBuild = config.command === 'build' const isWorker = config.isWorker const buildPlugins = isBuild @@ -115,7 +110,7 @@ export async function resolvePlugins( importAnalysisPlugin(config), // TODO: loadFallbackPlugin(config), ]), - ].filter(Boolean) as (Plugin | IsolatedPluginConstructor)[] + ].filter(Boolean) as Plugin[] } export function createPluginHookUtils( diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index dfdae99daf4b53..566b5d886e790f 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -26,7 +26,6 @@ import { indexHtmlMiddleware } from './server/middlewares/indexHtml' import { notFoundMiddleware } from './server/middlewares/notFound' import { proxyMiddleware } from './server/middlewares/proxy' import { resolveHostname, resolveServerUrls, shouldServeFile } from './utils' -import type { Plugin } from './plugin' import { printServerUrls } from './logger' import { bindCLIShortcuts } from './shortcuts' import type { BindCLIShortcutsOptions } from './shortcuts' @@ -115,13 +114,10 @@ export async function preview( const clientOutDir = config.environments.client.build.outDir ?? config.build.outDir const distDir = path.resolve(config.root, clientOutDir) - const plugins = config.plugins.filter( - (plugin) => typeof plugin !== 'function', - ) as Plugin[] if ( !fs.existsSync(distDir) && // error if no plugins implement `configurePreviewServer` - plugins.every((plugin) => !plugin.configurePreviewServer) && + config.plugins.every((plugin) => !plugin.configurePreviewServer) && // error if called in CLI only. programmatic usage could access `httpServer` // and affect file serving process.argv[1]?.endsWith(path.normalize('bin/vite.js')) && diff --git a/packages/vite/src/node/server/environment.ts b/packages/vite/src/node/server/environment.ts index 253c380b6d8296..60e7b507ca557c 100644 --- a/packages/vite/src/node/server/environment.ts +++ b/packages/vite/src/node/server/environment.ts @@ -16,7 +16,7 @@ import { createDepsOptimizer, createExplicitDepsOptimizer, } from '../optimizer/optimizer' -import { resolveIsolatedPlugins } from '../plugin' +import { resolveEnvironmentPlugins } from '../plugin' import type { DepsOptimizer } from '../optimizer' import { EnvironmentModuleGraph } from './moduleGraph' import type { HMRChannel } from './hmr' @@ -25,10 +25,10 @@ import { transformRequest } from './transformRequest' import type { TransformResult } from './transformRequest' import { ERR_CLOSED_SERVER, - createIsolatedPluginContainer, + createEnvironmentPluginContainer, } from './pluginContainer' import type { RemoteEnvironmentTransport } from './environmentTransport' -import type { IsolatedPluginContainer } from './pluginContainer' +import type { EnvironmentPluginContainer } from './pluginContainer' export interface DevEnvironmentSetup { hot?: false | HMRChannel @@ -52,7 +52,7 @@ export class DevEnvironment extends Environment { */ _ssrRunnerOptions: FetchModuleOptions | undefined - get pluginContainer(): IsolatedPluginContainer { + get pluginContainer(): EnvironmentPluginContainer { if (!this._pluginContainer) throw new Error( `${this.name} environment.pluginContainer called before initialized`, @@ -62,7 +62,7 @@ export class DevEnvironment extends Environment { /** * @internal */ - _pluginContainer: IsolatedPluginContainer | undefined + _pluginContainer: EnvironmentPluginContainer | undefined /** * TODO: should this be public? @@ -165,8 +165,8 @@ export class DevEnvironment extends Environment { return } this._inited = true - this._plugins = await resolveIsolatedPlugins(this) - this._pluginContainer = await createIsolatedPluginContainer( + this._plugins = await resolveEnvironmentPlugins(this) + this._pluginContainer = await createEnvironmentPluginContainer( this, this._plugins, ) diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index f0eeb428a7bdc1..b88a9c4185ff61 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -4,7 +4,6 @@ import MagicString from 'magic-string' import type { SourceMapInput } from 'rollup' import type { Connect } from 'dep-types/connect' import type { DefaultTreeAdapterMap, Token } from 'parse5' -import type { Plugin } from '../../plugin' import type { IndexHtmlTransformHook } from '../../plugins/html' import { addToHTMLProxyCache, @@ -67,8 +66,7 @@ export function createDevHtmlTransformFn( originalUrl?: string, ) => Promise { const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms( - // TODO: interaction between transformIndexHtml and plugin constructors - config.plugins.filter((plugin) => typeof plugin !== 'function') as Plugin[], + config.plugins, config.logger, ) const transformHooks = [ diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index fd798319ae7571..001034d3a094cf 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -61,7 +61,7 @@ import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping' import MagicString from 'magic-string' import type { FSWatcher } from 'chokidar' import colors from 'picocolors' -import type { IsolatedPlugin, Plugin, PluginEnvironment } from '../plugin' +import type { EnvironmentPlugin, Plugin, PluginEnvironment } from '../plugin' import { combineSourcemaps, createDebugger, @@ -103,7 +103,7 @@ export interface PluginContainerOptions { writeFile?: (name: string, source: string | Uint8Array) => void } -export interface IsolatedPluginContainer { +export interface EnvironmentPluginContainer { options: InputOptions buildStart(options: InputOptions): Promise resolveId( @@ -146,11 +146,11 @@ type PluginContext = Omit< * instead of using environment.plugins to allow the creation of different * pipelines working with the same environment (used for createIdResolver). */ -export async function createIsolatedPluginContainer( +export async function createEnvironmentPluginContainer( environment: PluginEnvironment, - plugins: IsolatedPlugin[], + plugins: EnvironmentPlugin[], watcher?: FSWatcher, -): Promise { +): Promise { const { config, logger,