@vitejs/plugin-react 2.2.0-beta.0 → 3.0.0-alpha.0
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/index.cjs +28 -116
- package/dist/index.d.ts +0 -5
- package/dist/index.mjs +21 -103
- package/package.json +5 -6
- package/dist/chunks/babel-restore-jsx.cjs +0 -138
- package/dist/chunks/babel-restore-jsx.mjs +0 -136
- package/src/babel.d.ts +0 -4
- package/src/fast-refresh.ts +0 -155
- package/src/index.ts +0 -479
- package/src/jsx-runtime/babel-import-to-require.ts +0 -35
- package/src/jsx-runtime/babel-restore-jsx.spec.ts +0 -132
- package/src/jsx-runtime/babel-restore-jsx.ts +0 -232
- package/src/jsx-runtime/restore-jsx.spec.ts +0 -147
- package/src/jsx-runtime/restore-jsx.ts +0 -74
@@ -1,136 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
|
3
|
-
* @license GNU General Public License v3.0
|
4
|
-
*/
|
5
|
-
function babelRestoreJsx({ types: t }, { reactAlias = "React" }) {
|
6
|
-
function getJSXNode(node) {
|
7
|
-
if (!isReactCreateElement(node)) {
|
8
|
-
return null;
|
9
|
-
}
|
10
|
-
const [nameNode, propsNode, ...childNodes] = node.arguments;
|
11
|
-
const name = getJSXName(nameNode);
|
12
|
-
if (name == null) {
|
13
|
-
return null;
|
14
|
-
}
|
15
|
-
const props = getJSXProps(propsNode);
|
16
|
-
if (props == null) {
|
17
|
-
return null;
|
18
|
-
}
|
19
|
-
const children = getJSXChildren(childNodes);
|
20
|
-
if (children == null) {
|
21
|
-
return null;
|
22
|
-
}
|
23
|
-
if (t.isJSXMemberExpression(name) && t.isJSXIdentifier(name.object) && name.object.name === reactAlias && name.property.name === "Fragment") {
|
24
|
-
return t.jsxFragment(
|
25
|
-
t.jsxOpeningFragment(),
|
26
|
-
t.jsxClosingFragment(),
|
27
|
-
children
|
28
|
-
);
|
29
|
-
}
|
30
|
-
const selfClosing = children.length === 0;
|
31
|
-
const startTag = t.jsxOpeningElement(name, props, selfClosing);
|
32
|
-
startTag.loc = node.loc;
|
33
|
-
const endTag = selfClosing ? null : t.jsxClosingElement(name);
|
34
|
-
return t.jsxElement(startTag, endTag, children, selfClosing);
|
35
|
-
}
|
36
|
-
function getJSXName(node) {
|
37
|
-
if (node == null) {
|
38
|
-
return null;
|
39
|
-
}
|
40
|
-
const name = getJSXIdentifier(node, true);
|
41
|
-
if (name != null) {
|
42
|
-
return name;
|
43
|
-
}
|
44
|
-
if (!t.isMemberExpression(node)) {
|
45
|
-
return null;
|
46
|
-
}
|
47
|
-
const object = getJSXName(node.object);
|
48
|
-
const property = getJSXName(node.property);
|
49
|
-
if (object == null || property == null) {
|
50
|
-
return null;
|
51
|
-
}
|
52
|
-
return t.jsxMemberExpression(object, property);
|
53
|
-
}
|
54
|
-
function getJSXProps(node) {
|
55
|
-
if (node == null || isNullLikeNode(node)) {
|
56
|
-
return [];
|
57
|
-
}
|
58
|
-
if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name: "_extends" })) {
|
59
|
-
const props = node.arguments.map(getJSXProps);
|
60
|
-
if (props.every((prop) => prop != null)) {
|
61
|
-
return [].concat(...props);
|
62
|
-
}
|
63
|
-
}
|
64
|
-
if (!t.isObjectExpression(node) && t.isExpression(node))
|
65
|
-
return [t.jsxSpreadAttribute(node)];
|
66
|
-
if (!isPlainObjectExpression(node)) {
|
67
|
-
return null;
|
68
|
-
}
|
69
|
-
return node.properties.map(
|
70
|
-
(prop) => t.isObjectProperty(prop) ? t.jsxAttribute(
|
71
|
-
getJSXIdentifier(prop.key),
|
72
|
-
getJSXAttributeValue(prop.value)
|
73
|
-
) : t.jsxSpreadAttribute(prop.argument)
|
74
|
-
).filter(
|
75
|
-
(prop) => t.isJSXIdentifier(prop.name) ? prop.name.name !== "__self" && prop.name.name !== "__source" : true
|
76
|
-
);
|
77
|
-
}
|
78
|
-
function getJSXChild(node) {
|
79
|
-
if (t.isStringLiteral(node)) {
|
80
|
-
return t.jsxText(node.value);
|
81
|
-
}
|
82
|
-
if (isReactCreateElement(node)) {
|
83
|
-
return getJSXNode(node);
|
84
|
-
}
|
85
|
-
if (t.isExpression(node)) {
|
86
|
-
return t.jsxExpressionContainer(node);
|
87
|
-
}
|
88
|
-
return null;
|
89
|
-
}
|
90
|
-
function getJSXChildren(nodes) {
|
91
|
-
const children = nodes.filter((node) => !isNullLikeNode(node)).map(getJSXChild);
|
92
|
-
if (children.some((child) => child == null)) {
|
93
|
-
return null;
|
94
|
-
}
|
95
|
-
return children;
|
96
|
-
}
|
97
|
-
function getJSXIdentifier(node, tag = false) {
|
98
|
-
if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
|
99
|
-
return t.jsxIdentifier(node.name);
|
100
|
-
}
|
101
|
-
if (t.isStringLiteral(node)) {
|
102
|
-
return t.jsxIdentifier(node.value);
|
103
|
-
}
|
104
|
-
return null;
|
105
|
-
}
|
106
|
-
function getJSXAttributeValue(node) {
|
107
|
-
if (t.isStringLiteral(node)) {
|
108
|
-
return node;
|
109
|
-
}
|
110
|
-
if (t.isJSXElement(node)) {
|
111
|
-
return node;
|
112
|
-
}
|
113
|
-
if (t.isExpression(node)) {
|
114
|
-
return t.jsxExpressionContainer(node);
|
115
|
-
}
|
116
|
-
return null;
|
117
|
-
}
|
118
|
-
const isReactCreateElement = (node) => t.isCallExpression(node) && t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.object, { name: reactAlias }) && t.isIdentifier(node.callee.property, { name: "createElement" }) && !node.callee.computed;
|
119
|
-
const isNullLikeNode = (node) => t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" });
|
120
|
-
const isPlainObjectExpression = (node) => t.isObjectExpression(node) && node.properties.every(
|
121
|
-
(property) => t.isSpreadElement(property) || t.isObjectProperty(property, { computed: false }) && getJSXIdentifier(property.key) != null && getJSXAttributeValue(property.value) != null
|
122
|
-
);
|
123
|
-
return {
|
124
|
-
visitor: {
|
125
|
-
CallExpression(path) {
|
126
|
-
const node = getJSXNode(path.node);
|
127
|
-
if (node == null) {
|
128
|
-
return null;
|
129
|
-
}
|
130
|
-
path.replaceWith(node);
|
131
|
-
}
|
132
|
-
}
|
133
|
-
};
|
134
|
-
}
|
135
|
-
|
136
|
-
export { babelRestoreJsx as default };
|
package/src/babel.d.ts
DELETED
package/src/fast-refresh.ts
DELETED
@@ -1,155 +0,0 @@
|
|
1
|
-
import fs from 'node:fs'
|
2
|
-
import path from 'node:path'
|
3
|
-
import { createRequire } from 'node:module'
|
4
|
-
import type { types as t } from '@babel/core'
|
5
|
-
|
6
|
-
export const runtimePublicPath = '/@react-refresh'
|
7
|
-
|
8
|
-
const _require = createRequire(import.meta.url)
|
9
|
-
const reactRefreshDir = path.dirname(
|
10
|
-
_require.resolve('react-refresh/package.json')
|
11
|
-
)
|
12
|
-
const runtimeFilePath = path.join(
|
13
|
-
reactRefreshDir,
|
14
|
-
'cjs/react-refresh-runtime.development.js'
|
15
|
-
)
|
16
|
-
|
17
|
-
export const runtimeCode = `
|
18
|
-
const exports = {}
|
19
|
-
${fs.readFileSync(runtimeFilePath, 'utf-8')}
|
20
|
-
function debounce(fn, delay) {
|
21
|
-
let handle
|
22
|
-
return () => {
|
23
|
-
clearTimeout(handle)
|
24
|
-
handle = setTimeout(fn, delay)
|
25
|
-
}
|
26
|
-
}
|
27
|
-
exports.performReactRefresh = debounce(exports.performReactRefresh, 16)
|
28
|
-
export default exports
|
29
|
-
`
|
30
|
-
|
31
|
-
export const preambleCode = `
|
32
|
-
import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
|
33
|
-
RefreshRuntime.injectIntoGlobalHook(window)
|
34
|
-
window.$RefreshReg$ = () => {}
|
35
|
-
window.$RefreshSig$ = () => (type) => type
|
36
|
-
window.__vite_plugin_react_preamble_installed__ = true
|
37
|
-
`
|
38
|
-
|
39
|
-
const header = `
|
40
|
-
import RefreshRuntime from "${runtimePublicPath}";
|
41
|
-
|
42
|
-
let prevRefreshReg;
|
43
|
-
let prevRefreshSig;
|
44
|
-
|
45
|
-
if (import.meta.hot) {
|
46
|
-
if (!window.__vite_plugin_react_preamble_installed__) {
|
47
|
-
throw new Error(
|
48
|
-
"@vitejs/plugin-react can't detect preamble. Something is wrong. " +
|
49
|
-
"See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
|
50
|
-
);
|
51
|
-
}
|
52
|
-
|
53
|
-
prevRefreshReg = window.$RefreshReg$;
|
54
|
-
prevRefreshSig = window.$RefreshSig$;
|
55
|
-
window.$RefreshReg$ = (type, id) => {
|
56
|
-
RefreshRuntime.register(type, __SOURCE__ + " " + id)
|
57
|
-
};
|
58
|
-
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
59
|
-
}`.replace(/[\n]+/gm, '')
|
60
|
-
|
61
|
-
const timeout = `
|
62
|
-
if (!window.__vite_plugin_react_timeout) {
|
63
|
-
window.__vite_plugin_react_timeout = setTimeout(() => {
|
64
|
-
window.__vite_plugin_react_timeout = 0;
|
65
|
-
RefreshRuntime.performReactRefresh();
|
66
|
-
}, 30);
|
67
|
-
}
|
68
|
-
`
|
69
|
-
|
70
|
-
const footer = `
|
71
|
-
if (import.meta.hot) {
|
72
|
-
window.$RefreshReg$ = prevRefreshReg;
|
73
|
-
window.$RefreshSig$ = prevRefreshSig;
|
74
|
-
|
75
|
-
__ACCEPT__
|
76
|
-
}`
|
77
|
-
|
78
|
-
const checkAndAccept = `
|
79
|
-
function isReactRefreshBoundary(mod) {
|
80
|
-
if (mod == null || typeof mod !== 'object') {
|
81
|
-
return false;
|
82
|
-
}
|
83
|
-
let hasExports = false;
|
84
|
-
let areAllExportsComponents = true;
|
85
|
-
for (const exportName in mod) {
|
86
|
-
hasExports = true;
|
87
|
-
if (exportName === '__esModule') {
|
88
|
-
continue;
|
89
|
-
}
|
90
|
-
const desc = Object.getOwnPropertyDescriptor(mod, exportName);
|
91
|
-
if (desc && desc.get) {
|
92
|
-
// Don't invoke getters as they may have side effects.
|
93
|
-
return false;
|
94
|
-
}
|
95
|
-
const exportValue = mod[exportName];
|
96
|
-
if (!RefreshRuntime.isLikelyComponentType(exportValue)) {
|
97
|
-
areAllExportsComponents = false;
|
98
|
-
}
|
99
|
-
}
|
100
|
-
return hasExports && areAllExportsComponents;
|
101
|
-
}
|
102
|
-
|
103
|
-
import.meta.hot.accept(mod => {
|
104
|
-
if (isReactRefreshBoundary(mod)) {
|
105
|
-
${timeout}
|
106
|
-
} else {
|
107
|
-
import.meta.hot.invalidate();
|
108
|
-
}
|
109
|
-
});
|
110
|
-
`
|
111
|
-
|
112
|
-
export function addRefreshWrapper(
|
113
|
-
code: string,
|
114
|
-
id: string,
|
115
|
-
accept: boolean
|
116
|
-
): string {
|
117
|
-
return (
|
118
|
-
header.replace('__SOURCE__', JSON.stringify(id)) +
|
119
|
-
code +
|
120
|
-
footer.replace('__ACCEPT__', accept ? checkAndAccept : timeout)
|
121
|
-
)
|
122
|
-
}
|
123
|
-
|
124
|
-
export function isRefreshBoundary(ast: t.File): boolean {
|
125
|
-
// Every export must be a potential React component.
|
126
|
-
// We'll also perform a runtime check that's more robust as well (isLikelyComponentType).
|
127
|
-
return ast.program.body.every((node) => {
|
128
|
-
if (node.type !== 'ExportNamedDeclaration') {
|
129
|
-
return true
|
130
|
-
}
|
131
|
-
const { declaration, specifiers } = node
|
132
|
-
if (declaration) {
|
133
|
-
if (declaration.type === 'ClassDeclaration') return false
|
134
|
-
if (declaration.type === 'VariableDeclaration') {
|
135
|
-
return declaration.declarations.every((variable) =>
|
136
|
-
isComponentLikeIdentifier(variable.id)
|
137
|
-
)
|
138
|
-
}
|
139
|
-
if (declaration.type === 'FunctionDeclaration') {
|
140
|
-
return !!declaration.id && isComponentLikeIdentifier(declaration.id)
|
141
|
-
}
|
142
|
-
}
|
143
|
-
return specifiers.every((spec) => {
|
144
|
-
return isComponentLikeIdentifier(spec.exported)
|
145
|
-
})
|
146
|
-
})
|
147
|
-
}
|
148
|
-
|
149
|
-
function isComponentLikeIdentifier(node: t.Node): boolean {
|
150
|
-
return node.type === 'Identifier' && isComponentLikeName(node.name)
|
151
|
-
}
|
152
|
-
|
153
|
-
function isComponentLikeName(name: string): boolean {
|
154
|
-
return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z'
|
155
|
-
}
|