Skip to content

Commit

Permalink
fix(plugin-react): fix React is not defined when component name is lo…
Browse files Browse the repository at this point in the history
…wercase

When the react variables (such as React__default) of the code in our third-party dependencies are converted to React, and the component name is lowercase, it is not converted to jsx, which will cause React to be undefined after restoring the code.
  • Loading branch information
yulong.xiang committed Feb 10, 2022
1 parent e38be3e commit 5c9c812
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/plugin-react/src/jsx-runtime/restore-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ export async function restoreJSX(
return jsxNotFound
}

const reactJsxRE = new RegExp(
'\\b' + reactAlias + '\\.(createElement|Fragment)\\b',
const reactJsxFragmentRE = new RegExp(
'\\b' + reactAlias + '\\.(Fragment)\\b',
'g'
)
const reactJsxCreatElementRE = new RegExp(
'\\b' + reactAlias + '\\.(createElement)\\b(\\([A-Z]\\w)',
'g'
)

let hasCompiledJsx = false
code = code.replace(reactJsxRE, (_, prop) => {
code = code.replace(reactJsxFragmentRE, (_, prop) => {
hasCompiledJsx = true
// Replace with "React" so JSX can be reverse compiled.
return 'React.' + prop
})
code = code.replace(reactJsxCreatElementRE, (_, prop, prop2) => {
hasCompiledJsx = true
return 'React.' + prop + prop2
})

if (!hasCompiledJsx) {
return jsxNotFound
Expand Down

0 comments on commit 5c9c812

Please sign in to comment.