Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Commit e936940

Browse files
authored
feat: add parserConfig option (#186)
1 parent b68fcd2 commit e936940

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Add parserConfig option
6+
7+
This will unlock to use the plugin in some use cases where the original source code is not in TS. Using this option to keep using JSX inside `.js` files is highly discouraged and can be removed in any future version.
8+
59
## 3.5.0
610

711
### Update peer dependency range to target Vite 5

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ Enable TypeScript decorators. Requires `experimentalDecorators` in tsconfig.
5656
react({ tsDecorators: true });
5757
```
5858

59-
## plugins
59+
### plugins
6060

6161
Use SWC plugins. Enable SWC at build time.
6262

6363
```ts
6464
react({ plugins: [["@swc/plugin-styled-components", {}]] });
6565
```
6666

67-
## devTarget
67+
### devTarget
6868

6969
Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
7070

@@ -76,6 +76,23 @@ For production target, see https://vitejs.dev/config/build-options.html#build-ta
7676
react({ devTarget: "es2022" });
7777
```
7878

79+
### parserConfig
80+
81+
Override the default include list (.ts, .tsx, .mts, .jsx, .mdx).
82+
83+
This requires to redefine the config for any file you want to be included (ts, mdx, ...).
84+
85+
If you want to trigger fast refresh on compiled JS, use `jsx: true`. Exclusion of node_modules should be handled by the function if needed. Using this option to use JSX inside `.js` files is highly discouraged and can be removed in any future version.
86+
87+
```ts
88+
react({
89+
parserConfig(id) {
90+
if (id.endsWith(".res")) return { syntax: "ecmascript", jsx: true };
91+
if (id.endsWith(".ts")) return { syntax: "typescript", tsx: false };
92+
},
93+
});
94+
```
95+
7996
## Consistent components exports
8097

8198
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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ type Options = {
5050
* @default "es2020"
5151
*/
5252
devTarget?: JscTarget;
53+
/**
54+
* Override the default include list (.ts, .tsx, .mts, .jsx, .mdx).
55+
* This requires to redefine the config for any file you want to be included.
56+
* If you want to trigger fast refresh on compiled JS, use `jsx: true`.
57+
* Exclusion of node_modules should be handled by the function if needed.
58+
*/
59+
parserConfig?: (id: string) => ParserConfig | undefined;
5360
};
5461

5562
const isWebContainer = globalThis.process?.versions?.webcontainer;
@@ -63,6 +70,7 @@ const react = (_options?: Options): PluginOption[] => {
6370
? _options?.plugins.map((el): typeof el => [resolve(el[0]), el[1]])
6471
: undefined,
6572
devTarget: _options?.devTarget ?? "es2020",
73+
parserConfig: _options?.parserConfig,
6674
};
6775

6876
return [
@@ -204,7 +212,9 @@ const transformWithOptions = async (
204212
reactConfig: ReactConfig,
205213
) => {
206214
const decorators = options?.tsDecorators ?? false;
207-
const parser: ParserConfig | undefined = id.endsWith(".tsx")
215+
const parser: ParserConfig | undefined = options.parserConfig
216+
? options.parserConfig(id)
217+
: id.endsWith(".tsx")
208218
? { syntax: "typescript", tsx: true, decorators }
209219
: id.endsWith(".ts") || id.endsWith(".mts")
210220
? { syntax: "typescript", tsx: false, decorators }

0 commit comments

Comments
 (0)