Skip to content
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

fix: deoptimize slots when a directive is used #638

Open
wants to merge 2 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
21 changes: 16 additions & 5 deletions packages/babel-plugin-jsx/src/transform-vue-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,18 @@ const transformJSXElement = (

const { optimize = false } = state.opts;

const slotFlag = path.getData('slotFlag') || SlotFlags.STABLE;
// #541 - directives can't be resolved in optimized slots
// all parents should be deoptimized
if (directives.length) {
let currentPath = path;
while (currentPath.parentPath?.isJSXElement()) {
currentPath = currentPath.parentPath;
currentPath.setData('slotFlag', 0);
}
}

const slotFlag = path.getData('slotFlag') ?? SlotFlags.STABLE;
const optimizeSlots = optimize && slotFlag !== 0;
let VNodeChild;

if (children.length > 1 || slots) {
Expand All @@ -390,7 +401,7 @@ const transformJSXElement = (
? (slots! as t.ObjectExpression).properties
: [t.spreadElement(slots!)]
) : []),
optimize && t.objectProperty(
optimizeSlots && t.objectProperty(
t.identifier('_'),
t.numericLiteral(slotFlag),
),
Expand All @@ -408,7 +419,7 @@ const transformJSXElement = (
t.identifier('default'),
t.arrowFunctionExpression([], t.arrayExpression(buildIIFE(path, [child]))),
),
optimize && t.objectProperty(
optimizeSlots && t.objectProperty(
t.identifier('_'),
t.numericLiteral(slotFlag),
) as any,
Expand All @@ -435,7 +446,7 @@ const transformJSXElement = (
t.objectProperty(
t.identifier('default'),
t.arrowFunctionExpression([], t.arrayExpression(buildIIFE(path, [slotId]))),
), optimize && t.objectProperty(
), optimizeSlots && t.objectProperty(
t.identifier('_'),
t.numericLiteral(slotFlag),
) as any,
Expand Down Expand Up @@ -463,7 +474,7 @@ const transformJSXElement = (
} else if (t.isObjectExpression(child)) {
VNodeChild = t.objectExpression([
...child.properties,
optimize && t.objectProperty(
optimizeSlots && t.objectProperty(
t.identifier('_'),
t.numericLiteral(slotFlag),
),
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-plugin-jsx/test/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ const transpile = (
</>
`,
},
{
name: 'directive in slot',
from: `
<>
<A>
<B><div /></B>
</A>
<A><div v-xxx /></A>
<A>
<B><div v-xxx /></B>
</A>
</>
`,
},
{
name: 'vModels',
from: '<C v-models={[[foo, ["modifier"]], [bar, "bar", ["modifier1", "modifier2"]]]} />',
Expand Down