feat(compiler-core): wrap slot functions with render context
This commit is contained in:
@@ -19,7 +19,7 @@ export const render = _withId(function render(_ctx, _cache) {
|
||||
`;
|
||||
|
||||
exports[`scopeId compiler support should wrap default slot 1`] = `
|
||||
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
|
||||
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
|
||||
const _withId = _withScopeId(\\"test\\")
|
||||
|
||||
export const render = _withId(function render(_ctx, _cache) {
|
||||
@@ -28,14 +28,14 @@ export const render = _withId(function render(_ctx, _cache) {
|
||||
return (_openBlock(), _createBlock(_component_Child, null, {
|
||||
default: _withId(() => [
|
||||
_createVNode(\\"div\\")
|
||||
]),
|
||||
], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
})"
|
||||
`;
|
||||
|
||||
exports[`scopeId compiler support should wrap dynamic slots 1`] = `
|
||||
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, renderList as _renderList, createSlots as _createSlots, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
|
||||
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, renderList as _renderList, createSlots as _createSlots, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
|
||||
const _withId = _withScopeId(\\"test\\")
|
||||
|
||||
export const render = _withId(function render(_ctx, _cache) {
|
||||
@@ -47,7 +47,7 @@ export const render = _withId(function render(_ctx, _cache) {
|
||||
name: \\"foo\\",
|
||||
fn: _withId(() => [
|
||||
_createVNode(\\"div\\")
|
||||
])
|
||||
], _ctx)
|
||||
}
|
||||
: undefined,
|
||||
_renderList(_ctx.list, (i) => {
|
||||
@@ -55,7 +55,7 @@ export const render = _withId(function render(_ctx, _cache) {
|
||||
name: i,
|
||||
fn: _withId(() => [
|
||||
_createVNode(\\"div\\")
|
||||
])
|
||||
], _ctx)
|
||||
}
|
||||
})
|
||||
]), 1024 /* DYNAMIC_SLOTS */))
|
||||
@@ -63,7 +63,7 @@ export const render = _withId(function render(_ctx, _cache) {
|
||||
`;
|
||||
|
||||
exports[`scopeId compiler support should wrap named slots 1`] = `
|
||||
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createVNode as _createVNode, resolveComponent as _resolveComponent, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
|
||||
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createVNode as _createVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock, withScopeId as _withScopeId } from \\"vue\\"
|
||||
const _withId = _withScopeId(\\"test\\")
|
||||
|
||||
export const render = _withId(function render(_ctx, _cache) {
|
||||
@@ -72,10 +72,10 @@ export const render = _withId(function render(_ctx, _cache) {
|
||||
return (_openBlock(), _createBlock(_component_Child, null, {
|
||||
foo: _withId(({ msg }) => [
|
||||
_createTextVNode(_toDisplayString(msg), 1 /* TEXT */)
|
||||
]),
|
||||
], _ctx),
|
||||
bar: _withId(() => [
|
||||
_createVNode(\\"div\\")
|
||||
]),
|
||||
], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
})"
|
||||
|
||||
@@ -239,14 +239,14 @@ exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist expr
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
with (_ctx) {
|
||||
const { toDisplayString: _toDisplayString, createTextVNode: _createTextVNode, resolveComponent: _resolveComponent, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
const { toDisplayString: _toDisplayString, createTextVNode: _createTextVNode, resolveComponent: _resolveComponent, withCtx: _withCtx, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
default: ({ foo }) => [
|
||||
default: _withCtx(({ foo }) => [
|
||||
_createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */)
|
||||
],
|
||||
], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`compiler: transform component slots dynamically named slots 1`] = `
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, withCtx: _withCtx, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
[_ctx.one]: ({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)],
|
||||
[_ctx.two]: ({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)],
|
||||
[_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
|
||||
[_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
|
||||
_: 1
|
||||
}, 1024 /* DYNAMIC_SLOTS */))
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler: transform component slots implicit default slot 1`] = `
|
||||
"const { createVNode: _createVNode, resolveComponent: _resolveComponent, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { createVNode: _createVNode, resolveComponent: _resolveComponent, withCtx: _withCtx, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
default: () => [
|
||||
default: _withCtx(() => [
|
||||
_createVNode(\\"div\\")
|
||||
],
|
||||
], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler: transform component slots named slot with v-for w/ prefixIdentifiers: true 1`] = `
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, renderList: _renderList, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, withCtx: _withCtx, renderList: _renderList, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
@@ -39,7 +39,7 @@ return function render(_ctx, _cache) {
|
||||
_renderList(_ctx.list, (name) => {
|
||||
return {
|
||||
name: name,
|
||||
fn: () => [_toDisplayString(name)]
|
||||
fn: _withCtx(() => [_toDisplayString(name)], _ctx)
|
||||
}
|
||||
})
|
||||
]), 1024 /* DYNAMIC_SLOTS */))
|
||||
@@ -47,7 +47,7 @@ return function render(_ctx, _cache) {
|
||||
`;
|
||||
|
||||
exports[`compiler: transform component slots named slot with v-if + prefixIdentifiers: true 1`] = `
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, withCtx: _withCtx, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
@@ -56,7 +56,7 @@ return function render(_ctx, _cache) {
|
||||
(_ctx.ok)
|
||||
? {
|
||||
name: \\"one\\",
|
||||
fn: (props) => [_toDisplayString(props)]
|
||||
fn: _withCtx((props) => [_toDisplayString(props)], _ctx)
|
||||
}
|
||||
: undefined
|
||||
]), 1024 /* DYNAMIC_SLOTS */))
|
||||
@@ -68,7 +68,7 @@ exports[`compiler: transform component slots named slot with v-if + v-else-if +
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
with (_ctx) {
|
||||
const { resolveComponent: _resolveComponent, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
const { resolveComponent: _resolveComponent, withCtx: _withCtx, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
@@ -76,16 +76,16 @@ return function render(_ctx, _cache) {
|
||||
ok
|
||||
? {
|
||||
name: \\"one\\",
|
||||
fn: () => [\\"foo\\"]
|
||||
fn: _withCtx(() => [\\"foo\\"], _ctx)
|
||||
}
|
||||
: orNot
|
||||
? {
|
||||
name: \\"two\\",
|
||||
fn: (props) => [\\"bar\\"]
|
||||
fn: _withCtx((props) => [\\"bar\\"], _ctx)
|
||||
}
|
||||
: {
|
||||
name: \\"one\\",
|
||||
fn: () => [\\"baz\\"]
|
||||
fn: _withCtx(() => [\\"baz\\"], _ctx)
|
||||
}
|
||||
]), 1024 /* DYNAMIC_SLOTS */))
|
||||
}
|
||||
@@ -97,7 +97,7 @@ exports[`compiler: transform component slots named slot with v-if 1`] = `
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
with (_ctx) {
|
||||
const { resolveComponent: _resolveComponent, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
const { resolveComponent: _resolveComponent, withCtx: _withCtx, createSlots: _createSlots, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
@@ -105,7 +105,7 @@ return function render(_ctx, _cache) {
|
||||
ok
|
||||
? {
|
||||
name: \\"one\\",
|
||||
fn: () => [\\"hello\\"]
|
||||
fn: _withCtx(() => [\\"hello\\"], _ctx)
|
||||
}
|
||||
: undefined
|
||||
]), 1024 /* DYNAMIC_SLOTS */))
|
||||
@@ -114,14 +114,14 @@ return function render(_ctx, _cache) {
|
||||
`;
|
||||
|
||||
exports[`compiler: transform component slots named slots 1`] = `
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, withCtx: _withCtx, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
one: ({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)],
|
||||
two: ({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)],
|
||||
one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
|
||||
two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
}"
|
||||
@@ -132,16 +132,16 @@ exports[`compiler: transform component slots named slots w/ implicit default slo
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
with (_ctx) {
|
||||
const { createVNode: _createVNode, resolveComponent: _resolveComponent, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
const { createVNode: _createVNode, resolveComponent: _resolveComponent, withCtx: _withCtx, openBlock: _openBlock, createBlock: _createBlock } = _Vue
|
||||
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
one: () => [\\"foo\\"],
|
||||
default: () => [
|
||||
one: _withCtx(() => [\\"foo\\"], _ctx),
|
||||
default: _withCtx(() => [
|
||||
\\"bar\\",
|
||||
_createVNode(\\"span\\")
|
||||
],
|
||||
], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
}
|
||||
@@ -149,36 +149,36 @@ return function render(_ctx, _cache) {
|
||||
`;
|
||||
|
||||
exports[`compiler: transform component slots nested slots scoping 1`] = `
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, withCtx: _withCtx, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Inner = _resolveComponent(\\"Inner\\")
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
default: ({ foo }) => [
|
||||
default: _withCtx(({ foo }) => [
|
||||
_createVNode(_component_Inner, null, {
|
||||
default: ({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)],
|
||||
default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)], _ctx),
|
||||
_: 1
|
||||
}, 1024 /* DYNAMIC_SLOTS */),
|
||||
\\" \\",
|
||||
_toDisplayString(foo),
|
||||
_toDisplayString(_ctx.bar),
|
||||
_toDisplayString(_ctx.baz)
|
||||
],
|
||||
], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler: transform component slots on-component default slot 1`] = `
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
"const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, withCtx: _withCtx, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
||||
|
||||
return function render(_ctx, _cache) {
|
||||
const _component_Comp = _resolveComponent(\\"Comp\\")
|
||||
|
||||
return (_openBlock(), _createBlock(_component_Comp, null, {
|
||||
default: ({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)],
|
||||
default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
|
||||
_: 1
|
||||
}))
|
||||
}"
|
||||
|
||||
@@ -768,6 +768,8 @@ function genFunctionExpression(
|
||||
|
||||
if (genScopeId) {
|
||||
push(`_withId(`)
|
||||
} else if (isSlot) {
|
||||
push(`_withCtx(`)
|
||||
}
|
||||
push(`(`, node)
|
||||
if (isArray(params)) {
|
||||
@@ -796,8 +798,8 @@ function genFunctionExpression(
|
||||
deindent()
|
||||
push(`}`)
|
||||
}
|
||||
if (genScopeId) {
|
||||
push(`)`)
|
||||
if (genScopeId || isSlot) {
|
||||
push(`, _ctx)`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ export const SET_BLOCK_TRACKING = Symbol(__DEV__ ? `setBlockTracking` : ``)
|
||||
export const PUSH_SCOPE_ID = Symbol(__DEV__ ? `pushScopeId` : ``)
|
||||
export const POP_SCOPE_ID = Symbol(__DEV__ ? `popScopeId` : ``)
|
||||
export const WITH_SCOPE_ID = Symbol(__DEV__ ? `withScopeId` : ``)
|
||||
export const WITH_CTX = Symbol(__DEV__ ? `withCtx` : ``)
|
||||
|
||||
// Name mapping for runtime helpers that need to be imported from 'vue' in
|
||||
// generated code. Make sure these are correctly exported in the runtime!
|
||||
@@ -56,7 +57,8 @@ export const helperNameMap: any = {
|
||||
[SET_BLOCK_TRACKING]: `setBlockTracking`,
|
||||
[PUSH_SCOPE_ID]: `pushScopeId`,
|
||||
[POP_SCOPE_ID]: `popScopeId`,
|
||||
[WITH_SCOPE_ID]: `withScopeId`
|
||||
[WITH_SCOPE_ID]: `withScopeId`,
|
||||
[WITH_CTX]: `withCtx`
|
||||
}
|
||||
|
||||
export function registerRuntimeHelpers(helpers: any) {
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
import { TransformContext, NodeTransform } from '../transform'
|
||||
import { createCompilerError, ErrorCodes } from '../errors'
|
||||
import { findDir, isTemplateNode, assert, isVSlot, hasScopeRef } from '../utils'
|
||||
import { CREATE_SLOTS, RENDER_LIST } from '../runtimeHelpers'
|
||||
import { CREATE_SLOTS, RENDER_LIST, WITH_CTX } from '../runtimeHelpers'
|
||||
import { parseForExpression, createForLoopParams } from './vFor'
|
||||
|
||||
const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
|
||||
@@ -119,6 +119,8 @@ export function buildSlots(
|
||||
slots: SlotsExpression
|
||||
hasDynamicSlots: boolean
|
||||
} {
|
||||
context.helper(WITH_CTX)
|
||||
|
||||
const { children, loc } = node
|
||||
const slotsProperties: Property[] = []
|
||||
const dynamicSlots: (ConditionalExpression | CallExpression)[] = []
|
||||
|
||||
Reference in New Issue
Block a user