web-infra-dev / rspack

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

[Bug]: Cannot access '[class x]' before initialization. when rspack handles class dependencies, the scope elevation does not take effect for the class. #7122

Open 1yasa opened 1 month ago

1yasa commented 1 month ago

System Info

System: OS: macOS 14.4 CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900KF Memory: 12.73 GB / 32.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 21.6.1 - /usr/local/bin/node npm: 10.2.4 - /usr/local/bin/npm pnpm: 9.4.0 - /usr/local/bin/pnpm Watchman: 2024.06.10.00 - /usr/local/bin/watchman

Details

rspack build output used in app error:

截屏2024-07-11 18 17 46

location for error code:

截屏2024-07-11 18 17 58

class dependencie [LexicalNode] defind in the last bottom:

截屏2024-07-11 18 18 52

rollup build output [LexicalNode] in the top(before used):

截屏2024-07-11 18 19 54

I tried all possible configurations of swc and rspack, and finally concluded that this issue might be due to the omission of some scenarios of class scope hoisting.

It could also be a circular dependency causing the scope to be hoisted incorrectly.

Reproduce link

https://github.com/openages/lexical

Reproduce Steps

pnpm run rspack for rspack build output.

pnpm run build for rollup build output.

use "lexical":"workspace:*" in the monorepo/packages/app.

https://lexical.dev/docs/getting-started/quick-start

1yasa commented 1 month ago

By the way, this also may cause by pure esm(rspack handle pure esm is experimental).

1yasa commented 1 month ago

Reproduce when use rollup(export LexicalNode instead of export type LexicalNode):

截屏2024-07-11 19 46 53

So question is, why rspack treat export type LexicalNode as a actually export, the rollup handle this case is right.

1yasa commented 1 month ago
截屏2024-07-11 20 19 18

Tested export order, that may cause output same error, fixed by export * from ''.

But rspack still scope to be hoisted incorrectly.

1yasa commented 1 month ago

@ahabhgk "same as webpack" It means that webpack is the same, so will it not be fixed?

ahabhgk commented 1 month ago

No, it means we will fix it for both rspack and webpack

ahabhgk commented 1 month ago

This is caused by circular imports and the case is very complex, so we may need more time to find the root issue and then fix it

1yasa commented 1 week ago

Is there any progress?