-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support object style hooks #9634
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0c12134
feat: support object style hooks
antfu a6b80ef
chore: update
antfu bb4ae28
chore: refactor
antfu 60fd590
feat: support object hooks for `configureServer` and `resolvedConfig`
antfu 8c7389b
feat: support object hooks for `config` and `handleHotUpdate`
antfu ec188a1
chore: fix types
antfu c8ff1df
chore: Merge remote-tracking branch 'origin/main' into feat/plugin-ob…
antfu b65e10a
chore: update
antfu f7748d1
chore: merge
antfu 31a4776
chore: upgrade to rollup ^2.78.0
antfu 301484f
fix: pluginContainer
antfu 8bdfc57
chore: cleanup
antfu d2f5229
fix: worker
antfu 9ab44f0
chore: lint
antfu 0b4d75f
fix: test https://github.com/rollup/rollup/pull/4588
antfu 6cc4b14
chore: limit rollup range
antfu 90f0185
chore: update
antfu 51235f5
chore: merge main
patak-dev 297f2c9
chore: merge main
patak-dev 55e342a
chore: update lock
patak-dev 31c5593
chore: redo lock
patak-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import type { Alias, AliasOptions } from 'types/alias' | |
import aliasPlugin from '@rollup/plugin-alias' | ||
import { build } from 'esbuild' | ||
import type { RollupOptions } from 'rollup' | ||
import type { Plugin } from './plugin' | ||
import type { HookHandler, Plugin } from './plugin' | ||
import type { | ||
BuildOptions, | ||
RenderBuiltAssetUrl, | ||
|
@@ -33,7 +33,11 @@ import { | |
normalizeAlias, | ||
normalizePath | ||
} from './utils' | ||
import { resolvePlugins } from './plugins' | ||
import { | ||
createPluginHookUtils, | ||
getSortedPluginsByHook, | ||
resolvePlugins | ||
} from './plugins' | ||
import type { ESBuildOptions } from './plugins/esbuild' | ||
import { | ||
CLIENT_ENTRY, | ||
|
@@ -289,7 +293,7 @@ export interface LegacyOptions { | |
buildSsrCjsExternalHeuristics?: boolean | ||
} | ||
|
||
export interface ResolveWorkerOptions { | ||
export interface ResolveWorkerOptions extends PluginHookUtils { | ||
format: 'es' | 'iife' | ||
plugins: Plugin[] | ||
rollupOptions: RollupOptions | ||
|
@@ -334,9 +338,16 @@ export type ResolvedConfig = Readonly< | |
worker: ResolveWorkerOptions | ||
appType: AppType | ||
experimental: ExperimentalOptions | ||
} | ||
} & PluginHookUtils | ||
> | ||
|
||
export interface PluginHookUtils { | ||
getSortedPlugins: (hookName: keyof Plugin) => Plugin[] | ||
getSortedPluginHooks: <K extends keyof Plugin>( | ||
hookName: K | ||
) => NonNullable<HookHandler<Plugin[K]>>[] | ||
} | ||
|
||
export type ResolveFn = ( | ||
id: string, | ||
importer?: string, | ||
|
@@ -431,9 +442,11 @@ export async function resolveConfig( | |
|
||
// run config hooks | ||
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins] | ||
for (const p of userPlugins) { | ||
if (p.config) { | ||
const res = await p.config(config, configEnv) | ||
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) | ||
} | ||
|
@@ -598,9 +611,11 @@ export async function resolveConfig( | |
...workerNormalPlugins, | ||
...workerPostPlugins | ||
] | ||
for (const p of workerUserPlugins) { | ||
if (p.config) { | ||
const res = await p.config(workerConfig, configEnv) | ||
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) | ||
} | ||
|
@@ -609,7 +624,9 @@ export async function resolveConfig( | |
const resolvedWorkerOptions: ResolveWorkerOptions = { | ||
format: workerConfig.worker?.format || 'iife', | ||
plugins: [], | ||
rollupOptions: workerConfig.worker?.rollupOptions || {} | ||
rollupOptions: workerConfig.worker?.rollupOptions || {}, | ||
getSortedPlugins: undefined!, | ||
getSortedPluginHooks: undefined! | ||
} | ||
|
||
const resolvedConfig: ResolvedConfig = { | ||
|
@@ -660,7 +677,9 @@ export async function resolveConfig( | |
importGlobRestoreExtension: false, | ||
hmrPartialAccept: false, | ||
...config.experimental | ||
} | ||
}, | ||
getSortedPlugins: undefined!, | ||
getSortedPluginHooks: undefined! | ||
} | ||
const resolved: ResolvedConfig = { | ||
...config, | ||
|
@@ -673,31 +692,34 @@ export async function resolveConfig( | |
normalPlugins, | ||
postPlugins | ||
) | ||
Object.assign(resolved, createPluginHookUtils(resolved.plugins)) | ||
|
||
const workerResolved: ResolvedConfig = { | ||
...workerConfig, | ||
...resolvedConfig, | ||
isWorker: true, | ||
mainConfig: resolved | ||
} | ||
|
||
resolvedConfig.worker.plugins = await resolvePlugins( | ||
workerResolved, | ||
workerPrePlugins, | ||
workerNormalPlugins, | ||
workerPostPlugins | ||
) | ||
Object.assign( | ||
resolvedConfig.worker, | ||
createPluginHookUtils(resolvedConfig.worker.plugins) | ||
) | ||
|
||
// call configResolved hooks | ||
await Promise.all( | ||
userPlugins | ||
.map((p) => p.configResolved?.(resolved)) | ||
.concat( | ||
resolvedConfig.worker.plugins.map((p) => | ||
p.configResolved?.(workerResolved) | ||
) | ||
) | ||
) | ||
await Promise.all([ | ||
...resolved | ||
.getSortedPluginHooks('configResolved') | ||
.map((hook) => hook(resolved)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
...resolvedConfig.worker | ||
.getSortedPluginHooks('configResolved') | ||
.map((hook) => hook(workerResolved)) | ||
]) | ||
|
||
// validate config | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is worth extracting a helper for these three functions to avoid repeating the function or object handling?