fix(v-once): fix v-once usage with v-if and v-for

fix #2035
This commit is contained in:
Evan You
2020-09-02 12:29:07 -04:00
parent ad93fa42fc
commit 52e45a9850
4 changed files with 77 additions and 13 deletions

View File

@@ -3,21 +3,17 @@ import {
transform,
NodeTypes,
generate,
CompilerOptions
CompilerOptions,
getBaseTransformPreset
} from '../../src'
import { transformOnce } from '../../src/transforms/vOnce'
import { transformElement } from '../../src/transforms/transformElement'
import { RENDER_SLOT, SET_BLOCK_TRACKING } from '../../src/runtimeHelpers'
import { transformBind } from '../../src/transforms/vBind'
import { transformSlotOutlet } from '../../src/transforms/transformSlotOutlet'
function transformWithOnce(template: string, options: CompilerOptions = {}) {
const ast = parse(template)
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset()
transform(ast, {
nodeTransforms: [transformOnce, transformElement, transformSlotOutlet],
directiveTransforms: {
bind: transformBind
},
nodeTransforms,
directiveTransforms,
...options
})
return ast
@@ -102,4 +98,30 @@ describe('compiler: v-once transform', () => {
})
expect(generate(root).code).toMatchSnapshot()
})
test('with v-if', () => {
const root = transformWithOnce(`<div v-if="true" v-once />`)
expect(root.cached).toBe(1)
expect(root.helpers).toContain(SET_BLOCK_TRACKING)
expect(root.children[0]).toMatchObject({
type: NodeTypes.IF,
// should cache the entire v-if expression, not just a single branch
codegenNode: {
type: NodeTypes.JS_CACHE_EXPRESSION
}
})
})
test('with v-for', () => {
const root = transformWithOnce(`<div v-for="i in list" v-once />`)
expect(root.cached).toBe(1)
expect(root.helpers).toContain(SET_BLOCK_TRACKING)
expect(root.children[0]).toMatchObject({
type: NodeTypes.FOR,
// should cache the entire v-for expression, not just a single branch
codegenNode: {
type: NodeTypes.JS_CACHE_EXPRESSION
}
})
})
})