Replies: 2 comments 4 replies
-
I like the idea. Option 2 looks nice, but it also isn't the same because awaiting will be sequential or users will be forced to use Do you know what was the rationale to avoid this in Rollup? (@lukastaegert, in case you are around maybe you recall a discussion, I searched the rollup issues but I could find one) |
Beta Was this translation helpful? Give feedback.
-
Similar to Alternative 2, it could be done like this: export default defineConfig(async () => ({
plugins: [await myPlugin()]
})) But allowing promise in |
Beta Was this translation helpful? Give feedback.
-
Update: This is now supported in Vite 3 (#8574)
Proposal
Allow passing promises to the
plugins
config.Changing the
plugins
type to:(Plugin | Plugin[] | Promise<Plugin | Plugin[]>)[]
Reason
For plugins that require running async code on initialization, e.g. importing a config file, not having async support makes it hard to arrange the code/logic of the plugin. The common workaround is to run the async code during the
config
orconfigResolved
hook, though it makes it hard to pass the async result around.Example:
Code organization-wise, being able to asynchronously load the config on initialization phase simplifies the flow too.
Alternatives
A solution is to use
Object.assign
for themy_config
to preserve the reference, though it isn't as elegant and has caveats, like being able to accessmy_config
after we know theconfig
hook has ran.Use top-level await when initializing plugins:
Though this only works in ESM.
Notes
esbuild
's plugin object has asetup
property that can be an async function:Beta Was this translation helpful? Give feedback.
All reactions