Skip to content

Commit

Permalink
feat: Support reading jsx option from tsconfig (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding authored Aug 26, 2022
1 parent fde5950 commit ecbf5a7
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@types/debug": "^4.1.7",
"@types/jest": "^27.0.2",
"@types/node": "14.14.37",
"esbuild": "^0.14.8",
"esbuild": "^0.14.51",
"istextorbinary": "^6.0.0",
"jest": "^27.3.1",
"lodash": "^4.17.21",
Expand Down
165 changes: 107 additions & 58 deletions pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export default ({
target: defaultOptions.target || 'es2017',
jsxFactory: defaultOptions.jsxFactory,
jsxFragment: defaultOptions.jsxFragment,
jsx: defaultOptions.jsx,
jsxDev: defaultOptions.jsxDev,
sourcemap: sourceMap,
sourcefile: id,
...esbuildOptions,
Expand Down
27 changes: 25 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import JoyCon from 'joycon'
import { parse } from 'jsonc-parser'
import { TransformOptions } from 'esbuild'

const joycon = new JoyCon()

Expand All @@ -12,21 +13,43 @@ joycon.addLoader({
},
})

const jsxValueMap: Record<any, Pick<TransformOptions, 'jsx' | 'jsxDev'>> = {
preserve: {
jsx: 'preserve',
},
react: {
jsx: 'transform',
},
'react-jsx': {
jsx: 'automatic',
},
'react-jsxdev': {
jsx: 'automatic',
jsxDev: true,
},
}

export const getOptions = async (
cwd: string,
tsconfig?: string
): Promise<{ jsxFactory?: string; jsxFragment?: string; target?: string }> => {
): Promise<
Pick<
TransformOptions,
'jsxFactory' | 'jsxFragment' | 'target' | 'jsx' | 'jsxDev'
>
> => {
// This call is cached
const { data, path } = await joycon.load([tsconfig || 'tsconfig.json'], cwd)
if (path && data) {
const { jsxFactory, jsxFragmentFactory, target } =
const { jsxFactory, jsxFragmentFactory, target, jsx } =
data.compilerOptions || {}
return {
jsxFactory,
jsxFragment: jsxFragmentFactory,
// Lowercased value to be compatible with esbuild
// Maybe remove in 3.0, #77
target: target && target.toLowerCase(),
...jsxValueMap[jsx],
}
}
return {}
Expand Down
Loading

0 comments on commit ecbf5a7

Please sign in to comment.