Skip to content

Commit

Permalink
refactor: config hook helper function (#9982)
Browse files Browse the repository at this point in the history
  • Loading branch information
musicq committed Sep 5, 2022
1 parent 855f2f0 commit 9c1be10
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions packages/vite/src/node/config.ts
Expand Up @@ -442,16 +442,7 @@ export async function resolveConfig(

// run config hooks
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins]
for (const p of getSortedPluginsByHook('config', userPlugins)) {
const hook = p.config
const handler = hook && 'handler' in hook ? hook.handler : hook
if (handler) {
const res = await handler(config, configEnv)
if (res) {
config = mergeConfig(config, res)
}
}
}
config = await runConfigHook(config, userPlugins, configEnv)

if (process.env.VITE_TEST_WITHOUT_PLUGIN_COMMONJS) {
config = mergeConfig(config, {
Expand Down Expand Up @@ -611,16 +602,7 @@ export async function resolveConfig(
...workerNormalPlugins,
...workerPostPlugins
]
for (const p of getSortedPluginsByHook('config', workerUserPlugins)) {
const hook = p.config
const handler = hook && 'handler' in hook ? hook.handler : hook
if (handler) {
const res = await handler(workerConfig, configEnv)
if (res) {
workerConfig = mergeConfig(workerConfig, res)
}
}
}
workerConfig = await runConfigHook(workerConfig, workerUserPlugins, configEnv)
const resolvedWorkerOptions: ResolveWorkerOptions = {
format: workerConfig.worker?.format || 'iife',
plugins: [],
Expand Down Expand Up @@ -1089,6 +1071,27 @@ async function loadConfigFromBundledFile(
}
}

async function runConfigHook(
config: InlineConfig,
plugins: Plugin[],
configEnv: ConfigEnv
): Promise<InlineConfig> {
let conf = config

for (const p of getSortedPluginsByHook('config', plugins)) {
const hook = p.config
const handler = hook && 'handler' in hook ? hook.handler : hook
if (handler) {
const res = await handler(conf, configEnv)
if (res) {
conf = mergeConfig(conf, res)
}
}
}

return conf
}

export function getDepOptimizationConfig(
config: ResolvedConfig,
ssr: boolean
Expand Down

0 comments on commit 9c1be10

Please sign in to comment.