Skip to content

Commit fff02a6

Browse files
committed
feat: add devTarget option (fixes #141)
1 parent 27e3854 commit fff02a6

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Add `devTarget` option (fixes [#141](https://github.com/vitejs/vite-plugin-react-swc/issues/141))
56
- Disable Fast Refresh based on `config.server.hmr === false` instead of `process.env.TEST`
67
- Warn when plugin is in WebContainers (see [#118](https://github.com/vitejs/vite-plugin-react-swc/issues/118))
78
- Better invalidation message when an export is added & fix HMR for export of nullish values ([#143](https://github.com/vitejs/vite-plugin-react-swc/issues/143))

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ This plugin has limited options to enable good performances and be transpiler ag
4040

4141
Control where the JSX factory is imported from.
4242

43+
`@default` "react"
44+
4345
```ts
4446
react({ jsxImportSource: "@emotion/react" });
4547
```
@@ -48,6 +50,8 @@ react({ jsxImportSource: "@emotion/react" });
4850

4951
Enable TypeScript decorators. Requires `experimentalDecorators` in tsconfig.
5052

53+
`@default` false
54+
5155
```ts
5256
react({ tsDecorators: true });
5357
```
@@ -60,6 +64,18 @@ Use SWC plugins. Enable SWC at build time.
6064
react({ plugins: [["@swc/plugin-styled-components", {}]] });
6165
```
6266

67+
## devTarget
68+
69+
Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
70+
71+
For production target, see https://vitejs.dev/config/build-options.html#build-target.
72+
73+
`@default` "es2020"
74+
75+
```ts
76+
react({ devTarget: "es2022" });
77+
```
78+
6379
## Consistent components exports
6480

6581
For React refresh to work correctly, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).

src/index.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ type Options = {
4444
* @default undefined
4545
*/
4646
plugins?: [string, Record<string, any>][];
47+
/**
48+
* Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
49+
* For production target, see https://vitejs.dev/config/build-options.html#build-target
50+
* @default "es2020"
51+
*/
52+
devTarget?: JscTarget;
4753
};
4854

4955
const isWebContainer = globalThis.process?.versions?.webcontainer;
@@ -56,6 +62,7 @@ const react = (_options?: Options): PluginOption[] => {
5662
plugins: _options?.plugins
5763
? _options?.plugins.map((el): typeof el => [resolve(el[0]), el[1]])
5864
: undefined,
65+
devTarget: _options?.devTarget ?? "es2020",
5966
};
6067

6168
return [
@@ -112,12 +119,18 @@ const react = (_options?: Options): PluginOption[] => {
112119
const id = _id.split("?")[0];
113120
const refresh = !transformOptions?.ssr && !hmrDisabled;
114121

115-
const result = await transformWithOptions(id, code, "es2020", options, {
116-
refresh,
117-
development: true,
118-
runtime: "automatic",
119-
importSource: options.jsxImportSource,
120-
});
122+
const result = await transformWithOptions(
123+
id,
124+
code,
125+
options.devTarget,
126+
options,
127+
{
128+
refresh,
129+
development: true,
130+
runtime: "automatic",
131+
importSource: options.jsxImportSource,
132+
},
133+
);
121134
if (!result) return;
122135

123136
if (!refresh || !refreshContentRE.test(result.code)) {

0 commit comments

Comments
 (0)