vitejs / vite-plugin-react-swc

Speed up your Vite dev server with SWC
MIT License
823 stars 52 forks source link

Support usage of `.swcrc` #1

Closed sar closed 1 year ago

sar commented 1 year ago

Add support for usage of external .swcrc configuration file as optional parameter passed to swcReactRefresh().

Example

swcReactRefresh({
  swcrc: true,  // override defaults
}),

Issue

Swc config appears to be hardcoded in-line and plugin does not respect .swcrc in rootDir. For example, adding changes like "transform" { "legacyDecorator": true } are not picked up by the plugin.

https://github.com/ArnaudBarre/vite-plugin-swc-react-refresh/blob/95291df10ecc2e1ec076db2f771e03fcd465fa64/src/swc-react-refresh.ts#L45-L62

Is overriding defaults supported (might be documentation issue)?

ArnaudBarre commented 1 year ago

This is hardcoded mostly because I did not see the need for a config.

I can add an option, but this plugin is only used in dev, so the build pipeline for vite need to support this as well. Is it the case for you?

If you need this for both dev and build, you can look at https://github.com/egoist/unplugin-swc

Edit: I just found that this plugin doesn't do fast refresh in dev. Which can be limiting.

sar commented 1 year ago

Testing in dev. Some environments require tweaking swcrc, it'd be useful to add support for an optional config param.

ArnaudBarre commented 1 year ago

Can you tell me if just changing swcrc to true in the node_modules does what you expect? If it works I can make it a plugin parameter, otherwise I need to look a bit more at how config loading & options merging works in swc

ArnaudBarre commented 1 year ago

Hi! After discussion with the core team, we decided to make this plugin official and keep the SWC configuration off.

If someone want to propose a use-case for it, please open a new issue to discuss it.

andyrichardson commented 1 year ago

Thanks for your work on this project! Just updated to the new version of Vite and wanted to try this out.

There is a swc plugin for styled components but I can't currently use it duebecause of this issue. This wasn't a problem on the babel plugin due to being able to specify plugins

react({
  babel: {
    plugins: [['babel-plugin-styled-components', { pure: true }]],
  },
})
ArnaudBarre commented 1 year ago

Thanks for reaching out.

We don't plan to support all use cases with this default plugin, but depending on the complexity and the need we could add an option for some use cases that have a good benefits/performance ratio.

Can you tell me if using an .swcrc file and changing this line to true in your node_modueles works for you?

Letting SWC resolve the configuration would be a good tradeoff, but would still make the caching feature I'm working on a bit more complex.

sar commented 1 year ago

Can you tell me if using an .swcrc file and changing this line to rue in your node_modueles works for you?

Overriding parameter swcrc: true in /node_modules/.../swc-react-refresh.js does fix it, correctly parses swc config from project root. Can you make it a plugin option?

ghost commented 1 year ago

should there be a base swc plugin for configuring the the .swcrc file instead? I doubt this plugin will be the last thing to want to use swc.

ArnaudBarre commented 1 year ago

I was a bit optimist yesterday, I totally forgot that this would require more work to support SWC at build time.

Tracking this in https://github.com/vitejs/vite-plugin-react-swc/issues/16