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

Library mode does not respect the --mode flag #11302

Closed
7 tasks done
juliushuck opened this issue Dec 10, 2022 · 4 comments · Fixed by #11309
Closed
7 tasks done

Library mode does not respect the --mode flag #11302

juliushuck opened this issue Dec 10, 2022 · 4 comments · Fixed by #11309
Labels
p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@juliushuck
Copy link

juliushuck commented Dec 10, 2022

Describe the bug

I'm using the vite library mode in my monorepo to build the common packages between front and backend.

For development I used: vite build --mode development

and for production: vite build

After upgrading to vite 4, both commands seem to replace import.meta.env.PROD with true, before, it replaced it with false in development mode.

The replacement depending on the mode works in non library mode as far as I can tell

Reproduction

https://github.com/juliushuck/vite-issue/settings

Steps to reproduce

I have added the two commands i use in the package.json scripts

System Info

System:
  OS: macOS 13.0
  CPU: (10) arm64 Apple M1 Pro
  Memory: 76.44 MB / 16.00 GB
  Shell: 5.8.1 - /bin/zsh
Binaries:
  Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
  Yarn: 3.3.0 - /opt/homebrew/bin/yarn
  npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
Browsers:
  Chrome: 108.0.5359.98
  Firefox: 105.0.3
  Safari: 16.0

Used Package Manager

npm

Logs

No response

Validations

@juliushuck juliushuck changed the title Library mode does not use the --mode flag Library mode does not respect the --mode flag Dec 10, 2022
@bluwy
Copy link
Member

bluwy commented Dec 10, 2022

This is expected in Vite 4. vite build will default to PROD by default, the mode defines the different type of build. If you want to get the old behaviour, you can set NODE_ENV=development in .env.development like in https://vitejs.dev/guide/env-and-mode.html#modes

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 10, 2022
@juliushuck
Copy link
Author

Ok,

so I set NODE_ENV=development in .env-development. The import.meta.env.PROD value did not change, but .MODE changed to development. So I could use that, but I do not want to have to create .env files in my common packages every time I clone the repo, because I have other values in those .env files that I can not push to GitHub.

So I tried around and got this working:

const checkIsProductionMode = () => process.env.NODE_ENV === "production";

To be honest, I do not even know why it works, because I thought process is a node thing and does not work in the browser. But with that, it uses the --mode flag of the command I run the final package with (the one that uses the common package build output).

@bluwy
Copy link
Member

bluwy commented Dec 11, 2022

so I set NODE_ENV=development in .env-development. The import.meta.env.PROD value did not change

Ah this is a bug. I found the cause.

So I could use that, but I do not want to have to create .env files in my common packages every time I clone the repo, because I have other values in those .env files that I can not push to GitHub.

You can put the secrets in .env.development.local instead. .env.development can be commited to git.

@bluwy bluwy reopened this Dec 11, 2022
@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) p4-important Violate documented behavior or significantly improves performance (priority) and removed pending triage p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 11, 2022
@juliushuck
Copy link
Author

You can put the secrets in .env.development.local instead. .env.development can be commited to git.

Ah, that's the trick, will use that with import.meta.env.MODE === "production" for now then

@github-actions github-actions bot locked and limited conversation to collaborators Dec 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants