@@ -8,7 +8,7 @@ import type { Alias, AliasOptions } from 'types/alias'
88import aliasPlugin from '@rollup/plugin-alias'
99import { build } from 'esbuild'
1010import type { RollupOptions } from 'rollup'
11- import type { Plugin } from './plugin'
11+ import type { HookHandler , Plugin } from './plugin'
1212import type {
1313 BuildOptions ,
1414 RenderBuiltAssetUrl ,
@@ -33,7 +33,11 @@ import {
3333 normalizeAlias ,
3434 normalizePath
3535} from './utils'
36- import { resolvePlugins } from './plugins'
36+ import {
37+ createPluginHookUtils ,
38+ getSortedPluginsByHook ,
39+ resolvePlugins
40+ } from './plugins'
3741import type { ESBuildOptions } from './plugins/esbuild'
3842import {
3943 CLIENT_ENTRY ,
@@ -289,7 +293,7 @@ export interface LegacyOptions {
289293 buildSsrCjsExternalHeuristics ?: boolean
290294}
291295
292- export interface ResolveWorkerOptions {
296+ export interface ResolveWorkerOptions extends PluginHookUtils {
293297 format : 'es' | 'iife'
294298 plugins : Plugin [ ]
295299 rollupOptions : RollupOptions
@@ -334,9 +338,16 @@ export type ResolvedConfig = Readonly<
334338 worker : ResolveWorkerOptions
335339 appType : AppType
336340 experimental : ExperimentalOptions
337- }
341+ } & PluginHookUtils
338342>
339343
344+ export interface PluginHookUtils {
345+ getSortedPlugins : ( hookName : keyof Plugin ) => Plugin [ ]
346+ getSortedPluginHooks : < K extends keyof Plugin > (
347+ hookName : K
348+ ) => NonNullable < HookHandler < Plugin [ K ] > > [ ]
349+ }
350+
340351export type ResolveFn = (
341352 id : string ,
342353 importer ?: string ,
@@ -431,9 +442,11 @@ export async function resolveConfig(
431442
432443 // run config hooks
433444 const userPlugins = [ ...prePlugins , ...normalPlugins , ...postPlugins ]
434- for ( const p of userPlugins ) {
435- if ( p . config ) {
436- const res = await p . config ( config , configEnv )
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 )
437450 if ( res ) {
438451 config = mergeConfig ( config , res )
439452 }
@@ -598,9 +611,11 @@ export async function resolveConfig(
598611 ...workerNormalPlugins ,
599612 ...workerPostPlugins
600613 ]
601- for ( const p of workerUserPlugins ) {
602- if ( p . config ) {
603- const res = await p . config ( workerConfig , configEnv )
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 )
604619 if ( res ) {
605620 workerConfig = mergeConfig ( workerConfig , res )
606621 }
@@ -609,7 +624,9 @@ export async function resolveConfig(
609624 const resolvedWorkerOptions : ResolveWorkerOptions = {
610625 format : workerConfig . worker ?. format || 'iife' ,
611626 plugins : [ ] ,
612- rollupOptions : workerConfig . worker ?. rollupOptions || { }
627+ rollupOptions : workerConfig . worker ?. rollupOptions || { } ,
628+ getSortedPlugins : undefined ! ,
629+ getSortedPluginHooks : undefined !
613630 }
614631
615632 const resolvedConfig : ResolvedConfig = {
@@ -660,7 +677,9 @@ export async function resolveConfig(
660677 importGlobRestoreExtension : false ,
661678 hmrPartialAccept : false ,
662679 ...config . experimental
663- }
680+ } ,
681+ getSortedPlugins : undefined ! ,
682+ getSortedPluginHooks : undefined !
664683 }
665684 const resolved : ResolvedConfig = {
666685 ...config ,
@@ -673,31 +692,34 @@ export async function resolveConfig(
673692 normalPlugins ,
674693 postPlugins
675694 )
695+ Object . assign ( resolved , createPluginHookUtils ( resolved . plugins ) )
676696
677697 const workerResolved : ResolvedConfig = {
678698 ...workerConfig ,
679699 ...resolvedConfig ,
680700 isWorker : true ,
681701 mainConfig : resolved
682702 }
683-
684703 resolvedConfig . worker . plugins = await resolvePlugins (
685704 workerResolved ,
686705 workerPrePlugins ,
687706 workerNormalPlugins ,
688707 workerPostPlugins
689708 )
709+ Object . assign (
710+ resolvedConfig . worker ,
711+ createPluginHookUtils ( resolvedConfig . worker . plugins )
712+ )
690713
691714 // call configResolved hooks
692- await Promise . all (
693- userPlugins
694- . map ( ( p ) => p . configResolved ?.( resolved ) )
695- . concat (
696- resolvedConfig . worker . plugins . map ( ( p ) =>
697- p . configResolved ?.( workerResolved )
698- )
699- )
700- )
715+ await Promise . all ( [
716+ ...resolved
717+ . getSortedPluginHooks ( 'configResolved' )
718+ . map ( ( hook ) => hook ( resolved ) ) ,
719+ ...resolvedConfig . worker
720+ . getSortedPluginHooks ( 'configResolved' )
721+ . map ( ( hook ) => hook ( workerResolved ) )
722+ ] )
701723
702724 // validate config
703725
0 commit comments