Skip to content

Commit

Permalink
feat: add useAtYourOwnRiskEditConfig option
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Jan 11, 2025
1 parent f642b22 commit 4415ff6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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({
});
```

### useAtYourOwnRiskEditConfig

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({
useAtYourOwnRiskEditConfig(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
18 changes: 16 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`
*/
useAtYourOwnRiskEditConfig?: (options: SWCOptions) => void;
};

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

return [
Expand Down Expand Up @@ -238,7 +248,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 +262,11 @@ const transformWithOptions = async (
react: reactConfig,
},
},
});
};
if (options.useAtYourOwnRiskEditConfig) {
options.useAtYourOwnRiskEditConfig(swcOptions);
}
result = await transform(code, swcOptions);
} catch (e: any) {
const message: string = e.message;
const fileStartIndex = message.indexOf("╭─[");
Expand Down

0 comments on commit 4415ff6

Please sign in to comment.