-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: don't replace NODE_ENV in vite:client-inject #6935
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bluwy I saw you did a PR before touching NODE_ENV replacement. Do you know why this branch is needed? Looks to me (as suggested by @sheremet-va) that the replacement done in the DefinePlugin should already take care of
process.env.NODE_ENV
. Why is this also being replaced here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran into this a couple of days ago when I wanted to configure some env variables for a service worker. It turned out that
DefinePlugin
only takes place for the production build and the server build (when SSR is enabled).vite/packages/vite/src/node/plugins/define.ts
Lines 84 to 89 in 3f27d58
As for the development build, it seems to be
vite/env.ts
that is supposed to define env variables. And it does it at runtime.For all that, I expected that importing
vite/env.ts
would defineprocess.env.NODE_ENV
. However, the reality was that it gets replaced by this workaround.So I would be interested in listening to why it was decided to keep this workaround instead of defining all the variables in
vite/env.ts
at runtime (in dev mode).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this look superfluous. The define plugin currently doesn't handle this case though since it bails out and relied on a global
globalThis.process.env.NODE_ENV = "env"
call for performance.vite/packages/vite/src/node/plugins/define.ts
Lines 85 to 88 in 56ed808
But it looks like currently we don't do a
globalThis.process.env.NODE_ENV = "env"
call anywhere, which causes the CI to fail. It should be fixable though if we addprocess.env.NODE_ENV
to the__DEFINES__
:vite/packages/vite/src/node/plugins/clientInjections.ts
Line 46 in 3f27d58
The current code now looks like a quick way to get it working before, which causes the Vitest issue since it doesn't handle
preventAssignment
cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So instead of having
processNodeEnv
in https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/define.ts#L10we should move it to the the place where Vite resolves config?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking of directly adding it in client injection fa7e458