Skip to content

Commit f499d75

Browse files
authored
feat: add useAtYourOwnRisk_mutateSwcOptions option (#255)
1 parent f642b22 commit f499d75

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
## Unreleased
44

5+
### Add useAtYourOwnRisk_mutateSwcOptions option
6+
7+
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.
8+
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`.
9+
10+
```ts
11+
react({
12+
useAtYourOwnRisk_mutateSwcOptions(options) {
13+
options.jsc.parser.decorators = true;
14+
options.jsAc.transform.decoratorVersion = "2022-03";
15+
},
16+
});
17+
```
18+
519
## 3.7.2
620

721
### Add Vite 6 to peerDependencies range [#207](https://github.com/vitejs/vite-plugin-react-swc/pull/207)

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ react({
9393
});
9494
```
9595

96+
### useAtYourOwnRisk_mutateSwcOptions
97+
98+
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.
99+
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`.
100+
101+
```ts
102+
react({
103+
useAtYourOwnRisk_mutateSwcOptions(options) {
104+
options.jsc.parser.decorators = true;
105+
options.jsc.transform.decoratorVersion = "2022-03";
106+
},
107+
});
108+
```
109+
96110
## Consistent components exports
97111

98112
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

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ReactConfig,
99
JscTarget,
1010
transform,
11+
type Options as SWCOptions,
1112
} from "@swc/core";
1213
import { PluginOption, UserConfig, BuildOptions } from "vite";
1314
import { createRequire } from "module";
@@ -58,6 +59,14 @@ type Options = {
5859
* Exclusion of node_modules should be handled by the function if needed.
5960
*/
6061
parserConfig?: (id: string) => ParserConfig | undefined;
62+
/**
63+
* The future of Vite is with OXC, and from the beginning this was a design choice
64+
* to not exposed too many specialties from SWC so that Vite React users can move to
65+
* another transformer later.
66+
* Also debugging why some specific version of decorators with some other unstable/legacy
67+
* feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`
68+
*/
69+
useAtYourOwnRisk_mutateSwcOptions?: (options: SWCOptions) => void;
6170
};
6271

6372
const isWebContainer = globalThis.process?.versions?.webcontainer;
@@ -72,6 +81,8 @@ const react = (_options?: Options): PluginOption[] => {
7281
: undefined,
7382
devTarget: _options?.devTarget ?? "es2020",
7483
parserConfig: _options?.parserConfig,
84+
useAtYourOwnRisk_mutateSwcOptions:
85+
_options?.useAtYourOwnRisk_mutateSwcOptions,
7586
};
7687

7788
return [
@@ -238,7 +249,7 @@ const transformWithOptions = async (
238249

239250
let result: Output;
240251
try {
241-
result = await transform(code, {
252+
const swcOptions: SWCOptions = {
242253
filename: id,
243254
swcrc: false,
244255
configFile: false,
@@ -252,7 +263,11 @@ const transformWithOptions = async (
252263
react: reactConfig,
253264
},
254265
},
255-
});
266+
};
267+
if (options.useAtYourOwnRisk_mutateSwcOptions) {
268+
options.useAtYourOwnRisk_mutateSwcOptions(swcOptions);
269+
}
270+
result = await transform(code, swcOptions);
256271
} catch (e: any) {
257272
const message: string = e.message;
258273
const fileStartIndex = message.indexOf("╭─[");

0 commit comments

Comments
 (0)