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

Add swcImplementation option #206

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Add swcImplementation option

Pass a custom SWC implementation. This can be used to keep a stable version of SWC instead of using package manager overrides, at the cost of installing it twice.

## 3.6.0

### Add parserConfig option
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ react({
});
```

### swcImplementation

Advanced. Pass a custom SWC implementation. This can be used to keep a stable version of SWC instead of using package manager overrides, at the cost of installing it twice.

## 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
37 changes: 28 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { SourceMapPayload } from "module";
import {
Output,
ParserConfig,
ReactConfig,
JscTarget,
type Output,
type ParserConfig,
type ReactConfig,
type JscTarget,
transform,
} from "@swc/core";
import { PluginOption, UserConfig, BuildOptions } from "vite";
Expand Down Expand Up @@ -58,6 +58,12 @@ type Options = {
* Exclusion of node_modules should be handled by the function if needed.
*/
parserConfig?: (id: string) => ParserConfig | undefined;
/**
* Advanced. Pass a custom SWC implementation.
* This can be used to keep a stable version of SWC instead of using
* package manager overrides, at the cost of installing it twice.
*/
swcImplementation?: any;
};

const isWebContainer = globalThis.process?.versions?.webcontainer;
Expand All @@ -72,6 +78,10 @@ const react = (_options?: Options): PluginOption[] => {
: undefined,
devTarget: _options?.devTarget ?? "es2020",
parserConfig: _options?.parserConfig,
transformFn:
(_options?.swcImplementation?.transform as
| typeof transform
| undefined) ?? transform,
};

return [
Expand Down Expand Up @@ -130,6 +140,7 @@ const react = (_options?: Options): PluginOption[] => {
const refresh = !transformOptions?.ssr && !hmrDisabled;

const result = await transformWithOptions(
options.transformFn,
id,
code,
options.devTarget,
Expand Down Expand Up @@ -192,10 +203,17 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
build: silenceUseClientWarning(userConfig),
}),
transform: (code, _id) =>
transformWithOptions(_id.split("?")[0], code, "esnext", options, {
runtime: "automatic",
importSource: options.jsxImportSource,
}),
transformWithOptions(
options.transformFn,
_id.split("?")[0],
code,
"esnext",
options,
{
runtime: "automatic",
importSource: options.jsxImportSource,
},
),
}
: {
name: "vite:react-swc",
Expand All @@ -215,6 +233,7 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
};

const transformWithOptions = async (
transformFn: typeof transform,
id: string,
code: string,
target: JscTarget,
Expand All @@ -238,7 +257,7 @@ const transformWithOptions = async (

let result: Output;
try {
result = await transform(code, {
result = await transformFn(code, {
filename: id,
swcrc: false,
configFile: false,
Expand Down
Loading