Skip to content
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

Add "presets" option to Vite config #2500

Closed
aleclarson opened this issue Mar 13, 2021 · 5 comments
Closed

Add "presets" option to Vite config #2500

aleclarson opened this issue Mar 13, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@aleclarson
Copy link
Member

Is your feature request related to a problem? Please describe.
Currently, plugins cannot add other plugins. #2484

Describe the solution you'd like
Add presets array option, which calls mergeConfig on each preset before plugins are processed.

The user config is merged after any presets.

@aleclarson aleclarson added the enhancement New feature or request label Mar 13, 2021
@patak-dev
Copy link
Member

Some extra context. If the preset needs to be configured, frameworks and libs tools like tailwind or windicss can provide a factory to build the preset, following the same pattern as plugins.

Example with presets

export default {
  presets: [ preact({ ... }), windiCSS({ ... }) ],
  // user options
})

Preact now provides withPreact() that uses mergeConfig internally

export default withPreact( withWindiCSS({
  // user options
})

SvelteKit is also talking about providing a Preset

@antfu was proposing extending defineConfig to directly take several configs/presets (and use mergeConfig internally)

export default defineConfig(
  preact({ ... }), 
  windiCSS({ ... }),
  {
    // user options
  }
})

(he used the name defineConfigWithPresets, I think it could remain the same)

I think that both adding a presets option, or extending defineConfig are better for users and easier for maintainers than the withXXX utility. And FWIW, I find @antfu's idea easier to read.

@bluwy
Copy link
Member

bluwy commented Mar 13, 2021

FWIW, a preset can also be written as a plugin. It can add other plugins by having the preset function to return an array of plugins instead, e.g. https://gist.github.com/bluwy/18b8d103cea5f823a95c4ce39d36a048. Vite will flatten the plugins before resolving them.

Though it doesn't look as simple as just exporting a config object/function.

@marvinhagemeister
Copy link
Contributor

@bluwy TIL, didn't know that it was possible to return arrays there!

@atzcl
Copy link

atzcl commented Mar 14, 2021

on this basis, hope to support async export of userConfig

typeof userConfig === 'function' ? userConfig(configEnv) : userConfig

@yyx990803
Copy link
Member

What @bluwy pointed out: Vite actually accepts nested array of plugins so you can do:

const myPlugin = () => [subPluginA(), subPluginB()]

export default {
  plugins: [myPlugin()]
}

This is essentially what @vitejs/plugin-legacy is doing (it contains 3 internal sub-plugins). I'm not sure if the concept of presets is really necessary when you can just do that.

marvinhagemeister added a commit to preactjs/preset-vite that referenced this issue Mar 15, 2021
Releasing this preset to the public sparked a discussions on
how presets should be done in vite. Presumably we were the first
project to do so.

It turns out that plugins can be an array of plugins. This makes the
config more composable and we can avoid using a somewhat internal function to
merge configs.

See this issue for more background: vitejs/vite#2500
marvinhagemeister added a commit to preactjs/preset-vite that referenced this issue Mar 15, 2021
Releasing this preset to the public sparked a discussions on
how presets should be done in vite. Presumably we were the first
project to do so.

It turns out that plugins can be an array of plugins. This makes the
config more composable and we can avoid using a somewhat internal function to
merge configs.

See this issue for more background: vitejs/vite#2500
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants