-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check consistency of JSX between swc and tsconfig
- Loading branch information
Showing
9 changed files
with
142 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { RsbuildConfig, RsbuildPlugin } from '@rsbuild/core'; | ||
import { color } from './utils/helper'; | ||
import { logger } from './utils/logger'; | ||
|
||
type PluginReactOptions = { | ||
compilerOptions?: Record<string, any>; | ||
}; | ||
|
||
const mapTsconfigJsxToSwcJsx = (swcJsx: string): string | null => { | ||
switch (swcJsx) { | ||
case 'react-jsx': | ||
case 'react-jsxdev': | ||
return 'automatic'; | ||
case 'react': | ||
return 'classic'; | ||
default: | ||
return 'classic'; | ||
} | ||
}; | ||
|
||
const checkJsx = ({ compilerOptions }: PluginReactOptions): RsbuildPlugin => ({ | ||
name: 'rsbuild:lib-check', | ||
setup(api) { | ||
api.onBeforeEnvironmentCompile(({ environment }) => { | ||
const envName = environment.name; | ||
const config = api.getNormalizedConfig({ | ||
environment: envName, | ||
}); | ||
|
||
const swc = config.tools.swc; | ||
const tsconfigJsx = compilerOptions?.jsx; | ||
// @ts-ignore | ||
const swcJsx = swc.jsc.transform.react.runtime; | ||
const mapped = mapTsconfigJsxToSwcJsx(tsconfigJsx); | ||
if (swcJsx && mapped !== null) { | ||
const matched = mapped === swcJsx; | ||
if (!matched) { | ||
logger.warn( | ||
`You're using ${color.green(`"${swcJsx}"`)} JSX runtime in SWC, but ${color.green(`"${mapped}"`)} in tsconfig.json. This may cause unexpected behavior, considering aligning them.`, | ||
); | ||
} | ||
} | ||
}); | ||
}, | ||
}); | ||
|
||
export const composeCheckConfig = ( | ||
compilerOptions: Record<string, any>, | ||
): RsbuildConfig => { | ||
return { plugins: [checkJsx({ compilerOptions })] }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { join } from 'node:path'; | ||
import stripAnsi from 'strip-ansi'; | ||
import { buildAndGetResults, proxyConsole } from 'test-helper'; | ||
import { expect, test } from 'vitest'; | ||
|
||
test('should receive JSX mismatch warning of SWC with tsconfig', async () => { | ||
const { logs, restore } = proxyConsole(); | ||
const fixturePath = join(__dirname, 'jsx'); | ||
await buildAndGetResults({ fixturePath }); | ||
const logStrings = logs | ||
.map((log) => stripAnsi(log)) | ||
.filter((log) => log.startsWith('warn')) | ||
.sort() | ||
.join('\n'); | ||
|
||
expect(logStrings).toMatchInlineSnapshot(` | ||
"warn You're using "automatic" JSX runtime in SWC, but "classic" in tsconfig.json. This may cause unexpected behavior, considering aligning them. | ||
warn You're using "automatic" JSX runtime in SWC, but "classic" in tsconfig.json. This may cause unexpected behavior, considering aligning them." | ||
`); | ||
|
||
restore(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "check-jsx-test", | ||
"version": "1.0.0", | ||
"private": true, | ||
"type": "module", | ||
"devDependencies": { | ||
"@rsbuild/plugin-react": "^1.1.0", | ||
"@types/react": "^19.0.6", | ||
"react": "^19.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { pluginReact } from '@rsbuild/plugin-react'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; | ||
|
||
export default defineConfig({ | ||
lib: [generateBundleEsmConfig(), generateBundleCjsConfig()], | ||
plugins: [pluginReact()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React from 'react'; | ||
|
||
export const Foo = <div>foo</div>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "@rslib/tsconfig/base", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"jsx": "react" | ||
}, | ||
"include": ["src"] | ||
} |