Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function getTransformer(opts: { sourceMaps?: boolean }) {
const babel = await babelImport

// Reverse-compile any React.createElement calls
let [ast, isCommonJS] = await viteReactJsx.restoreJSX(babel, code)
let [ast, isCommonJS] = await viteReactJsx.restoreJSX(babel, code, id)

// Then apply the JSX automatic runtime transform
if (ast) {
Expand All @@ -84,6 +84,7 @@ function getTransformer(opts: { sourceMaps?: boolean }) {
const result = await babel.transformFromAstAsync(ast, undefined, {
plugins,
sourceMaps: opts.sourceMaps,
filename: id
})
if (result?.code) {
return {
Expand All @@ -104,6 +105,7 @@ function getTransformer(opts: { sourceMaps?: boolean }) {
[await babelTransformJsx, { runtime: 'automatic' }],
],
sourceMaps: opts.sourceMaps,
filename: id
})
if (res?.code) {
return {
Expand All @@ -129,7 +131,8 @@ let babelRestoreJSX: any
/** Restore JSX from `React.createElement` calls */
async function restoreJSX(
babel: typeof import('@babel/core'),
code: string
code: string,
id: string
): Promise<RestoredJSX> {
const [reactAlias, isCommonJS] = parseReactAlias(code)
const reactJsxRE = new RegExp(
Expand Down Expand Up @@ -163,6 +166,7 @@ async function restoreJSX(
plugins: ['jsx'],
},
plugins: [await babelRestoreJSX],
filename: id
})

return [result?.ast, isCommonJS]
Expand Down