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

refactor: react jsx runtime respect tsconfig.json #561

Merged
merged 5 commits into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 3 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/builder/bundle/index.ts
Expand Up @@ -69,7 +69,10 @@ export default async (opts: {
presetEnv: {
targets: getBundleTargets(config),
},
presetReact: getBabelPresetReactOpts(opts.configProvider.pkg),
presetReact: getBabelPresetReactOpts(
opts.configProvider.pkg,
opts.cwd,
),
presetTypeScript: {},
pluginTransformRuntime: {},
pluginLockCoreJS: {},
Expand Down
6 changes: 4 additions & 2 deletions src/builder/bundless/loaders/javascript/babel.ts
Expand Up @@ -39,7 +39,10 @@ const babelTransformer: IJSTransformer = function (content) {
targets: getBundlessTargets(this.config),
modules: this.config.format === IFatherBundlessTypes.ESM ? false : 'auto',
},
presetReact: getBabelPresetReactOpts(this.pkg),
presetReact: getBabelPresetReactOpts(
this.pkg,
path.dirname(this.paths.fileAbsPath),
),
presetTypeScript: {},
};

Expand Down Expand Up @@ -68,7 +71,6 @@ const babelTransformer: IJSTransformer = function (content) {
version: this.pkg.dependencies?.['@babel/runtime'],
};
}
// TODO: recommend install @babel/runtime in doctor
const { code, map } = transform(content, {
filename: this.paths.fileAbsPath,
cwd: this.paths.cwd,
Expand Down
5 changes: 4 additions & 1 deletion src/builder/bundless/loaders/javascript/swc.ts
Expand Up @@ -143,7 +143,10 @@ const swcTransformer: IJSTransformer = async function (content) {
...(!isTSFile && isJSXFile ? { jsx: true } : {}),
},
transform: {
react: getSWCTransformReactOpts(this.pkg),
react: getSWCTransformReactOpts(
this.pkg,
path.dirname(this.paths.fileAbsPath),
),
},
},
module: {
Expand Down
42 changes: 24 additions & 18 deletions src/builder/utils.ts
Expand Up @@ -6,6 +6,7 @@ import {
IFatherJSTransformerTypes,
IFatherPlatformTypes,
} from '../types';
import { getTsconfig } from './bundless/dts';
import type { IBundlessConfig } from './config';

export function addSourceMappingUrl(code: string, loc: string) {
Expand All @@ -16,16 +17,7 @@ export function addSourceMappingUrl(code: string, loc: string) {
);
}

export function getIsLTRReact17(pkg: IApi['pkg']) {
const reactVer = Object.assign(
{},
pkg.dependencies,
pkg.peerDependencies,
).react;
return semver.subset(reactVer, '>=17.0.0-0');
}

export function getBaseTransformReactOpts(pkg: IApi['pkg']) {
export function getBaseTransformReactOpts(pkg: IApi['pkg'], cwd: string) {
const reactVer = Object.assign(
{},
pkg.dependencies,
Expand All @@ -34,30 +26,44 @@ export function getBaseTransformReactOpts(pkg: IApi['pkg']) {
let opts: Record<string, any> = {};

if (reactVer) {
const isLTRReact17 = getIsLTRReact17(pkg);
let isNewJSX: boolean;
const tsconfig = getTsconfig(cwd);

/* istanbul ignore else -- @preserve */
if (tsconfig) {
// respect tsconfig first, `4` means `react-jsx`
isNewJSX = tsconfig.options?.jsx === 4;
} else {
// fallback to match react versions which support new JSX transform
// ref: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#how-to-upgrade-to-the-new-jsx-transform
isNewJSX = semver.subset(
reactVer,
'>=17.0.0-0||^16.14.0||^15.7.0||^0.14.10',
);
}

opts = {
// force use production mode, to make sure dist of dev/build are consistent
// ref: https://github.com/umijs/umi/blob/6f63435d42f8ef7110f73dcf33809e6cda750332/packages/babel-preset-umi/src/index.ts#L45
development: false,
// use legacy jsx runtime for react@<17
runtime: isLTRReact17 ? 'automatic' : 'classic',
...(isLTRReact17 ? {} : { importSource: undefined }),
// set jsx runtime automatically
runtime: isNewJSX ? 'automatic' : 'classic',
...(isNewJSX ? {} : { importSource: undefined }),
};
}

return opts;
}

export function getBabelPresetReactOpts(pkg: IApi['pkg']) {
export function getBabelPresetReactOpts(pkg: IApi['pkg'], cwd: string) {
return {
...getBaseTransformReactOpts(pkg),
...getBaseTransformReactOpts(pkg, cwd),
};
}

export function getSWCTransformReactOpts(pkg: IApi['pkg']) {
export function getSWCTransformReactOpts(pkg: IApi['pkg'], cwd: string) {
return {
...getBaseTransformReactOpts(pkg),
...getBaseTransformReactOpts(pkg, cwd),
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/build/automatic-jsx/package.json
@@ -1,5 +1,5 @@
{
"dependencies": {
"react": "^17.0.0"
"react": "^16.14.0"
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/build/automatic-jsx/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"jsx": "react-jsx"
}
}