Skip to content

Commit 4b0ca87

Browse files
committed
feat(compiler-core): support aliasing vue: prefixed events to inline vnode hooks
1 parent 1c9a481 commit 4b0ca87

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

Diff for: packages/compiler-core/__tests__/transforms/transformElement.spec.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1209,12 +1209,7 @@ describe('compiler: element transform', () => {
12091209

12101210
test('force block for inline before-update handlers w/ children', () => {
12111211
expect(
1212-
parseWithElementTransform(`<div @vnode-before-update>hello</div>`).node
1213-
.isBlock
1214-
).toBe(true)
1215-
1216-
expect(
1217-
parseWithElementTransform(`<div @vnodeBeforeUpdate>hello</div>`).node
1212+
parseWithElementTransform(`<div @vue:before-update>hello</div>`).node
12181213
.isBlock
12191214
).toBe(true)
12201215
})

Diff for: packages/compiler-core/__tests__/transforms/vOn.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,32 @@ describe('compiler: transform v-on', () => {
438438
})
439439
})
440440

441+
test('vue: prefixed events', () => {
442+
const { node } = parseWithVOn(
443+
`<div v-on:vue:mounted="onMount" @vue:before-update="onBeforeUpdate" />`
444+
)
445+
expect((node.codegenNode as VNodeCall).props).toMatchObject({
446+
properties: [
447+
{
448+
key: {
449+
content: `onVnodeMounted`
450+
},
451+
value: {
452+
content: `onMount`
453+
}
454+
},
455+
{
456+
key: {
457+
content: `onVnodeBeforeUpdate`
458+
},
459+
value: {
460+
content: `onBeforeUpdate`
461+
}
462+
}
463+
]
464+
})
465+
})
466+
441467
describe('cacheHandler', () => {
442468
test('empty handler', () => {
443469
const { root, node } = parseWithVOn(`<div v-on:click.prevent />`, {

Diff for: packages/compiler-core/src/transforms/transformElement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ export function buildProps(
550550
(isVBind && isStaticArgOf(arg, 'key')) ||
551551
// inline before-update hooks need to force block so that it is invoked
552552
// before children
553-
(isVOn && hasChildren && isStaticArgOf(arg, 'vnodeBeforeUpdate', true))
553+
(isVOn && hasChildren && isStaticArgOf(arg, 'vue:before-update'))
554554
) {
555555
shouldUseBlock = true
556556
}

Diff for: packages/compiler-core/src/transforms/vOn.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export const transformOn: DirectiveTransform = (
4242
let eventName: ExpressionNode
4343
if (arg.type === NodeTypes.SIMPLE_EXPRESSION) {
4444
if (arg.isStatic) {
45-
const rawName = arg.content
45+
let rawName = arg.content
46+
// TODO deprecate @vnodeXXX usage
47+
if (rawName.startsWith('vue:')) {
48+
rawName = `vnode-${rawName.slice(4)}`
49+
}
4650
// for all event listeners, auto convert it to camelCase. See issue #2249
4751
eventName = createSimpleExpression(
4852
toHandlerKey(camelize(rawName)),

Diff for: packages/compiler-core/src/utils.ts

+3-15
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ import {
4242
WITH_MEMO,
4343
OPEN_BLOCK
4444
} from './runtimeHelpers'
45-
import {
46-
isString,
47-
isObject,
48-
hyphenate,
49-
extend,
50-
NOOP,
51-
camelize
52-
} from '@vue/shared'
45+
import { isString, isObject, hyphenate, extend, NOOP } from '@vue/shared'
5346
import { PropsExpression } from './transforms/transformElement'
5447
import { parseExpression } from '@babel/parser'
5548
import { Expression } from '@babel/types'
@@ -298,14 +291,9 @@ export function findProp(
298291

299292
export function isStaticArgOf(
300293
arg: DirectiveNode['arg'],
301-
name: string,
302-
camel?: boolean
294+
name: string
303295
): boolean {
304-
return !!(
305-
arg &&
306-
isStaticExp(arg) &&
307-
(camel ? camelize(arg.content) : arg.content) === name
308-
)
296+
return !!(arg && isStaticExp(arg) && arg.content === name)
309297
}
310298

311299
export function hasDynamicKeyVBind(node: ElementNode): boolean {

0 commit comments

Comments
 (0)