Replies: 6 comments 31 replies
-
Thanks for the writing down the proposal @bluwy! It would be great for others to check things out, maybe even try to implement some of this in a POC as I made deeps mental cliffs now with the other design and it is a bit hard to get out of them and check it as a blank slate. I think there are some things that we seems to agree on here that is good to enumerate again as this is the bulk of what we are introducing in Vite 6 as experimental:
So to guide others, this proposal affects how to configure the environments and an alternative for API for environment instances. |
Beta Was this translation helpful? Give feedback.
-
About:
We evaluated this idea and decided that having the One of the ideas we checked out was using top level keys with a namespace for environments. For example, Nuxt uses top level export default defineConfig({
$client: { ... },
$ssr: { ... },
$edge: { ... },
}) One of the ideas for Note that this could also apply to your proposal. Given a good way to redefine how the current |
Beta Was this translation helpful? Give feedback.
-
About having the top level keys refer to the client environment. We first implemented a similar design. I can't find where we did the switch now in the sea of commits. I think it is confusing that some top level options are client only, and some of them affect all environments acting as a default. We currently also have a mix of these though, and people are fine with it. If we think that we should have certain top level options configure the client environment only (a good case may be We would need to document for each config option if it is client-only or a default for all environments. And this would be orthogonal to the option being shared or per-environment. We also would need to treat these client-only options in a different way during config resolution. This may be good to explore in a PR maybe. That being said, this is possible with the current design, it is just another concept users needs to be aware of. If we end up going with something like |
Beta Was this translation helpful? Give feedback.
-
@bluwy says:
I didn't get to this yet as it was unrelated to the other bits in the proposal. I don't think an environment should stop users from configuring things like function opinionatedEdgeEnvironment(config: PartialUserConfig) {
return {
...config,
// some options that can't be overriden
}
} export default defineConfig({
$edge: opinionatedEdgeEnvironment({
// only some options are available, typing works fine
})
}
This also works fine right now function opinionatedEdgeEnvironment(config: ExtendedUserConfig) {
const { a, b, c, ...userConfig } = config
return {
...userConfig,
}
}
Environments are already using the pattern above if I understand correctly, so they see the value at least in extending with their own options. |
Beta Was this translation helpful? Give feedback.
-
What if it's both? The proposal is to have declare module 'vite' {
interface ViteDevEnvironments {
rsc: {
type: RunnableDevEnvironment
config: NodeEnvironmentOptions
resolvedConfig: ResolvedNodeEnvironmentOptions
}
worker: {
type: FetchableDevEnvironment
config: WorkerdEnvironmentOptions
resolvedConfig: ResolvedWorkerdEnvironmentOptions
}
}
} Another thing is
I don't think this should be a topic of discussion around the Environment API. Vitest can adapt to any APIs as long as they provide module graphs.
With the Generally speaking, I like the goals that were set in this proposal. I like separating the environment options into a top-level property. I also like moving the environment creating specific properties (like
This doesn't make a lot of sense to me. If plugin authors populate the export default {
plugins: [miniflare()] // sets `create` in the `config` hook
}
The "environments" plugin-like API feels excessive to me, personally. It just duplicates plugin hooks. Vite already has plugins, and I am satisfied with how the environment is created right now. Moving around properties might be required for better TS support, but that's it. Proposed life-cycle separation seems harder to work with than a single |
Beta Was this translation helpful? Give feedback.
-
First of all, I think an idea is invaluable, no matter when it is submitted, so thanks for writing!
I guess it's difficult to type
I think the
In the current env API, there isn't a way to set an option of environments from frameworks (or other plugins). This is a known issue for plugins as well. This is something I think it'd nice to be solved. |
Beta Was this translation helpful? Give feedback.
-
Exploration of a different API design for Vite's environment API and different tradeoffs. In typical fashion, I submit ideas usually too late 😅 So I don't expect that we're integrating or be considering any of these soon.
Goals
API design
Vite config
Click to expand
Most Vite users are spending most of the time interacting with the Vite config. It should be additive, approachable, and simple to configure the options.
Vite stays browser-focused, with all top-level options reflecting for browsers first. Some options may be re-used for other environments (like SSR), and some may not make sense for them, which is fine. The environment specific options should be kept under the environment key.
Here's an example without environment API yet (as an exercise to clean up the API to make way for environment API later):
Vite config with environment API
Click to expand
Vite config & environment API types
Click to expand
Environment API libraries (from runtime authors) can return a config type that consumers can use. Consumers like framework or plugin authors can add something like this:
The benefit with this approach is that framework or plugin authors can define what they expect to exist and define it themselves. They also get stricter types ootb, for example:
polyfillNode
, can also be defined in the config.Create an environment (runtime authors)
Click to expand
An environment instance looks like a plugin with certain properties that act like hooks.
Using en environment (framework & plugin authors)
Click to expand
As showed in Vite config with environment API section, additional environments can be configured by framework or plugin authors by adding to the
environments
key. If an existing one exists during config merging, it'll error.In most cases, framework can access via
server.environments.ssr.import('...')
like before.For plugin authors, I quite like the current
this.environment.<env-name>
design. The discussions around per-environment plugin should also be unaffected by this proposal. The only change with this proposal is thatconfigEnvironment
is likely not needed anymore as environment instances have theconfigResolve
hook.Summary
ssr
andworker
options.Caveats or uncovered topics
An obvious pitfall is that extended environments occupy top-level config keys. We could namespace them, but I think it defeats the purpose and ease of use.
I personally don't think it's a big issue as we don't add new top-level options often. We can mark this not covered by semver, but still check the ecosystem if there's certain keys in use that're popular if we need to add a new option.
Compatibility with Vitest. Since I worked on frameworks more, my bias here probably won't fit well with how Vitest will integrate. I'm open to extending certain parts of the API to make it work better with Vitest.
However, I think we should consider frameworks as our main target audience given the ecosystem, and if Vitest need to apply specific hacks or workaround, I think it should be acceptable.
Beta Was this translation helpful? Give feedback.
All reactions