From 306c22efe1dbc3e98a8fe31277bc5025715b37e1 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 3 Oct 2019 14:08:14 -0400 Subject: [PATCH] feat(compiler): mark compiler-generated slots for runtime --- .../__snapshots__/vSlot.spec.ts.snap | 26 ++++++----- .../__tests__/transforms/vSlot.spec.ts | 43 ++++++++++++------- .../compiler-core/src/transforms/vSlot.ts | 4 +- packages/runtime-core/src/componentSlots.ts | 2 +- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap index 78142174..6d5193d5 100644 --- a/packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap @@ -9,7 +9,8 @@ return function render() { return (openBlock(), createBlock(_component_Comp, null, { [_ctx.one]: ({ foo }) => [toString(foo), toString(_ctx.bar)], - [_ctx.two]: ({ bar }) => [toString(_ctx.foo), toString(bar)] + [_ctx.two]: ({ bar }) => [toString(_ctx.foo), toString(bar)], + _compiled: true }, 256 /* DYNAMIC_SLOTS */)) }" `; @@ -22,7 +23,8 @@ return function render() { const _component_Comp = resolveComponent(\\"Comp\\") return (openBlock(), createBlock(_component_Comp, null, { - default: ({ foo }) => [toString(foo), toString(_ctx.bar)] + default: ({ foo }) => [toString(foo), toString(_ctx.bar)], + _compiled: true })) }" `; @@ -37,7 +39,8 @@ return function render() { return (openBlock(), createBlock(_component_Comp, null, { default: () => [ createVNode(\\"div\\") - ] + ], + _compiled: true })) }" `; @@ -49,7 +52,7 @@ return function render() { const _ctx = this const _component_Comp = resolveComponent(\\"Comp\\") - return (openBlock(), createBlock(_component_Comp, null, createSlots({}, [ + return (openBlock(), createBlock(_component_Comp, null, createSlots({ _compiled: true }, [ renderList(_ctx.list, (name) => { return { name: name, @@ -67,7 +70,7 @@ return function render() { const _ctx = this const _component_Comp = resolveComponent(\\"Comp\\") - return (openBlock(), createBlock(_component_Comp, null, createSlots({}, [ + return (openBlock(), createBlock(_component_Comp, null, createSlots({ _compiled: true }, [ (_ctx.ok) ? { name: \\"one\\", @@ -87,7 +90,7 @@ return function render() { const _component_Comp = _resolveComponent(\\"Comp\\") - return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({}, [ + return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _compiled: true }, [ ok ? { name: \\"one\\", @@ -116,7 +119,7 @@ return function render() { const _component_Comp = _resolveComponent(\\"Comp\\") - return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({}, [ + return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _compiled: true }, [ ok ? { name: \\"one\\", @@ -137,7 +140,8 @@ return function render() { return (openBlock(), createBlock(_component_Comp, null, { one: ({ foo }) => [toString(foo), toString(_ctx.bar)], - two: ({ bar }) => [toString(_ctx.foo), toString(bar)] + two: ({ bar }) => [toString(_ctx.foo), toString(bar)], + _compiled: true })) }" `; @@ -153,12 +157,14 @@ return function render() { return (openBlock(), createBlock(_component_Comp, null, { default: ({ foo }) => [ createVNode(_component_Inner, null, { - default: ({ bar }) => [toString(foo), toString(bar), toString(_ctx.baz)] + default: ({ bar }) => [toString(foo), toString(bar), toString(_ctx.baz)], + _compiled: true }), toString(foo), toString(_ctx.bar), toString(_ctx.baz) - ] + ], + _compiled: true })) }" `; diff --git a/packages/compiler-core/__tests__/transforms/vSlot.spec.ts b/packages/compiler-core/__tests__/transforms/vSlot.spec.ts index a36e5388..5dc22d4c 100644 --- a/packages/compiler-core/__tests__/transforms/vSlot.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vSlot.spec.ts @@ -43,17 +43,22 @@ function parseWithSlots(template: string, options: CompilerOptions = {}) { function createSlotMatcher(obj: Record) { return { type: NodeTypes.JS_OBJECT_EXPRESSION, - properties: Object.keys(obj).map(key => { - return { - type: NodeTypes.JS_PROPERTY, - key: { - type: NodeTypes.SIMPLE_EXPRESSION, - isStatic: !/^\[/.test(key), - content: key.replace(/^\[|\]$/g, '') - }, - value: obj[key] - } - }) + properties: Object.keys(obj) + .map(key => { + return { + type: NodeTypes.JS_PROPERTY, + key: { + type: NodeTypes.SIMPLE_EXPRESSION, + isStatic: !/^\[/.test(key), + content: key.replace(/^\[|\]$/g, '') + }, + value: obj[key] + } as any + }) + .concat({ + key: { content: `_compiled` }, + value: { content: `true` } + }) } } @@ -330,7 +335,9 @@ describe('compiler: transform component slots', () => { type: NodeTypes.JS_CALL_EXPRESSION, callee: `_${CREATE_SLOTS}`, arguments: [ - createObjectMatcher({}), + createObjectMatcher({ + _compiled: `[true]` + }), { type: NodeTypes.JS_ARRAY_EXPRESSION, elements: [ @@ -370,7 +377,9 @@ describe('compiler: transform component slots', () => { type: NodeTypes.JS_CALL_EXPRESSION, callee: CREATE_SLOTS, arguments: [ - createObjectMatcher({}), + createObjectMatcher({ + _compiled: `[true]` + }), { type: NodeTypes.JS_ARRAY_EXPRESSION, elements: [ @@ -417,7 +426,9 @@ describe('compiler: transform component slots', () => { type: NodeTypes.JS_CALL_EXPRESSION, callee: `_${CREATE_SLOTS}`, arguments: [ - createObjectMatcher({}), + createObjectMatcher({ + _compiled: `[true]` + }), { type: NodeTypes.JS_ARRAY_EXPRESSION, elements: [ @@ -474,7 +485,9 @@ describe('compiler: transform component slots', () => { type: NodeTypes.JS_CALL_EXPRESSION, callee: CREATE_SLOTS, arguments: [ - createObjectMatcher({}), + createObjectMatcher({ + _compiled: `[true]` + }), { type: NodeTypes.JS_ARRAY_EXPRESSION, elements: [ diff --git a/packages/compiler-core/src/transforms/vSlot.ts b/packages/compiler-core/src/transforms/vSlot.ts index 29286042..f68d08df 100644 --- a/packages/compiler-core/src/transforms/vSlot.ts +++ b/packages/compiler-core/src/transforms/vSlot.ts @@ -264,7 +264,9 @@ export function buildSlots( } let slots: ObjectExpression | CallExpression = createObjectExpression( - slotsProperties, + slotsProperties.concat( + createObjectProperty(`_compiled`, createSimpleExpression(`true`, false)) + ), loc ) if (dynamicSlots.length) { diff --git a/packages/runtime-core/src/componentSlots.ts b/packages/runtime-core/src/componentSlots.ts index 67858ee3..534caed5 100644 --- a/packages/runtime-core/src/componentSlots.ts +++ b/packages/runtime-core/src/componentSlots.ts @@ -42,7 +42,7 @@ export function resolveSlots( ) { let slots: Slots | void if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) { - if ((children as any)._normalized) { + if ((children as any)._compiled) { // pre-normalized slots object generated by compiler slots = children as Slots } else {