web-infra-dev / rspack

The fast Rust-based web bundler with webpack-compatible API 🦀️
https://rspack.dev
MIT License
9.79k stars 561 forks source link

[Bug]: import is removed if not used even if the module has sideEffect #7574

Closed hzy closed 3 weeks ago

hzy commented 2 months ago

System Info

System: OS: macOS 14.1.1 CPU: (10) arm64 Apple M1 Max Memory: 149.39 MB / 64.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.20.4 - ~/.nvm/versions/node/v18.20.4/bin/node npm: 10.7.0 - ~/.nvm/versions/node/v18.20.4/bin/npm pnpm: 7.33.6 - ~/.nvm/versions/node/v18.20.4/bin/pnpm bun: 1.1.4 - ~/.bun/bin/bun Browsers: Chrome: 127.0.6533.101 Safari: 17.1

Details

A module is import but not none of its exports is used, in rspack this import is removed by builtin:swc-loader, causing the side effect statement of the import is not executed.

If i create a project with cra with same code, the side effect statement of the import is executed.

Below is a screenshot of rsdoctor loader analysis:

image

Reproduce link

No response

Reproduce Steps

  1. init a rsbuild project
  2. setup rsdoctor
  3. create a file with name sideEffect.js with content below:
    console.log(11111);
    export const a = 1;
  4. run project, the 11111 log SHOULD be printed, but in rspack it is not
JSerFeng commented 2 months ago

This is the same behavior as TypeScript, you can preserve import statement using this configuration

stale[bot] commented 4 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!