Skip to content

Commit ba0583c

Browse files
committed
don't gate behind optimize
1 parent e025421 commit ba0583c

File tree

4 files changed

+46
-47
lines changed

4 files changed

+46
-47
lines changed

packages/babel-plugin-jsx/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export default declare<VueJSXPluginOptions, BabelCore.PluginObj<State>>(
184184

185185
if (pragma) {
186186
state.set('createVNode', () => t.identifier(pragma));
187+
state.set('createElementVNode', () => t.identifier(pragma));
187188
}
188189

189190
if (file.ast.comments) {

packages/babel-plugin-jsx/src/transform-vue-jsx.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
7878

7979
const mergeArgs: (t.CallExpression | t.ObjectExpression | t.Identifier)[] =
8080
[];
81-
const { mergeProps = true, optimize = false } = state.opts;
81+
const { mergeProps = true } = state.opts;
8282
props.forEach((prop) => {
8383
if (prop.isJSXAttribute()) {
8484
let name = getJSXAttributeName(prop);
@@ -310,7 +310,19 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
310310
);
311311
} else {
312312
// single no need for a mergeProps call
313-
propsExpression = mergeArgs[0];
313+
if (isComponent) {
314+
// createVNode already normalizes props
315+
propsExpression = mergeArgs[0];
316+
} else {
317+
propsExpression = t.callExpression(
318+
createIdentifier(state, 'normalizeProps'),
319+
[
320+
t.callExpression(createIdentifier(state, 'guardReactiveProps'), [
321+
mergeArgs[0],
322+
]),
323+
]
324+
);
325+
}
314326
}
315327
} else if (properties.length) {
316328
// single no need for spread
@@ -320,34 +332,25 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
320332
propsExpression = t.objectExpression(
321333
dedupeProperties(properties, mergeProps)
322334
);
323-
if (optimize) {
324-
if (hasClassBinding) {
325-
const klass = propsExpression.properties.find(
326-
(prop) =>
327-
t.isObjectProperty(prop) &&
328-
t.isStringLiteral(prop.key) &&
329-
prop.key.value === 'class'
335+
for (let i = 0; i < propsExpression.properties.length; i++) {
336+
const property = propsExpression.properties[i];
337+
if (
338+
!t.isObjectProperty(property) ||
339+
!t.isStringLiteral(property.key) ||
340+
!t.isExpression(property.value) ||
341+
isConstant(property.value)
342+
)
343+
continue;
344+
if (property.key.value === 'class') {
345+
property.value = t.callExpression(
346+
createIdentifier(state, 'normalizeClass'),
347+
[property.value]
330348
);
331-
if (t.isObjectProperty(klass)) {
332-
klass.value = t.callExpression(
333-
createIdentifier(state, 'normalizeClass'),
334-
[klass.value as any]
335-
);
336-
}
337-
}
338-
if (hasStyleBinding) {
339-
const style = propsExpression.properties.find(
340-
(prop) =>
341-
t.isObjectProperty(prop) &&
342-
t.isStringLiteral(prop.key) &&
343-
prop.key.value === 'style'
349+
} else if (property.key.value === 'style') {
350+
property.value = t.callExpression(
351+
createIdentifier(state, 'normalizeStyle'),
352+
[property.value]
344353
);
345-
if (t.isObjectProperty(style)) {
346-
style.value = t.callExpression(
347-
createIdentifier(state, 'normalizeStyle'),
348-
[style.value as any]
349-
);
350-
}
351354
}
352355
}
353356
}
@@ -583,12 +586,7 @@ const transformJSXElement = (
583586
}
584587

585588
const createVNode = t.callExpression(
586-
optimize
587-
? createIdentifier(
588-
state,
589-
isComponent ? 'createVNode' : 'createElementVNode'
590-
)
591-
: createIdentifier(state, 'createVNode'),
589+
createIdentifier(state, isComponent ? 'createVNode' : 'createElementVNode'),
592590
[
593591
tag,
594592
props,

packages/babel-plugin-jsx/test/__snapshots__/resolve-type.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`resolve type > runtime props > basic 1`] = `
4-
"import { createVNode as _createVNode } from "vue";
4+
"import { createElementVNode as _createElementVNode } from "vue";
55
interface Props {
66
foo?: string;
77
}
8-
const App = defineComponent((props: Props) => _createVNode("div", null, null), {
8+
const App = defineComponent((props: Props) => _createElementVNode("div", null, null), {
99
props: {
1010
foo: {
1111
type: String,

packages/babel-plugin-jsx/test/__snapshots__/snapshot.test.ts.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`_Fragment already imported > _Fragment already imported 1`] = `
4-
"import { Fragment as _Fragment, Fragment as _Fragment2, createTextVNode as _createTextVNode, createVNode as _createVNode } from 'vue';
5-
const Root1 = () => _createVNode(_Fragment2, null, [_createTextVNode("root1")]);
6-
const Root2 = () => _createVNode(_Fragment, null, [_createTextVNode("root2")]);"
4+
"import { Fragment as _Fragment, Fragment as _Fragment2, createTextVNode as _createTextVNode, createElementVNode as _createElementVNode } from 'vue';
5+
const Root1 = () => _createElementVNode(_Fragment2, null, [_createTextVNode("root1")]);
6+
const Root2 = () => _createElementVNode(_Fragment, null, [_createTextVNode("root2")]);"
77
`;
88

99
exports[`MereProps Order > MereProps Order 1`] = `
@@ -125,8 +125,8 @@ _withDirectives(_createElementVNode("input", {
125125
`;
126126
127127
exports[`isCustomElement > isCustomElement 1`] = `
128-
"import { createTextVNode as _createTextVNode, createVNode as _createVNode } from "vue";
129-
_createVNode("foo", null, [_createVNode("span", null, [_createTextVNode("foo")])]);"
128+
"import { createTextVNode as _createTextVNode, createElementVNode as _createElementVNode } from "vue";
129+
_createElementVNode("foo", null, [_createElementVNode("span", null, [_createTextVNode("foo")])]);"
130130
`;
131131
132132
exports[`named import specifier \`Keep Alive\` > named import specifier \`Keep Alive\` 1`] = `
@@ -141,7 +141,7 @@ _createElementVNode(Vue.KeepAlive, null, [_createTextVNode("123")]);"
141141
`;
142142
143143
exports[`override props multiple > multiple 1`] = `
144-
"import { resolveComponent as _resolveComponent, createVNode as _createVNode } from "vue";
144+
"import { resolveComponent as _resolveComponent, normalizeStyle as _normalizeStyle, createVNode as _createVNode } from "vue";
145145
_createVNode(_resolveComponent("A"), {
146146
"loading": true,
147147
...a,
@@ -150,13 +150,13 @@ _createVNode(_resolveComponent("A"), {
150150
d: 2
151151
},
152152
"class": "x",
153-
"style": x
153+
"style": _normalizeStyle(x)
154154
}, null);"
155155
`;
156156
157157
exports[`override props single > single 1`] = `
158-
"import { createVNode as _createVNode } from "vue";
159-
_createVNode("div", a, null);"
158+
"import { createElementVNode as _createElementVNode } from "vue";
159+
_createElementVNode("div", a, null);"
160160
`;
161161
162162
exports[`passing object slots via JSX children directive in slot > directive in slot 1`] = `
@@ -274,8 +274,8 @@ _createElementVNode("div", null, [_createTextVNode("Vue")]);"
274274
`;
275275
276276
exports[`single no need for a mergeProps call > single no need for a mergeProps call 1`] = `
277-
"import { createTextVNode as _createTextVNode, createElementVNode as _createElementVNode } from "vue";
278-
_createElementVNode("div", x, [_createTextVNode("single")], 16);"
277+
"import { createTextVNode as _createTextVNode, normalizeProps as _normalizeProps, guardReactiveProps as _guardReactiveProps, createElementVNode as _createElementVNode } from "vue";
278+
_createElementVNode("div", _normalizeProps(_guardReactiveProps(x)), [_createTextVNode("single")], 16);"
279279
`;
280280
281281
exports[`specifiers should be merged into a single importDeclaration > specifiers should be merged into a single importDeclaration 1`] = `

0 commit comments

Comments
 (0)