vitejs / vite-plugin-react-swc

Speed up your Vite dev server with SWC
MIT License
778 stars 50 forks source link

useDefineForClassFields breaks class fields assignment #117

Closed ludinov closed 1 year ago

ludinov commented 1 year ago

I believe that useDefineForClassFields was turned on by strong reasons, but right now it breaks assignments order in compiled bundle.

class Test {
  constructor(private a: string) {}
  b = this.a + "!"
}

compiles into

class f{constructor(t){n(this,"b",this.a+"!");this.a=t}}

So property b becomes undefined!

probably it should compile into something like this:

class f{constructor(t){n(this,"b",void 0);this.a=t;this.b=this.a+"!"}}

swc itself looks like handle it properly: https://play.swc.rs/?version=1.3.61&code=H4sIAAAAAAAAA0vOSSwuVghJLS5RqOZSAILk%2FLzikqLS5JL8Io2CosyyxJJUhURNhepasGySgq1CSUZmsV6igraCkqKSNVctAGoH4EtCAAAA&config=H4sIAAAAAAAAA1VPSQ7DIAy85xXI5x6iHPuHPAJRJyJiE3akoih%2FLxBC25s9i2d8DELARgqe4shjXoKMhLHvGaHkWL4zApwCkoo6MDxulqlQHHesyHkRwDKuyMWENI3T2AxgvCfM8CINYcOsdnpJv5HK2xCR6F9YpNKtpvuvxKGlgvWvvZLtlVK3NMjXrHcbwVd5J%2FbroGm%2B7fWZ8wOibzeCGwEAAA%3D%3D

ludinov commented 1 year ago

actually it is vite issue : https://github.com/vitejs/vite-plugin-react-swc/issues/117