Skip to content

perf: use createElementVNode #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions packages/babel-plugin-jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default declare<VueJSXPluginOptions, BabelCore.PluginObj<State>>(
if (hasJSX(path)) {
const importNames = [
'createVNode',
'createElementVNode',
'Fragment',
'resolveComponent',
'withDirectives',
Expand All @@ -73,6 +74,10 @@ export default declare<VueJSXPluginOptions, BabelCore.PluginObj<State>>(
'vModelDynamic',
'resolveDirective',
'mergeProps',
'normalizeProps',
'normalizeClass',
'normalizeStyle',
'guardReactiveProps',
'createTextVNode',
'isVNode',
];
Expand Down Expand Up @@ -179,6 +184,7 @@ export default declare<VueJSXPluginOptions, BabelCore.PluginObj<State>>(

if (pragma) {
state.set('createVNode', () => t.identifier(pragma));
state.set('createElementVNode', () => t.identifier(pragma));
}

if (file.ast.comments) {
Expand Down
45 changes: 43 additions & 2 deletions packages/babel-plugin-jsx/src/transform-vue-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,19 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
);
} else {
// single no need for a mergeProps call
propsExpression = mergeArgs[0];
if (isComponent) {
// createVNode already normalizes props
propsExpression = mergeArgs[0];
} else {
propsExpression = t.callExpression(
createIdentifier(state, 'normalizeProps'),
[
t.callExpression(createIdentifier(state, 'guardReactiveProps'), [
mergeArgs[0],
]),
]
);
}
}
} else if (properties.length) {
// single no need for spread
Expand All @@ -320,6 +332,35 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
propsExpression = t.objectExpression(
dedupeProperties(properties, mergeProps)
);
for (let i = 0; i < propsExpression.properties.length; i++) {
const property = propsExpression.properties[i];
if (
!t.isObjectProperty(property) ||
!t.isStringLiteral(property.key) ||
!t.isExpression(property.value)
) {
continue;
}
// TODO: if isConstant, pre-normalize class and style during build
if (property.key.value === 'class') {
if (!t.isStringLiteral(property.value)) {
property.value = t.callExpression(
createIdentifier(state, 'normalizeClass'),
[property.value]
);
}
} else if (property.key.value === 'style') {
if (
!t.isStringLiteral(property.value) &&
!t.isObjectExpression(property.value)
) {
property.value = t.callExpression(
createIdentifier(state, 'normalizeStyle'),
[property.value]
);
}
}
}
}
}

Expand Down Expand Up @@ -553,7 +594,7 @@ const transformJSXElement = (
}

const createVNode = t.callExpression(
createIdentifier(state, 'createVNode'),
createIdentifier(state, isComponent ? 'createVNode' : 'createElementVNode'),
[
tag,
props,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`resolve type > runtime props > basic 1`] = `
"import { createVNode as _createVNode } from "vue";
"import { createElementVNode as _createElementVNode } from "vue";
interface Props {
foo?: string;
}
const App = defineComponent((props: Props) => _createVNode("div", null, null), {
const App = defineComponent((props: Props) => _createElementVNode("div", null, null), {
props: {
foo: {
type: String,
Expand Down
Loading