-
Notifications
You must be signed in to change notification settings - Fork 31
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
10 changed files
with
149 additions
and
13 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,61 @@ | ||
import type { RsbuildConfig, RsbuildPlugin } from '@rsbuild/core'; | ||
import type { TsconfigCompilerOptions } from './types'; | ||
import { color } from './utils/helper'; | ||
import { logger } from './utils/logger'; | ||
|
||
type PluginReactOptions = { | ||
tsconfigCompilerOptions?: TsconfigCompilerOptions; | ||
}; | ||
|
||
const mapTsconfigJsxToSwcJsx = (jsx: string | undefined): string | null => { | ||
if (jsx === undefined) { | ||
// 'preserve' is the default value of tsconfig.compilerOptions.jsx | ||
return null; | ||
} | ||
|
||
// Calculate a corresponding SWC JSX config if tsconfig.compilerOptions.jsx is set to React related option. | ||
// Return `null` stands for no need to check. | ||
switch (jsx) { | ||
case 'react-jsx': | ||
case 'react-jsxdev': | ||
return 'automatic'; | ||
case 'react': | ||
return 'classic'; | ||
case 'preserve': | ||
case 'react-native': | ||
// SWC JSX does not support `preserve` as of now. | ||
return null; | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
const checkJsx = ({ | ||
tsconfigCompilerOptions, | ||
}: PluginReactOptions): RsbuildPlugin => ({ | ||
name: 'rsbuild:lib-check', | ||
setup(api) { | ||
api.onBeforeEnvironmentCompile(({ environment }) => { | ||
const config = api.getNormalizedConfig({ | ||
environment: environment.name, | ||
}); | ||
const swc = config.tools.swc; | ||
const tsconfigJsx = tsconfigCompilerOptions?.jsx; | ||
if (swc && !Array.isArray(swc) && typeof swc !== 'function') { | ||
const swcReactRuntime = swc?.jsc?.transform?.react?.runtime || null; | ||
const mapped = mapTsconfigJsxToSwcJsx(tsconfigJsx); | ||
if (mapped !== swcReactRuntime) { | ||
logger.warn( | ||
`JSX runtime is set to ${color.green(`${JSON.stringify(swcReactRuntime)}`)} in SWC, but got ${color.green(`${JSON.stringify(tsconfigJsx)}`)} in tsconfig.json. This may cause unexpected behavior, considering aligning them.`, | ||
); | ||
} | ||
} | ||
}); | ||
}, | ||
}); | ||
|
||
export const composeCheckConfig = ( | ||
compilerOptions: TsconfigCompilerOptions, | ||
): RsbuildConfig => { | ||
return { plugins: [checkJsx({ tsconfigCompilerOptions: 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
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 JSX runtime is set to "automatic" in SWC, but got undefined in tsconfig.json. This may cause unexpected behavior, considering aligning them. | ||
warn JSX runtime is set to "automatic" in SWC, but got undefined 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"] | ||
} |