feat: preserve process env vars in lib build#8090
feat: preserve process env vars in lib build#8090patak-cat merged 12 commits intovitejs:mainfrom bluwy:lib-env-replacement
Conversation
|
Tests are fixed. Seems like CI is flaky though |
It is killing us, there is a node seg fault we are hitting constantly in v16 |
|
For anyone else who stumbles over this backwards-incompatible change in Vite 3.0: to get back env var replacements in lib builds, it is now necessary to manually add the relevant definitions to the Vite config. For example: export default defineConfig({
define: {
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
},
...
}This will allow the resulting libraries to be used directly in the browser, without needing an additional build step. |
|
Thanks for the tip @latk. I think this is worth a mention in the docs, since specifically mentions building a browser-oriented library. I'll make a PR for it. |
bug introduced by vitejs/vite#8090
|
@bluwy bluwy, I have a question, why skip the library build pattern case? vite/packages/vite/src/node/plugins/define.ts Lines 15 to 30 in 1128b4d I am using react to build a library for the browser. React used process.env.NODE _ENV, and I found that you skipped the library mode when dealing with process polyfill. Is there any special reason for this? Of course @latk 's method can solve my problem, I'm just curious. |
|
@gwsbhqt Do you mean the |
|
Hi @bluwy The behavior of code in define.ts when handling import.meta.env replacement in lib mode does not match the comments here https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/define.ts#L40 Shall we introduce a config flag |
|
Hmm that comment should be removed. It's likely I was testing things out and copy-pasted wrongly. The reason is that |
bug introduced by vitejs/vite#8090
Description
Fix #3229
Preserve
process.env.*in library mode buildAdditional context
I also found a bug where
process.env.UNKNOWNisn't replaced as({}).UNKNOWNin non-lib build mode. Bug introduced in #5972. Seems tricky to fix but otherwise I'll open an issue soon.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).