vitejs / vite-plugin-react-swc

Speed up your Vite dev server with SWC
MIT License
774 stars 49 forks source link

Support useDefineForClassFields configurable #183

Closed gwsbhqt closed 6 months ago

gwsbhqt commented 6 months ago

I'm building a library for automatic modeling, and a feature needs to turn off useDefineForClassFields option. I found that this configuration cannot be configured in this plugin. Can I provide a configuration that is turned off?

I saw your reply, but I need to provide this library for others to use, so I can't patch this line of code in every development environment.

ArnaudBarre commented 6 months ago

I'm not sure to understand your need, are you using Vite in library mode? In that case patching in your workflow and publishing the compiled code should work. If your library requires users to use this outdated spec instead of the new one, I think my point stands that this is not something that should be too easy to do nowdays.

gwsbhqt commented 6 months ago

@ArnaudBarre In fact, my need is to read the decorator information of the prototype of the subclass in the parent class, and then model the attribute in the parent class in advance according to special rules. But if useDefineForClassFields is turned on, the attribute definition in the subclass will assign me to the coverage in the parent class. So I have been looking for a way to turn off useDefineForClassFields before. In fact, I have found another solution. Just use the declaration in the subclass attribute. In this way, the attribute declaration will be wiped out during compile, and then coverage will not occur. Like this.

Before compile

class A {
  normalAttr: string
  declare declareAttr: string // Like this.
}

After compile

class A {
  constructor() {
    this.normalAttr = undefined // useDefineForClassFields cause normalAttr to still exist
    // declare cause declareAttr to be wiped
  }
}