vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.1k stars 5.91k forks source link

Allow options to be returned from a callback #15783

Open Tofandel opened 5 months ago

Tofandel commented 5 months ago

Description

When running vite without a config file and passing options directly to build a server restart will not reload the config and plugins (and how could it?)

It would be nice to add the ability to pass the config as a callback which returns it and on server.restart() can reload the config from the callback

Suggested solution

Accept a function as a InlineConfig and call it in resolveConfig

Alternative

Accept a function as the option configFile in InlineConfig

Additional context

https://github.com/vitejs/vite/blob/775bb5026ee1d7e15b75c8829e7f528c1b26c493/packages/vite/src/node/config.ts#L401-L440 https://github.com/vitejs/vite/blob/775bb5026ee1d7e15b75c8829e7f528c1b26c493/packages/vite/src/node/config.ts#L335-L338

Validations

bluwy commented 5 months ago

We've discussed the feature in https://github.com/vitejs/vite/pull/10326 before. I think you can workaround this by creating a Vite plugin with a config() hook that assigns the config instead?

Tofandel commented 5 months ago

@bluwy But then it puts this burden on all the plugins instead of the vite builder which would be able to reload everything at once

This would mean all the plugins need to change their implementation whereas this proposal simply would allow for a function as configFile which would achieve a better result and DX when only the build tool using vite need to change their implementation

I'd be willing to make a PR

bluwy commented 5 months ago

I don't understand why all plugins have to change their implementation. Wouldn't it be something like this?

createServer({
  inlineConfig: {
    plugins: [
      {
        name: 'config',
        config() {
          return { /* assign your config here */ }
         }
      }
    ]
  }
})

You'd miss out on the inline-config-only properties like configFile and envFile of course. Not sure if you're using that, but it would be strange for the config files and env files to change on every restart.

Tofandel commented 5 months ago

From what I tried the config hook is not able to assign plugins, it's already too late so this is not a viable workaround

It would also be a plugin reassigning plugins so there is some paradox here

plugins: [
      {
        name: 'config',
        config() {
          return { plugins: [/* Any plugin added here will never run */] }
         }
      }
    ]

The issue is very specific to plugins not reloading when changing env files or config files

The burden on the vite plugins would be with the buildStart hook that you referenced in the PR