vitejs / vite-plugin-react-swc

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

`TypeError: react is not a function` when importing via commonjs #14

Closed benatshippabo closed 1 year ago

benatshippabo commented 1 year ago

When using commonjs imports

const react = require('@vitejs/plugin-react-swc');

We get TypeError: react is not a function. It works with esm imports, but I thought this was worth bringing up.

Example reproduction:

https://stackblitz.com/edit/vitejs-vite-tcrxby?file=vite.config.js

ArnaudBarre commented 1 year ago

There is not SWC bindings available for Stackblitz yet, so with ESM it fails just after. That's strange though, there is probably an issue with default export. Thanks for reporting!

ArnaudBarre commented 1 year ago

So yeah basically this a classic interop issue, which is why we should move away from default export (or cjs, but one is simpler than the other).

I can patch esbuild output for the cjs output, but before doing that I want to know if it will be possible to support this directly in esbuild: https://github.com/evanw/esbuild/issues/2733

In the meantime you can change your export to require('@vitejs/plugin-react-swc').default

ArnaudBarre commented 1 year ago

This is will be fixed in the next path in few days