Skip to content

Commit

Permalink
fix(slots): differentiate dynamic/static compiled slots
Browse files Browse the repository at this point in the history
fix #1557
  • Loading branch information
yyx990803 committed Jul 13, 2020
1 parent ba3b3cd commit 65beba9
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const _withId = /*#__PURE__*/_withScopeId(\\"test\\")
export const render = /*#__PURE__*/_withId(function render(_ctx, _cache) {
const _component_Child = _resolveComponent(\\"Child\\")
return (_openBlock(), _createBlock(_component_Child, null, _createSlots({ _: 1 }, [
return (_openBlock(), _createBlock(_component_Child, null, _createSlots({ _: 2 }, [
(_ctx.ok)
? {
name: \\"foo\\",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ return function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Comp, null, {
[_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
[_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
_: 1
_: 2
}, 1024 /* DYNAMIC_SLOTS */))
}"
`;
Expand All @@ -35,7 +35,7 @@ exports[`compiler: transform component slots named slot with v-for w/ prefixIden
return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 1 }, [
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 2 }, [
_renderList(_ctx.list, (name) => {
return {
name: name,
Expand All @@ -52,7 +52,7 @@ exports[`compiler: transform component slots named slot with v-if + prefixIdenti
return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 1 }, [
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 2 }, [
(_ctx.ok)
? {
name: \\"one\\",
Expand All @@ -72,7 +72,7 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 1 }, [
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 2 }, [
ok
? {
name: \\"one\\",
Expand Down Expand Up @@ -101,7 +101,7 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 1 }, [
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 2 }, [
ok
? {
name: \\"one\\",
Expand Down Expand Up @@ -145,7 +145,7 @@ return function render(_ctx, _cache) {
default: _withCtx(({ foo }) => [
_createVNode(_component_Inner, null, {
default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)]),
_: 1
_: 2
}, 1024 /* DYNAMIC_SLOTS */),
\\" \\",
_toDisplayString(foo),
Expand All @@ -165,8 +165,8 @@ return function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Comp, null, {
[_ctx.named]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
_: 1
}))
_: 2
}, 1024 /* DYNAMIC_SLOTS */))
}"
`;

Expand Down
195 changes: 102 additions & 93 deletions packages/compiler-core/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function parseWithSlots(template: string, options: CompilerOptions = {}) {
}
}

function createSlotMatcher(obj: Record<string, any>) {
function createSlotMatcher(obj: Record<string, any>, isDynamic = false) {
return {
type: NodeTypes.JS_OBJECT_EXPRESSION,
properties: Object.keys(obj)
Expand All @@ -70,7 +70,7 @@ function createSlotMatcher(obj: Record<string, any>) {
})
.concat({
key: { content: `_` },
value: { content: `1`, isStatic: false }
value: { content: isDynamic ? `2` : `1`, isStatic: false }
})
}
}
Expand Down Expand Up @@ -230,29 +230,32 @@ describe('compiler: transform component slots', () => {
{ prefixIdentifiers: true }
)
expect(slots).toMatchObject(
createSlotMatcher({
'[_ctx.named]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `foo` }, ` }`]
},
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `foo`
}
createSlotMatcher(
{
'[_ctx.named]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `foo` }, ` }`]
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.bar`
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `foo`
}
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.bar`
}
}
}
]
}
})
]
}
},
true
)
)
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
})
Expand Down Expand Up @@ -307,50 +310,53 @@ describe('compiler: transform component slots', () => {
{ prefixIdentifiers: true }
)
expect(slots).toMatchObject(
createSlotMatcher({
'[_ctx.one]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `foo` }, ` }`]
},
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `foo`
}
createSlotMatcher(
{
'[_ctx.one]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `foo` }, ` }`]
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.bar`
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `foo`
}
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.bar`
}
}
}
]
},
'[_ctx.two]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `bar` }, ` }`]
]
},
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.foo`
}
'[_ctx.two]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `bar` }, ` }`]
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `bar`
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.foo`
}
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `bar`
}
}
}
]
}
})
]
}
},
true
)
)
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
})
Expand Down Expand Up @@ -382,35 +388,38 @@ describe('compiler: transform component slots', () => {
type: NodeTypes.VNODE_CALL,
tag: `_component_Inner`,
props: undefined,
children: createSlotMatcher({
default: {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `bar` }, ` }`]
},
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `foo`
}
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `bar`
}
children: createSlotMatcher(
{
default: {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [`{ `, { content: `bar` }, ` }`]
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.baz`
returns: [
{
type: NodeTypes.INTERPOLATION,
content: {
content: `foo`
}
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `bar`
}
},
{
type: NodeTypes.INTERPOLATION,
content: {
content: `_ctx.baz`
}
}
}
]
}
}),
]
}
},
true
),
// nested slot should be forced dynamic, since scope variables
// are not tracked as dependencies of the slot.
patchFlag: genFlagText(PatchFlags.DYNAMIC_SLOTS)
Expand Down Expand Up @@ -522,7 +531,7 @@ describe('compiler: transform component slots', () => {
callee: CREATE_SLOTS,
arguments: [
createObjectMatcher({
_: `[1]`
_: `[2]`
}),
{
type: NodeTypes.JS_ARRAY_EXPRESSION,
Expand Down Expand Up @@ -564,7 +573,7 @@ describe('compiler: transform component slots', () => {
callee: CREATE_SLOTS,
arguments: [
createObjectMatcher({
_: `[1]`
_: `[2]`
}),
{
type: NodeTypes.JS_ARRAY_EXPRESSION,
Expand Down Expand Up @@ -613,7 +622,7 @@ describe('compiler: transform component slots', () => {
callee: CREATE_SLOTS,
arguments: [
createObjectMatcher({
_: `[1]`
_: `[2]`
}),
{
type: NodeTypes.JS_ARRAY_EXPRESSION,
Expand Down Expand Up @@ -672,7 +681,7 @@ describe('compiler: transform component slots', () => {
callee: CREATE_SLOTS,
arguments: [
createObjectMatcher({
_: `[1]`
_: `[2]`
}),
{
type: NodeTypes.JS_ARRAY_EXPRESSION,
Expand Down
12 changes: 10 additions & 2 deletions packages/compiler-core/src/transforms/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function buildSlots(
let hasDynamicSlots = context.scopes.vSlot > 0 || context.scopes.vFor > 0
// with `prefixIdentifiers: true`, this can be further optimized to make
// it dynamic only when the slot actually uses the scope variables.
if (!__BROWSER__ && context.prefixIdentifiers) {
if (!__BROWSER__ && !context.ssr && context.prefixIdentifiers) {
hasDynamicSlots = hasScopeRef(node, context.identifiers)
}

Expand All @@ -144,6 +144,9 @@ export function buildSlots(
const onComponentSlot = findDir(node, 'slot', true)
if (onComponentSlot) {
const { arg, exp } = onComponentSlot
if (arg && !isStaticExp(arg)) {
hasDynamicSlots = true
}
slotsProperties.push(
createObjectProperty(
arg || createSimpleExpression('default', true),
Expand Down Expand Up @@ -317,7 +320,12 @@ export function buildSlots(

let slots = createObjectExpression(
slotsProperties.concat(
createObjectProperty(`_`, createSimpleExpression(`1`, false))
createObjectProperty(
`_`,
// 2 = compiled but dynamic = can skip normalization, but must run diff
// 1 = compiled and static = can skip normalization AND diff as optimized
createSimpleExpression(hasDynamicSlots ? `2` : `1`, false)
)
),
loc
) as SlotsExpression
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-ssr/__tests__/ssrComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('ssr: components', () => {
return function ssrRender(_ctx, _push, _parent, _attrs) {
const _component_foo = _resolveComponent(\\"foo\\")
_push(_ssrRenderComponent(_component_foo, _attrs, _createSlots({ _: 1 }, [
_push(_ssrRenderComponent(_component_foo, _attrs, _createSlots({ _: 2 }, [
(_ctx.ok)
? {
name: \\"named\\",
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('ssr: components', () => {
return function ssrRender(_ctx, _push, _parent, _attrs) {
const _component_foo = _resolveComponent(\\"foo\\")
_push(_ssrRenderComponent(_component_foo, _attrs, _createSlots({ _: 1 }, [
_push(_ssrRenderComponent(_component_foo, _attrs, _createSlots({ _: 2 }, [
_renderList(_ctx.names, (key) => {
return {
name: key,
Expand Down
Loading

0 comments on commit 65beba9

Please sign in to comment.