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

[Bug]: Environment API can not load .env files into ESM output in dev mode #3394

Closed
sibbng opened this issue Sep 5, 2024 · 2 comments
Closed
Labels
documentation Improvements or additions to documentation

Comments

@sibbng
Copy link

sibbng commented Sep 5, 2024

Version

System:
    OS: Windows 10 10.0.19042
    CPU: (4) x64 Intel(R) Core(TM) i5-7400 CPU @ 3.00GHz
    Memory: 1.94 GB / 7.83 GB
  Browsers:
    Edge: Spartan (44.19041.423.0), Chromium (127.0.2651.74)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    @rsbuild/core: 1.0.1-beta.16 => 1.0.1-beta.16

Details

I use Rsbuild environment API to build a client and a server. I enforce ESM output on both environments and have module: "module" in my package.json. But Rspack trying to execute that file in CJS environment, because it requires mjs extension.

https://github.com/web-infra-dev/rspack/blob/d9a6d592e5b9cde55391a0e22786817d3bc5b63d/packages/rspack-test-tools/src/runner/runner/esm.ts#L23-L26

I fixed this issue by modifying output filename:

https://github.com/sibbng/voby-ssr-demo/blob/02d98800b11cb0306d8dc6ccb1924903f90ecac0/apps/web/rsbuild.config.ts#L75-L77

After these changes I managed to run my server in ESM mode I've got this error:

queueMicrotask undefined

I assume this is something related to vmContext. I fixed this issue by manually patching queueMicrotask for the dependencies failing. I probably should have done that in my entry files as it will be needed in the future for other deps.

https://github.com/sibbng/voby-ssr-demo/blob/02d98800b11cb0306d8dc6ccb1924903f90ecac0/packages/voby-unplugin/src/index.ts#L189

After pathing all these, I managed to get a working dev and prod environment. But for some reason, environment values are not loaded in dev mode.

https://github.com/sibbng/voby-ssr-demo/blob/02d98800b11cb0306d8dc6ccb1924903f90ecac0/apps/web/package.json#L7-L8

Even though commands are identical (except entry files), server/index.tsx missing PUBLIC environment variables defined in .env.development and throw process undefined error.

As a workaround, I manually load environment variables during to ssr environment:

https://github.com/sibbng/voby-ssr-demo/blob/9dba96c8d2de3425e11dfbcf707821a7df1605e4/apps/web/rsbuild.config.ts#L67-L72

Reproduce link

https://github.com/sibbng/voby-ssr-demo

Reproduce Steps

  1. remove my workaround
  2. pnpm dev
  3. Visit localhost:3000 in browser
  4. Check console. It says process not defined.
@sibbng sibbng added the 🐞 bug Something isn't working label Sep 5, 2024
@9aoy
Copy link
Collaborator

9aoy commented Sep 6, 2024

@sibbng Thank you for providing such a detailed description of the problem.
Automatic load env is a capability provided by the Rsbuild CLI, activated when you execute the rsbuild dev / rsbuild build .
When utilizing the Environment API, you need explicitly invoke the loadEnv method. Apologies for the oversight in our documentation; I will ensure it is updated accordingly.

@9aoy 9aoy added documentation Improvements or additions to documentation and removed 🐞 bug Something isn't working labels Sep 6, 2024
@sibbng
Copy link
Author

sibbng commented Sep 6, 2024

Thanks for clarification. I will raise another issue on Rspack repo about queueMicrotask being undefined in ESM environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants