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: don't replace NODE_ENV in vite:client-inject #6935

Merged
merged 5 commits into from Feb 18, 2022

Conversation

patak-dev
Copy link
Member

Description

See vitest-dev/vitest#202 (comment)


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

@patak-dev
Copy link
Member Author

Checking CI, @sheremet-va I think you are right about this branch being superfluous

@sheremet-va
Copy link
Member

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)
)
}
Copy link
Member Author

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?

Copy link
Contributor

@vursen vursen Feb 16, 2022

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).

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).

Copy link
Member

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.

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.

Copy link
Member

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?

Copy link
Member Author

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

@patak-dev patak-dev requested a review from bluwy February 17, 2022 21:11
@patak-dev patak-dev merged commit 2b70003 into main Feb 18, 2022
@patak-dev patak-dev deleted the fix/do-not-replace-node-env-in-plugin-inject branch February 18, 2022 04:32
@patak-dev
Copy link
Member Author

@antfu @bluwy Im thinking this may be a bigger change than what looked like at the beggining. We are now going to have a process global in dev. So some errors with user code using process will be different (and inconsistent between dev and build). I think this is the reason for doing the replacement out of defines. Maybe we need to avoid replacing it always, or replace with a guarded value instead of config.mode (like we do in the define plugin). I'll check later today, for sure this shouldnt be in a patch

@antfu
Copy link
Member

antfu commented Feb 18, 2022

Or I guess we could bypass the replace only for ssr

@bluwy
Copy link
Member

bluwy commented Feb 18, 2022

Ah I think you're right @patak-dev. I just found 8ad7ecd which prevents this from happening. Anthony's idea would be better.

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.

None yet

5 participants