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

refactor: top level keys for environments (and .environments array) #18439

Closed
wants to merge 8 commits into from

Conversation

patak-dev
Copy link
Member

@patak-dev patak-dev commented Oct 23, 2024

Description

Inspired by Nuxt $production/$development top level keys. See environment overrides.

Instead of:

export default defineConfig({
  environments: {
    client: {},
    ssr: {},
    edge: {},
  }
})

this changes the API to use top level keys prefixed by $

export default defineConfig({
  $client: {},
  $ssr: {},
  $edge: {},
})

The idea is avoiding the extra nesting as config options become to verbose:

config.environments.client.dev.sourcemap
// vs
config.$client.dev.sourcemap

The same environment top level keys are mapped to ResolvedConfig, ViteDevServer, ViteBuilder

const { render } = await vite.environments.ssr.import(url)
// vs
const { render } = await vite.$ssr.import(url)
// making it closer to
const { render } = await vite.ssrLoadModule(url)

resolvedConfig.environments, viteDevServer.environments, builder.environments is now a Env[] instead of a Record<string,Env>. The object form is no longer needed now that we have access at the top level, and the list simplifies iteration (no need for Object.values(), Object.keys()). For example:

buildApp: async (builder) => {
  return Promise.all(
    builder.environments.map((environment) => builder.build(environment)),
  )
},
// vs
buildApp: async (builder) => {
  return Promise.all(
    Object.values(builder.environments).map((environment) => builder.build(environment)),
  )
},

Built on top of #18426 but moving .environments to be an array. It is generally a lot easier to use. A lot of Object.values() are removed.

Proposal triggered by this discussion #18415 (comment)

@GalacticHypernova

This comment was marked as off-topic.

@patak-dev

This comment was marked as off-topic.

@GalacticHypernova

This comment was marked as off-topic.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice. The only worry I have is forgetting to sync environments and $foos.

Comment on lines 14 to +17
config: ResolvedConfig,
): ResolvedEnvironmentOptions {
return {
name: '$',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name should be passed from the argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name here is a dummy value for defaults. I don't think it would help to pass it from the two places where this function is called. Ideally, it shouldn't be there, but I don't want to make it nullable.

@patak-dev
Copy link
Member Author

The only worry I have is forgetting to sync environments and $foos.

This is the same as in #18426, no? And at least right now environments are only created on server and builder init, so it shouldn't be a problem (except for fixing the issue you saw with the prev server having ghost environments at root). Or do you see some other problems?

@sapphi-red
Copy link
Member

Ah, yeah it is. I think it's fine. Just that we have to be careful not to forget to call environments.push when assigning $env.

patak-dev and others added 2 commits October 24, 2024 14:14
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
@patak-dev patak-dev changed the title refactor: .environments as array refactor: top level environments (and .environments array) Oct 24, 2024
@patak-dev patak-dev changed the title refactor: top level environments (and .environments array) refactor: top level keys for environments (and .environments array) Oct 24, 2024
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the majority is onboard, I'm happy with this change too 👍

Comment on lines +978 to +979
if (name.startsWith('$')) {
const environmentName = name as `$${string}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also disallow naming environments with just $, but I think we can think about this later.

@patak-dev patak-dev marked this pull request as draft October 25, 2024 10:14
@patak-dev
Copy link
Member Author

Closing as one of the main reasons for this exploration is gone after merging:

Even without this change, we got a lot of feedback that mixing environments and other config props wasn't worth removing the environments extra nested level. I think it was still good that we checked it with a PR to see how it works.

For later reference, we also asked for feedback about vite.envs vs vite.environments and not using the full word wasn't well received.

@patak-dev patak-dev closed this Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants