The `package` command does not replace `process.env.*`
See original GitHub issueDescribe the bug
When you run npm run package
in a SvelteKit project, process.env
references (like process.env.NODE_ENV
) are not statically replaced; the files that are output still contain the process.env
references.
I don’t know if there are cases when this is desirable, but in my case it is not. My library has a few checks for process.env.NODE_ENV === "test"
which are only internal. I have a hard time imagining a case when a package user would want anything other than "production"
from a third-party library.
In particular, this seems like it causes an issue with Svelte projects built with https://github.com/sveltejs/template where the packaging also does not handle process.env
.
Reproduction
It’s not a minimal example, but I ran into this with my component library: https://github.com/rgossiaux/svelte-headlessui
You can repro this by checking out & installing the repo, then running npm run package
.
A user then reported that the resulting package is broken with https://github.com/sveltejs/template using this example repo: https://github.com/jerriclynsjohn/svelte-headlessui-test-repo
Logs
No response
System Info
System:
OS: macOS 12.1
CPU: (10) arm64 Apple M1 Max
Memory: 7.89 GB / 64.00 GB
Shell: 3.3.1 - /opt/homebrew/bin/fish
Binaries:
Node: 16.8.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23841_1640400023703/bin/node
Yarn: 1.22.17 - /opt/homebrew/bin/yarn
npm: 7.21.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23841_1640400023703/bin/npm
Browsers:
Chrome: 97.0.4692.99
Safari: 15.2
### Severity
serious, but I can work around it
### Additional Information
_No response_
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Sure, I’m working around this by just manually replacing
process.env.NODE_ENV
in thepackage
script inpackage.json
:https://github.com/rgossiaux/svelte-headlessui/blob/f431f891271291c8db7c75fe00c7c19b2542a21d/package.json#L22
Pretty janky but it gets the job done for my use case.
Possibly related to #2572 - It would help fix this issue because (assuming bundling would use vite or rollup) you could use Vite’s
define
option or@rollup/plugin-replace
to do the replacement, but as of right now using either of them won’t work.