Skip to content

Commit 9c1be10

Browse files
authored
refactor: config hook helper function (#9982)
1 parent 855f2f0 commit 9c1be10

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

packages/vite/src/node/config.ts

+23-20
Original file line numberDiff line numberDiff line change
@@ -442,16 +442,7 @@ export async function resolveConfig(
442442

443443
// run config hooks
444444
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins]
445-
for (const p of getSortedPluginsByHook('config', userPlugins)) {
446-
const hook = p.config
447-
const handler = hook && 'handler' in hook ? hook.handler : hook
448-
if (handler) {
449-
const res = await handler(config, configEnv)
450-
if (res) {
451-
config = mergeConfig(config, res)
452-
}
453-
}
454-
}
445+
config = await runConfigHook(config, userPlugins, configEnv)
455446

456447
if (process.env.VITE_TEST_WITHOUT_PLUGIN_COMMONJS) {
457448
config = mergeConfig(config, {
@@ -611,16 +602,7 @@ export async function resolveConfig(
611602
...workerNormalPlugins,
612603
...workerPostPlugins
613604
]
614-
for (const p of getSortedPluginsByHook('config', workerUserPlugins)) {
615-
const hook = p.config
616-
const handler = hook && 'handler' in hook ? hook.handler : hook
617-
if (handler) {
618-
const res = await handler(workerConfig, configEnv)
619-
if (res) {
620-
workerConfig = mergeConfig(workerConfig, res)
621-
}
622-
}
623-
}
605+
workerConfig = await runConfigHook(workerConfig, workerUserPlugins, configEnv)
624606
const resolvedWorkerOptions: ResolveWorkerOptions = {
625607
format: workerConfig.worker?.format || 'iife',
626608
plugins: [],
@@ -1089,6 +1071,27 @@ async function loadConfigFromBundledFile(
10891071
}
10901072
}
10911073

1074+
async function runConfigHook(
1075+
config: InlineConfig,
1076+
plugins: Plugin[],
1077+
configEnv: ConfigEnv
1078+
): Promise<InlineConfig> {
1079+
let conf = config
1080+
1081+
for (const p of getSortedPluginsByHook('config', plugins)) {
1082+
const hook = p.config
1083+
const handler = hook && 'handler' in hook ? hook.handler : hook
1084+
if (handler) {
1085+
const res = await handler(conf, configEnv)
1086+
if (res) {
1087+
conf = mergeConfig(conf, res)
1088+
}
1089+
}
1090+
}
1091+
1092+
return conf
1093+
}
1094+
10921095
export function getDepOptimizationConfig(
10931096
config: ResolvedConfig,
10941097
ssr: boolean

0 commit comments

Comments
 (0)