diff --git a/CHANGELOG.md b/CHANGELOG.md index b5d9f9d..3164380 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 69b6a8f..faa973e 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/index.ts b/src/index.ts index 7ee93d3..9a98eef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; @@ -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; @@ -72,6 +81,8 @@ const react = (_options?: Options): PluginOption[] => { : undefined, devTarget: _options?.devTarget ?? "es2020", parserConfig: _options?.parserConfig, + useAtYourOwnRisk_mutateSwcOptions: + _options?.useAtYourOwnRisk_mutateSwcOptions, }; return [ @@ -238,7 +249,7 @@ const transformWithOptions = async ( let result: Output; try { - result = await transform(code, { + const swcOptions: SWCOptions = { filename: id, swcrc: false, configFile: false, @@ -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("╭─[");