Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useAtYourOwnRisk_mutateSwcOptions option #255

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

## Unreleased

### Add useAtYourOwnRisk_mutateSwcOptions option

The future of Vite is with OXC, and from the beginning this was a design choice to not exposed too many specialties from SWC so that Vite React users can move to another transformer later.
Also debugging why some specific version of decorators with some other unstable/legacy feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`.

```ts
react({
useAtYourOwnRisk_mutateSwcOptions(options) {
options.jsc.parser.decorators = true;
options.jsAc.transform.decoratorVersion = "2022-03";
},
});
```

## 3.7.2

### Add Vite 6 to peerDependencies range [#207](https://github.com/vitejs/vite-plugin-react-swc/pull/207)
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ react({
});
```

### useAtYourOwnRisk_mutateSwcOptions

The future of Vite is with OXC, and from the beginning this was a design choice to not exposed too many specialties from SWC so that Vite React users can move to another transformer later.
Also debugging why some specific version of decorators with some other unstable/legacy feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`.

```ts
react({
useAtYourOwnRisk_mutateSwcOptions(options) {
options.jsc.parser.decorators = true;
options.jsc.transform.decoratorVersion = "2022-03";
},
});
```

## 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
19 changes: 17 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ReactConfig,
JscTarget,
transform,
type Options as SWCOptions,
} from "@swc/core";
import { PluginOption, UserConfig, BuildOptions } from "vite";
import { createRequire } from "module";
Expand Down Expand Up @@ -58,6 +59,14 @@ type Options = {
* Exclusion of node_modules should be handled by the function if needed.
*/
parserConfig?: (id: string) => ParserConfig | undefined;
/**
* The future of Vite is with OXC, and from the beginning this was a design choice
* to not exposed too many specialties from SWC so that Vite React users can move to
* another transformer later.
* Also debugging why some specific version of decorators with some other unstable/legacy
* feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`
*/
useAtYourOwnRisk_mutateSwcOptions?: (options: SWCOptions) => void;
};

const isWebContainer = globalThis.process?.versions?.webcontainer;
Expand All @@ -72,6 +81,8 @@ const react = (_options?: Options): PluginOption[] => {
: undefined,
devTarget: _options?.devTarget ?? "es2020",
parserConfig: _options?.parserConfig,
useAtYourOwnRisk_mutateSwcOptions:
_options?.useAtYourOwnRisk_mutateSwcOptions,
};

return [
Expand Down Expand Up @@ -238,7 +249,7 @@ const transformWithOptions = async (

let result: Output;
try {
result = await transform(code, {
const swcOptions: SWCOptions = {
filename: id,
swcrc: false,
configFile: false,
Expand All @@ -252,7 +263,11 @@ const transformWithOptions = async (
react: reactConfig,
},
},
});
};
if (options.useAtYourOwnRisk_mutateSwcOptions) {
options.useAtYourOwnRisk_mutateSwcOptions(swcOptions);
}
result = await transform(code, swcOptions);
} catch (e: any) {
const message: string = e.message;
const fileStartIndex = message.indexOf("╭─[");
Expand Down
Loading