Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Commit 8775186

Browse files
committed
Use named export instead of default export for better esm/cjs interop & add support for jsx automatic runtime [publish]
1 parent cf65a9a commit 8775186

File tree

5 files changed

+186
-152
lines changed

5 files changed

+186
-152
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 0.2.0
4+
5+
Breaking: Use named export instead of default export for better esm/cjs interop.
6+
7+
To migrate, replace your import by `import { swcReactRefresh } from "vite-plugin-swc-react-refresh";`
8+
9+
The JSX automatic runtime is also now supported if you bump esbuild to at least [0.14.51](https://github.com/evanw/esbuild/releases/tag/v0.14.51).
10+
11+
To use it, update your config from `esbuild: { jsxInject: 'import React from "react"' },` to `esbuild: { runtime: "automatic" },`
12+
313
## 0.1.2
414

515
- Add vite as peer dependency

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
Use the versatility of [swc](https://swc.rs/) for development and the maturity of [esbuild](https://esbuild.github.io/) for production.
44

55
- ✅ A fast Fast Refresh (~20x faster than Babel)
6-
- ✅ Skip `import React`
7-
- ❌ Not compatible with [automatic JSX runtime](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html). See [this section](#jsx-runtime)
6+
- ✅ Compatible with [automatic JSX runtime](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)
87
- ❌ Don't check for consistent components exports. See [this section](#consistent-components-exports)
98

109
## Installation
@@ -15,19 +14,29 @@ npm i -D vite-plugin-swc-react-refresh
1514

1615
## Usage
1716

17+
With the automatic JSX runtime (requires esbuild => [0.14.51](https://github.com/evanw/esbuild/releases/tag/v0.14.51)):
18+
1819
```ts
1920
import { defineConfig } from "vite";
20-
import swcReactRefresh from "vite-plugin-swc-react-refresh";
21+
import { swcReactRefresh } from "vite-plugin-swc-react-refresh";
2122

2223
export default defineConfig({
2324
plugins: [swcReactRefresh()],
24-
esbuild: { jsxInject: `import React from "react"` },
25+
esbuild: { runtime: "automatic" },
2526
});
2627
```
2728

28-
## JSX Runtime
29+
Without the automatic JSX runtime, but still omitting the `React` default import:
30+
31+
```ts
32+
import { defineConfig } from "vite";
33+
import { swcReactRefresh } from "vite-plugin-swc-react-refresh";
2934

30-
This plugin is only used in development, and esbuild (which doesn't support the automatic JSX runtime) will be used by Vite for bundling. However, you can omit the default React import with the `esbuild.jsxInject` Vite option.
35+
export default defineConfig({
36+
plugins: [swcReactRefresh()],
37+
esbuild: { jsxInject: `import React from "react"` },
38+
});
39+
```
3140

3241
## Consistent components exports
3342

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-swc-react-refresh",
33
"description": "Use the versatility of swc for development and the maturity of esbuild for production",
4-
"version": "0.1.2",
4+
"version": "2.0.0",
55
"license": "MIT",
66
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
77
"main": "src/swc-react-refresh.js",
@@ -36,6 +36,6 @@
3636
"@types/node": "^18.0.6",
3737
"prettier": "^2.7.1",
3838
"typescript": "^4.7.4",
39-
"vite": "^3.0.2"
39+
"vite": "^3.0.5"
4040
}
4141
}

src/swc-react-refresh.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ window.$RefreshSig$ = () => (type) => type;`;
1313
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
1414

1515
let define: { [key: string]: string } | undefined;
16+
let automaticRuntime = false;
1617

17-
export default (): PluginOption => ({
18+
export const swcReactRefresh = (): PluginOption => ({
1819
name: "react-refresh",
1920
apply: "serve",
2021
config: (config) => {
21-
if (config.esbuild) define = config.esbuild.define;
22+
if (config.esbuild) {
23+
define = config.esbuild.define;
24+
automaticRuntime = config.esbuild.jsx === "automatic";
25+
}
2226
config.esbuild = false;
2327
},
2428
resolveId: (id) => (id === runtimePublicPath ? id : undefined),
@@ -41,7 +45,12 @@ export default (): PluginOption => ({
4145
jsc: {
4246
target: "es2020",
4347
transform: {
44-
react: { refresh: true, development: true, useBuiltins: true },
48+
react: {
49+
refresh: true,
50+
development: true,
51+
useBuiltins: true,
52+
runtime: automaticRuntime ? "automatic" : undefined,
53+
},
4554
optimizer: { globals: { vars: define } },
4655
},
4756
},

0 commit comments

Comments
 (0)