Skip to content

Commit

Permalink
feat: add devTarget option (fixes #141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Sep 17, 2023
1 parent 27e3854 commit 82821d4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add `devTarget` option (fixes [#141](https://github.com/vitejs/vite-plugin-react-swc/issues/141))
- Disable Fast Refresh based on `config.server.hmr === false` instead of `process.env.TEST`
- Warn when plugin is in WebContainers (see [#118](https://github.com/vitejs/vite-plugin-react-swc/issues/118))
- 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))
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ This plugin has limited options to enable good performances and be transpiler ag

Control where the JSX factory is imported from.

`@default` "react"

```ts
react({ jsxImportSource: "@emotion/react" });
```
Expand All @@ -48,6 +50,8 @@ react({ jsxImportSource: "@emotion/react" });

Enable TypeScript decorators. Requires `experimentalDecorators` in tsconfig.

`@default` false

```ts
react({ tsDecorators: true });
```
Expand All @@ -60,6 +64,18 @@ Use SWC plugins. Enable SWC at build time.
react({ plugins: [["@swc/plugin-styled-components", {}]] });
```

## devTarget

Set the target for SWC in dev. This can avoid to down-transpile private class method for example.

For production target, see https://vitejs.dev/config/build-options.html#build-target.

`@default` "es2020"

```ts
react({ devTarget: "es2022" });
```

## Consistent components exports

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).
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type Options = {
* @default undefined
*/
plugins?: [string, Record<string, any>][];
/**
* Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
* For production target, see https://vitejs.dev/config/build-options.html#build-target
* @default "es2020"
*/
devTarget?: JscTarget;
};

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

return [
Expand Down Expand Up @@ -112,7 +119,7 @@ const react = (_options?: Options): PluginOption[] => {
const id = _id.split("?")[0];
const refresh = !transformOptions?.ssr && !hmrDisabled;

const result = await transformWithOptions(id, code, "es2020", options, {
const result = await transformWithOptions(id, code, options.devTarget, options, {
refresh,
development: true,
runtime: "automatic",
Expand Down

0 comments on commit 82821d4

Please sign in to comment.