-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcheck.ts
61 lines (56 loc) · 2.01 KB
/
check.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 })] };
};