Skip to content

Commit 98bc9a2

Browse files
committed
wip: more compat tweaks
1 parent 12abd4a commit 98bc9a2

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

packages/runtime-core/src/apiWatch.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { queuePostRenderEffect } from './renderer'
3636
import { warn } from './warning'
3737
import { DeprecationTypes } from './compat/compatConfig'
3838
import { checkCompatEnabled, isCompatEnabled } from './compat/compatConfig'
39+
import { ObjectWatchOptionItem } from './componentOptions'
3940

4041
export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void
4142

@@ -354,7 +355,7 @@ function doWatch(
354355
export function instanceWatch(
355356
this: ComponentInternalInstance,
356357
source: string | Function,
357-
cb: WatchCallback,
358+
value: WatchCallback | ObjectWatchOptionItem,
358359
options?: WatchOptions
359360
): WatchStopHandle {
360361
const publicThis = this.proxy as any
@@ -363,6 +364,13 @@ export function instanceWatch(
363364
? createPathGetter(publicThis, source)
364365
: () => publicThis[source]
365366
: source.bind(publicThis)
367+
let cb
368+
if (isFunction(value)) {
369+
cb = value
370+
} else {
371+
cb = value.handler as Function
372+
options = value
373+
}
366374
return doWatch(getter, cb.bind(publicThis), options, this)
367375
}
368376

packages/runtime-core/src/compat/global.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export function createCompatVue(
109109
} as any
110110

111111
const singletonApp = createApp({})
112+
// @ts-ignore
113+
singletonApp.prototype = singletonApp.config.globalProperties
112114

113115
function createCompatApp(options: ComponentOptions = {}, Ctor: any) {
114116
assertCompatEnabled(DeprecationTypes.GLOBAL_MOUNT, null)
@@ -145,7 +147,10 @@ export function createCompatVue(
145147

146148
// copy prototype augmentations as config.globalProperties
147149
if (isCompatEnabled(DeprecationTypes.GLOBAL_PROTOTYPE, null)) {
148-
app.config.globalProperties = Ctor.prototype
150+
app.config.globalProperties = extend(
151+
Object.create(Ctor.prototype),
152+
singletonApp.config.globalProperties
153+
)
149154
}
150155
let hasPrototypeAugmentations = false
151156
for (const key in Ctor.prototype) {

packages/runtime-core/src/compat/instance.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { extend, NOOP, toDisplayString, toNumber } from '@vue/shared'
1+
import {
2+
extend,
3+
looseEqual,
4+
looseIndexOf,
5+
NOOP,
6+
toDisplayString,
7+
toNumber
8+
} from '@vue/shared'
29
import { PublicPropertiesMap } from '../componentPublicInstance'
310
import { getCompatChildren } from './instanceChildren'
411
import {
@@ -14,13 +21,17 @@ import { compatH } from './renderFn'
1421
import { createCommentVNode, createTextVNode } from '../vnode'
1522
import { renderList } from '../helpers/renderList'
1623
import {
24+
legacyBindDynamicKeys,
1725
legacyBindObjectListeners,
1826
legacyBindObjectProps,
1927
legacyCheckKeyCodes,
28+
legacyMarkOnce,
29+
legacyPrependModifier,
2030
legacyRenderSlot,
2131
legacyRenderStatic,
2232
legacyresolveScopedSlots
2333
} from './renderHelpers'
34+
import { resolveFilter } from '../helpers/resolveAssets'
2435

2536
export function installCompatInstanceProperties(map: PublicPropertiesMap) {
2637
const set = (target: any, key: any, val: any) => {
@@ -85,16 +96,22 @@ export function installCompatInstanceProperties(map: PublicPropertiesMap) {
8596
$createElement: () => compatH,
8697
_self: i => i.proxy,
8798
_c: () => compatH,
99+
_o: () => legacyMarkOnce,
88100
_n: () => toNumber,
89101
_s: () => toDisplayString,
90102
_l: () => renderList,
91103
_t: i => legacyRenderSlot.bind(null, i),
104+
_q: () => looseEqual,
105+
_i: () => looseIndexOf,
106+
_m: i => legacyRenderStatic.bind(null, i),
107+
_f: () => resolveFilter,
108+
_k: i => legacyCheckKeyCodes.bind(null, i),
92109
_b: () => legacyBindObjectProps,
93-
_e: () => createCommentVNode,
94110
_v: () => createTextVNode,
95-
_m: i => legacyRenderStatic.bind(null, i),
96-
_g: () => legacyBindObjectListeners,
111+
_e: () => createCommentVNode,
97112
_u: () => legacyresolveScopedSlots,
98-
_k: i => legacyCheckKeyCodes.bind(null, i)
113+
_g: () => legacyBindObjectListeners,
114+
_d: () => legacyBindDynamicKeys,
115+
_p: () => legacyPrependModifier
99116
} as PublicPropertiesMap)
100117
}

packages/runtime-core/src/compat/renderHelpers.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,21 @@ function isKeyNotMatch<T>(expect: T | T[], actual: T): boolean {
162162
return expect !== actual
163163
}
164164
}
165+
166+
export function legacyMarkOnce(tree: VNode) {
167+
return tree
168+
}
169+
170+
export function legacyBindDynamicKeys(props: any, values: any[]) {
171+
for (let i = 0; i < values.length; i += 2) {
172+
const key = values[i]
173+
if (typeof key === 'string' && key) {
174+
props[values[i]] = values[i + 1]
175+
}
176+
}
177+
return props
178+
}
179+
180+
export function legacyPrependModifier(value: any, symbol: string) {
181+
return typeof value === 'string' ? symbol + value : value
182+
}

packages/runtime-core/src/componentOptions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,11 @@ export type ExtractComputedReturns<T extends any> = {
360360
: T[key] extends (...args: any[]) => infer TReturn ? TReturn : never
361361
}
362362

363-
type WatchOptionItem =
364-
| string
365-
| WatchCallback
366-
| { handler: WatchCallback | string } & WatchOptions
363+
export type ObjectWatchOptionItem = {
364+
handler: WatchCallback | string
365+
} & WatchOptions
366+
367+
type WatchOptionItem = string | WatchCallback | ObjectWatchOptionItem
367368

368369
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
369370

@@ -949,7 +950,7 @@ function resolveData(
949950
}
950951
}
951952

952-
function createWatcher(
953+
export function createWatcher(
953954
raw: ComponentWatchOptionItem,
954955
ctx: Data,
955956
publicThis: ComponentPublicInstance,

0 commit comments

Comments
 (0)