wip: more compat tweaks

This commit is contained in:
Evan You 2021-04-22 17:30:54 -04:00
parent 7e0224aa8c
commit 6f8fe4eac9
6 changed files with 29 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import { DeprecationTypes, isCompatEnabled } from './compatConfig'
export function shouldSkipAttr( export function shouldSkipAttr(
key: string, key: string,
value: any,
instance: ComponentInternalInstance instance: ComponentInternalInstance
): boolean { ): boolean {
if ( if (
@ -18,5 +19,9 @@ export function shouldSkipAttr(
) { ) {
return true return true
} }
// vue-router
if (key.startsWith('routerView') || key === 'registerRouteInstance') {
return true
}
return false return false
} }

View File

@ -153,10 +153,7 @@ export function createCompatVue(
// copy prototype augmentations as config.globalProperties // copy prototype augmentations as config.globalProperties
if (isCompatEnabled(DeprecationTypes.GLOBAL_PROTOTYPE, null)) { if (isCompatEnabled(DeprecationTypes.GLOBAL_PROTOTYPE, null)) {
app.config.globalProperties = extend( app.config.globalProperties = Ctor.prototype
Object.create(Ctor.prototype),
singletonApp.config.globalProperties
)
} }
let hasPrototypeAugmentations = false let hasPrototypeAugmentations = false
for (const key in Ctor.prototype) { for (const key in Ctor.prototype) {
@ -449,7 +446,9 @@ function defineReactive(obj: any, key: string, val: any) {
}) })
} else { } else {
Object.keys(val).forEach(key => { Object.keys(val).forEach(key => {
defineReactiveSimple(val, key, val[key]) try {
defineReactiveSimple(val, key, val[key])
} catch (e) {}
}) })
} }
} }

View File

@ -93,11 +93,14 @@ export function installCompatInstanceProperties(map: PublicPropertiesMap) {
$children: getCompatChildren, $children: getCompatChildren,
$listeners: getCompatListeners, $listeners: getCompatListeners,
// inject parent into $options for compat $vnode: i => i.vnode,
// inject addtional properties into $options for compat
$options: i => { $options: i => {
let res = resolveMergedOptions(i) let res = resolveMergedOptions(i)
if (res === i.type) res = i.type.__merged = extend({}, res) if (res === i.type) res = i.type.__merged = extend({}, res)
res.parent = i.proxy!.$parent res.parent = i.proxy!.$parent
res.propsData = i.vnode.props
return res return res
}, },

View File

@ -24,6 +24,7 @@ import {
resolveDynamicComponent resolveDynamicComponent
} from '../helpers/resolveAssets' } from '../helpers/resolveAssets'
import { import {
Comment,
createVNode, createVNode,
isVNode, isVNode,
normalizeChildren, normalizeChildren,
@ -121,6 +122,10 @@ export function compatH(
propsOrChildren?: any, propsOrChildren?: any,
children?: any children?: any
): VNode { ): VNode {
if (!type) {
type = Comment
}
// to support v2 string component name look!up // to support v2 string component name look!up
if (typeof type === 'string') { if (typeof type === 'string') {
const t = hyphenate(type) const t = hyphenate(type)
@ -201,6 +206,8 @@ function convertLegacyProps(
} }
} }
} }
} else if (key === 'hook') {
// TODO
} else if (!skipLegacyRootLevelProps(key)) { } else if (!skipLegacyRootLevelProps(key)) {
converted[key] = legacyProps[key as keyof LegacyVNodeProps] converted[key] = legacyProps[key as keyof LegacyVNodeProps]
} }

View File

@ -229,7 +229,7 @@ export function updateProps(
) )
} }
} else { } else {
if (__COMPAT__ && shouldSkipAttr(key, instance)) { if (__COMPAT__ && shouldSkipAttr(key, attrs[key], instance)) {
continue continue
} }
if (value !== attrs[key]) { if (value !== attrs[key]) {
@ -337,7 +337,7 @@ function setFullProps(
// Any non-declared (either as a prop or an emitted event) props are put // Any non-declared (either as a prop or an emitted event) props are put
// into a separate `attrs` object for spreading. Make sure to preserve // into a separate `attrs` object for spreading. Make sure to preserve
// original key casing // original key casing
if (__COMPAT__ && shouldSkipAttr(key, instance)) { if (__COMPAT__ && shouldSkipAttr(key, attrs[key], instance)) {
continue continue
} }
if (value !== attrs[key]) { if (value !== attrs[key]) {

View File

@ -345,8 +345,13 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
hasOwn(globalProperties, key)) hasOwn(globalProperties, key))
) { ) {
if (__COMPAT__) { if (__COMPAT__) {
const val = globalProperties[key] const desc = Object.getOwnPropertyDescriptor(globalProperties, key)!
return isFunction(val) ? val.bind(instance.proxy) : val if (desc.get) {
return desc.get.call(instance.proxy)
} else {
const val = globalProperties[key]
return isFunction(val) ? val.bind(instance.proxy) : val
}
} else { } else {
return globalProperties[key] return globalProperties[key]
} }