vitejs / vite-plugin-react-swc

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

feat: vitest deps.experimentalOptimizer (fixes #111) #115

Closed ArnaudBarre closed 1 year ago

ArnaudBarre commented 1 year ago

Fixes #111

To be validated by @sheremet-va

sheremet-va commented 1 year ago

Is „refresh“ the option that injects react refresh into HTML? Can it only be configured by the plugin itself?

ArnaudBarre commented 1 year ago

It's the option that enabled specific AST transformation for fast refresh (React HMR).

Then if this transformation is enabled and the output actually contains something React Refresh specific, we wrap the module with the import of the runtime code (which requires the transfromIndexHTML to run, which is not the case here)

My idea is to disable the HMR specific transform when running inside Vitest, and I wondering if process.env.TEST is a good check

sheremet-va commented 1 year ago

I wondering if process.env.TEST is a good check

If you only want to target Vitest, you can use process.env.VITEST, but we also set process.env.TEST, so it will work in Vitest.

Then if this transformation is enabled and the output actually contains something React Refresh specific, we wrap the module with the import of the runtime code (which requires the transfromIndexHTML to run, which is not the case here)

Interesting. I've been wondering how to disable this on our side in @vitejs/plugin-react which I assume uses the same mechanism. 🤔 But I assume this PR fixes it?

ArnaudBarre commented 1 year ago

The two plugins are independent but have the mechanism. I will update the Babel one just after.

I will go with process.env.TEST because this condition is more related to testing in general than juts Vitest (the problem did not surface before because of the ssr flag is on wihtout the optimizer)