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

Optional Environment Variables #7364

Open
Blankeos opened this issue Jul 30, 2024 · 5 comments
Open

Optional Environment Variables #7364

Blankeos opened this issue Jul 30, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@Blankeos
Copy link

System Info

System:
OS: macOS 14.5
CPU: (8) arm64 Apple M2
Memory: 88.97 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v20.14.0/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
pnpm: 9.5.0 - ~/.nvm/versions/node/v20.14.0/bin/pnpm
bun: 1.1.21 - ~/.bun/bin/bun
Browsers:
Chrome: 127.0.6533.73
Safari: 17.5

Details

When trying to access env vars via:
process.env.PUBLIC_NICE
import.meta.env.PUBLIC_NICE

And I don't have PUBLIC_NICE in my .env. It throws an error instead of just being undefined.

Afaik in Vite, I'm allowed to have optional env variables. I usually setup my config in a typescript file with default values for local like:

export const config = {
   NICE: process.env.PUBLIC_NICE ?? "my default";
};

Reproduce link

No response

Reproduce Steps

  1. Make an rspack project

  2. Go to App.tsx and write process.env.PUBLIC_NICE somewhere

  3. Run the app (expect: error)

  4. Add PUBLIC_NICE="Hello" to .env

  5. Run the app (expect: no errors)

@Blankeos Blankeos added bug Something isn't working pending triage The issue/PR is currently untouched. labels Jul 30, 2024
@CPunisher
Copy link
Contributor

Maybe you can define an empty process.env with DefinePlugin.

https://rspack.dev/zh/plugins/webpack/define-plugin
https://rsbuild.dev/config/source/define

new rspack.DefinePlugin({
  "process.env": {},
}),

@abenhamdine
Copy link

abenhamdine commented Aug 1, 2024

it's indeed a non documented breaking change in rspack beta 1.0.0 beta0, see web-infra-dev/rsbuild#3029

@LingyuCoder
Copy link
Collaborator

Currently Rspack/Webpack does not support import.meta.env. You can use DefinePlugin and process.env instead. The previous version of Rspack would replace import.meta.env.xxxx with undefined, this may result in entering an unexpected branch without knowing.

@LingyuCoder LingyuCoder removed the pending triage The issue/PR is currently untouched. label Aug 1, 2024
@abenhamdine
Copy link

abenhamdine commented Aug 1, 2024

Currently Rspack/Webpack does not support import.meta.env. You can use DefinePlugin and process.env instead. The previous version of Rspack would replace import.meta.env.xxxx with undefined, this may result in entering an unexpected branch without knowing.

Hmm rsbuild/rspack supports import.meta.env, we use it in our code.
But since rsbuild beta 6 (rspack 1.0.0 beta 0), if the variable is not defined in the environment, import.meta.env is replaced by undefined, thus trying to access undefined.PUBLIC_MY_VARIABLE results in a error.

@LingyuCoder
Copy link
Collaborator

Currently Rspack/Webpack does not support import.meta.env. You can use DefinePlugin and process.env instead. The previous version of Rspack would replace import.meta.env.xxxx with undefined, this may result in entering an unexpected branch without knowing.

Hmm rsbuild/rspack supports import.meta.env, we use it in our code.
But since rsbuild beta 6 (rspack 1.0.0 beta 0), if the variable is not defined in the environment, import.meta.env is replaced by undefined, thus trying to access undefined.PUBLIC_MY_VARIABLE results in a error.

ok, we will find a better way to handle this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants