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

fix!: make NODE_ENV more predictable #10996

Merged
merged 1 commit into from Nov 22, 2022
Merged

fix!: make NODE_ENV more predictable #10996

merged 1 commit into from Nov 22, 2022

Conversation

benmccann
Copy link
Collaborator

@benmccann benmccann commented Nov 19, 2022

Description

Make the value of NODE_ENV more predictable

Additional context

This was discussed in #9274

The one intricacy in implementing was related to running tests. When the first test runs Vite will set a value for NODE_ENV and then Vite will no longer reset the value on subsequent tests, so I had to handle that in the tests themselves. That's unlikely to affect our users though, so I think this is still a good trade-off. E.g. when Playwright runs the Vite server in the tests, it will do so in a sub-process, which I think would isolate each server process from one another.

There was a comment in one of the tests about plugin-legacy that I deleted as it appears to be outdated. Had it not been outdated, I think fixing plugin-legacy would have been the better approach than making Vite adhere to the behavior of a single plugin, but that appears no longer relevant.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@benmccann benmccann added this to the 4.0 milestone Nov 19, 2022
@benmccann benmccann force-pushed the NODE_ENV branch 2 times, most recently from b48f16c to e51b965 Compare November 19, 2022 14:22
@benmccann benmccann changed the title fix: don't override user NODE_ENV value fix: make NODE_ENV more predictable Nov 19, 2022
@benmccann benmccann force-pushed the NODE_ENV branch 7 times, most recently from 2f4fa35 to 3c43204 Compare November 20, 2022 04:25
@bluwy bluwy self-requested a review November 20, 2022 07:11
@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Nov 22, 2022

📝 Ran ecosystem CI: Open

suite result
astro ✅ success
histoire ❌ failure
iles ❌ failure
ladle ✅ success
laravel ❌ failure
marko ✅ success
nuxt-framework ❌ failure
rakkas ⏹️ cancelled
svelte ❌ failure
vite-plugin-ssr ❌ failure
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ❌ failure
windicss ✅ success

@patak-dev
Copy link
Member

I think we should move forward with this one to be able to test it during the alpha window. @bluwy feel free to merge after you review it.

@patak-dev patak-dev changed the title fix: make NODE_ENV more predictable fix!: make NODE_ENV more predictable Nov 22, 2022
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.

Thanks for making the changes! This LGTM too.

Looks like overall this implements #9274, except:

  • Doesn't remove NODE_ENV support in .env files. Maybe ok for now to minimize the breaking change, but it does mean staging mode still builds in development NODE_ENV by default.
  • NODE_ENV is still mutated in resolveConfig. Maybe it's also ok as the rule is simpler now.

Merging since we can improve the above later.

@bluwy bluwy merged commit 8148af7 into vitejs:main Nov 22, 2022
@benmccann benmccann deleted the NODE_ENV branch November 22, 2022 18:36
@benmccann
Copy link
Collaborator Author

Doesn't remove NODE_ENV support in .env files. Maybe ok for now to minimize the breaking change, but it does mean staging mode still builds in development NODE_ENV by default.

@bluwy can you clarify? I believe the default would be production:

If you don't define NODE_ENV in an .env file I think it shouldn't have any effect (I haven't tested though). Is there a reason we don't want users to be able to set it there?

@bluwy
Copy link
Member

bluwy commented Nov 23, 2022

Ah you're right I missed that. Nevermind then 😄

If you don't define NODE_ENV in an .env file I think it shouldn't have any effect (I haven't tested though). Is there a reason we don't want users to be able to set it there?

It's mostly the concern that resolveConfig has another way to mutate NODE_ENV that would be nice to avoid

const isProduction =
(process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) ===
'production'
if (isProduction) {
// in case default mode was not production and is overwritten
process.env.NODE_ENV = 'production'
}

Now that I think about it, since builds are production NODE_ENV by default, the above code doesn't allow someone to build in development NODE_ENV. I can fix that with a follow up PR.

fc pushed a commit to fc/vite that referenced this pull request Nov 23, 2022
rnathuji added a commit to openstax/k12-apps-raise that referenced this pull request Jan 13, 2023
There were a couple of improvements in the updated version of Vite
around decoupling build modes and NODE_ENV:

* vitejs/vite#10996
* vitejs/vite#11045

Accordingly, this change:

* Consistently uses import.meta.env.MODE to set build mode specific
  values vs relying on the (previous) behavior around how
  import.meta.env.PROD was set.
* Adds a development environment file so NODE_ENV is set to development
  for this builds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants