Skip to content

Commit

Permalink
Revert "feat: call configResolved hook on new plugins"
Browse files Browse the repository at this point in the history
This reverts commit 0e552f5.
  • Loading branch information
patak-dev committed Nov 5, 2024
1 parent 0e552f5 commit 51c3c07
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 59 deletions.
7 changes: 2 additions & 5 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ import {
normalizePath,
partialEncodeURIPath,
} from './utils'
import {
getHookHandler,
perEnvironmentPlugin,
resolveEnvironmentPlugins,
} from './plugin'
import { perEnvironmentPlugin, resolveEnvironmentPlugins } from './plugin'
import { manifestPlugin } from './plugins/manifest'
import type { Logger } from './logger'
import { dataURIPlugin } from './plugins/dataUri'
Expand All @@ -70,6 +66,7 @@ import {
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
import { mergeConfig } from './publicUtils'
import { webWorkerPostPlugin } from './plugins/worker'
import { getHookHandler } from './plugins'
import {
BaseEnvironment,
getDefaultResolvedEnvironmentOptions,
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
PluginOption,
PluginWithRequiredHook,
} from './plugin'
import { getHookHandler, getSortedPluginsByHook } from './plugin'
import type {
BuildEnvironmentOptions,
BuilderOptions,
Expand Down Expand Up @@ -67,7 +66,12 @@ import {
normalizeAlias,
normalizePath,
} from './utils'
import { createPluginHookUtils, resolvePlugins } from './plugins'
import {
createPluginHookUtils,
getHookHandler,
getSortedPluginsByHook,
resolvePlugins,
} from './plugins'
import type { ESBuildOptions } from './plugins/esbuild'
import type {
EnvironmentResolveOptions,
Expand Down
54 changes: 6 additions & 48 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,24 +333,18 @@ export async function resolveEnvironmentPlugins(
environment: PartialEnvironment,
): Promise<Plugin[]> {
const environmentPlugins: Plugin[] = []
const topLevelConfig = environment.getTopLevelConfig()
for (const plugin of topLevelConfig.plugins) {
for (const plugin of environment.getTopLevelConfig().plugins) {
if (plugin.applyToEnvironment) {
const applied = await plugin.applyToEnvironment(environment)
if (!applied) {
continue
}
if (applied !== true) {
const newPlugins = (await asyncFlatten(arraify(applied))).filter(
Boolean,
) as Plugin[]
for (const plugin of getSortedPluginsByHook(
'configResolved',
newPlugins,
)) {
getHookHandler(plugin.configResolved)(topLevelConfig)
}
environmentPlugins.push(...newPlugins)
environmentPlugins.push(
...((await asyncFlatten(arraify(applied))).filter(
Boolean,
) as Plugin[]),
)
continue
}
}
Expand All @@ -373,39 +367,3 @@ export function perEnvironmentPlugin(
applyToEnvironment,
}
}

export function getSortedPluginsByHook<K extends keyof Plugin>(
hookName: K,
plugins: readonly Plugin[],
): PluginWithRequiredHook<K>[] {
const sortedPlugins: Plugin[] = []
// Use indexes to track and insert the ordered plugins directly in the
// resulting array to avoid creating 3 extra temporary arrays per hook
let pre = 0,
normal = 0,
post = 0
for (const plugin of plugins) {
const hook = plugin[hookName]
if (hook) {
if (typeof hook === 'object') {
if (hook.order === 'pre') {
sortedPlugins.splice(pre++, 0, plugin)
continue
}
if (hook.order === 'post') {
sortedPlugins.splice(pre + normal + post++, 0, plugin)
continue
}
}
sortedPlugins.splice(pre + normal++, 0, plugin)
}
}

return sortedPlugins as PluginWithRequiredHook<K>[]
}

export function getHookHandler<T extends ObjectHook<Function>>(
hook: T,
): HookHandler<T> {
return (typeof hook === 'object' ? hook.handler : hook) as HookHandler<T>
}
38 changes: 37 additions & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias'
import type { ObjectHook } from 'rollup'
import type { PluginHookUtils, ResolvedConfig } from '../config'
import { isDepOptimizationDisabled } from '../optimizer'
import type { HookHandler, Plugin, PluginWithRequiredHook } from '../plugin'
import { getHookHandler, getSortedPluginsByHook } from '../plugin'
import { watchPackageDataPlugin } from '../packages'
import { jsonPlugin } from './json'
import { resolvePlugin } from './resolve'
Expand Down Expand Up @@ -138,6 +138,42 @@ export function createPluginHookUtils(
}
}

export function getSortedPluginsByHook<K extends keyof Plugin>(
hookName: K,
plugins: readonly Plugin[],
): PluginWithRequiredHook<K>[] {
const sortedPlugins: Plugin[] = []
// Use indexes to track and insert the ordered plugins directly in the
// resulting array to avoid creating 3 extra temporary arrays per hook
let pre = 0,
normal = 0,
post = 0
for (const plugin of plugins) {
const hook = plugin[hookName]
if (hook) {
if (typeof hook === 'object') {
if (hook.order === 'pre') {
sortedPlugins.splice(pre++, 0, plugin)
continue
}
if (hook.order === 'post') {
sortedPlugins.splice(pre + normal + post++, 0, plugin)
continue
}
}
sortedPlugins.splice(pre + normal++, 0, plugin)
}
}

return sortedPlugins as PluginWithRequiredHook<K>[]
}

export function getHookHandler<T extends ObjectHook<Function>>(
hook: T,
): HookHandler<T> {
return (typeof hook === 'object' ? hook.handler : hook) as HookHandler<T>
}

// Same as `@rollup/plugin-alias` default resolver, but we attach additional meta
// if we can't resolve to something, which will error in `importAnalysis`
export const viteAliasCustomResolver: ResolverFunction = async function (
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 @@ -7,7 +7,7 @@ import type { RollupError } from 'rollup'
import { CLIENT_DIR } from '../constants'
import { createDebugger, normalizePath } from '../utils'
import type { InferCustomEventPayload, ViteDevServer } from '..'
import { getHookHandler } from '../plugin'
import { getHookHandler } from '../plugins'
import { isCSSRequest } from '../plugins/css'
import { isExplicitImportRequired } from '../plugins/importAnalysis'
import { getEnvFilesForMode } from '../env'
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ import {
timeFrom,
} from '../utils'
import { FS_PREFIX } from '../constants'
import { createPluginHookUtils } from '../plugins'
import { getHookHandler } from '../plugin'
import { createPluginHookUtils, getHookHandler } from '../plugins'
import { cleanUrl, unwrapId } from '../../shared/utils'
import type { PluginHookUtils } from '../config'
import type { Environment } from '../environment'
Expand Down

0 comments on commit 51c3c07

Please sign in to comment.