-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Conversation
Checking CI, @sheremet-va I think you are right about this branch being superfluous |
Well, I see CI failing.. 👀 |
return code.replace( | ||
/\bprocess\.env\.NODE_ENV\b/g, | ||
JSON.stringify(config.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.
@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
const ssr = options?.ssr === true | |
if (!ssr && !isBuild) { | |
// for dev we inject actual global defines in the vite client to | |
// avoid the transform cost. | |
return | |
} |
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 define process.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
if (!ssr && !isBuild) { | |
// for dev we inject actual global defines in the vite client to | |
// avoid the transform cost. | |
return |
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 add process.env.NODE_ENV
to the __DEFINES__
:
.replace(`__DEFINES__`, serializeDefine(config.define || {})) |
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#L10
we 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
@antfu @bluwy Im thinking this may be a bigger change than what looked like at the beggining. We are now going to have a |
Or I guess we could bypass the replace only for ssr |
Ah I think you're right @patak-dev. I just found 8ad7ecd which prevents this from happening. Anthony's idea would be better. |
This reverts commit 2b70003.
This reverts commit 2b70003.
Description
See vitest-dev/vitest#202 (comment)
What is the purpose of this pull request?