@vitejs/plugin-react 1.3.2 → 2.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/babel-restore-jsx.cjs +125 -0
- package/dist/chunks/babel-restore-jsx.mjs +123 -0
- package/dist/{index.js → index.cjs} +108 -248
- package/dist/index.d.ts +69 -62
- package/dist/index.mjs +387 -0
- package/package.json +25 -13
- package/src/fast-refresh.ts +4 -2
- package/src/index.ts +77 -36
- package/src/jsx-runtime/babel-import-to-require.ts +2 -3
- package/src/jsx-runtime/babel-restore-jsx.spec.ts +2 -1
- package/src/jsx-runtime/restore-jsx.spec.ts +3 -2
- package/src/jsx-runtime/restore-jsx.ts +18 -13
@@ -1,5 +1,4 @@
|
|
1
1
|
import type * as babelCore from '@babel/core'
|
2
|
-
import type { types as t, Visitor } from '@babel/core'
|
3
2
|
|
4
3
|
/**
|
5
4
|
* Replace this:
|
@@ -11,13 +10,13 @@ import type { types as t, Visitor } from '@babel/core'
|
|
11
10
|
* var _jsx = require("react/jsx-runtime").jsx
|
12
11
|
*/
|
13
12
|
export function babelImportToRequire({ types: t }: typeof babelCore): {
|
14
|
-
visitor: Visitor
|
13
|
+
visitor: babelCore.Visitor
|
15
14
|
} {
|
16
15
|
return {
|
17
16
|
visitor: {
|
18
17
|
ImportDeclaration(path) {
|
19
18
|
const decl = path.node
|
20
|
-
const spec = decl.specifiers[0] as
|
19
|
+
const spec = decl.specifiers[0] as babelCore.types.ImportSpecifier
|
21
20
|
|
22
21
|
path.replaceWith(
|
23
22
|
t.variableDeclaration('var', [
|
@@ -1,9 +1,10 @@
|
|
1
|
-
import { restoreJSX } from './restore-jsx'
|
2
1
|
import * as babel from '@babel/core'
|
2
|
+
import { describe, expect, it } from 'vitest'
|
3
|
+
import { restoreJSX } from './restore-jsx'
|
3
4
|
|
4
5
|
async function jsx(sourceCode: string) {
|
5
6
|
const [ast] = await restoreJSX(babel, sourceCode, 'test.js')
|
6
|
-
if (ast
|
7
|
+
if (ast == null) {
|
7
8
|
return ast
|
8
9
|
}
|
9
10
|
const { code } = await babel.transformFromAstAsync(ast, null, {
|
@@ -1,24 +1,32 @@
|
|
1
1
|
import type * as babelCore from '@babel/core'
|
2
|
-
import type { PluginItem, types as t } from '@babel/core'
|
3
2
|
|
4
|
-
type RestoredJSX = [
|
3
|
+
type RestoredJSX = [
|
4
|
+
result: babelCore.types.File | null | undefined,
|
5
|
+
isCommonJS: boolean
|
6
|
+
]
|
5
7
|
|
6
|
-
let babelRestoreJSX: Promise<PluginItem> | undefined
|
8
|
+
let babelRestoreJSX: Promise<babelCore.PluginItem> | undefined
|
7
9
|
|
8
10
|
const jsxNotFound: RestoredJSX = [null, false]
|
9
11
|
|
12
|
+
async function getBabelRestoreJSX() {
|
13
|
+
if (!babelRestoreJSX)
|
14
|
+
babelRestoreJSX = import('./babel-restore-jsx').then((r) => {
|
15
|
+
const fn = r.default
|
16
|
+
if ('default' in fn)
|
17
|
+
// @ts-expect-error
|
18
|
+
return fn.default
|
19
|
+
return fn
|
20
|
+
})
|
21
|
+
return babelRestoreJSX
|
22
|
+
}
|
23
|
+
|
10
24
|
/** Restore JSX from `React.createElement` calls */
|
11
25
|
export async function restoreJSX(
|
12
26
|
babel: typeof babelCore,
|
13
27
|
code: string,
|
14
28
|
filename: string
|
15
29
|
): Promise<RestoredJSX> {
|
16
|
-
// Avoid parsing the optimized react-dom since it will never
|
17
|
-
// contain compiled JSX and it's a pretty big file (800kb).
|
18
|
-
if (filename.includes('/.vite/react-dom.js')) {
|
19
|
-
return jsxNotFound
|
20
|
-
}
|
21
|
-
|
22
30
|
const [reactAlias, isCommonJS] = parseReactAlias(code)
|
23
31
|
|
24
32
|
if (!reactAlias) {
|
@@ -56,8 +64,6 @@ export async function restoreJSX(
|
|
56
64
|
return jsxNotFound
|
57
65
|
}
|
58
66
|
|
59
|
-
babelRestoreJSX ||= import('./babel-restore-jsx')
|
60
|
-
|
61
67
|
const result = await babel.transformAsync(code, {
|
62
68
|
babelrc: false,
|
63
69
|
configFile: false,
|
@@ -67,8 +73,7 @@ export async function restoreJSX(
|
|
67
73
|
parserOpts: {
|
68
74
|
plugins: ['jsx']
|
69
75
|
},
|
70
|
-
|
71
|
-
plugins: [(await babelRestoreJSX).default]
|
76
|
+
plugins: [await getBabelRestoreJSX()]
|
72
77
|
})
|
73
78
|
|
74
79
|
return [result?.ast, isCommonJS]
|