]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: allow compatConfig mode to be a function
authorEvan You <yyx990803@gmail.com>
Thu, 6 May 2021 14:57:34 +0000 (10:57 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 6 May 2021 14:57:34 +0000 (10:57 -0400)
packages/runtime-core/src/compat/compatConfig.ts
packages/vue-compat/__tests__/misc.spec.ts

index cbff8c785636f9a55b827591fd997d212b7a24bd..e786cf09814286e21fedf14df47e05ac6faf63ce 100644 (file)
@@ -1,5 +1,6 @@
-import { extend, hasOwn, isArray } from '@vue/shared'
+import { extend, hasOwn, isArray, isFunction } from '@vue/shared'
 import {
+  Component,
   ComponentInternalInstance,
   ComponentOptions,
   formatComponentName,
@@ -505,7 +506,7 @@ export function warnDeprecation(
 export type CompatConfig = Partial<
   Record<DeprecationTypes, boolean | 'suppress-warning'>
 > & {
-  MODE?: 2 | 3
+  MODE?: 2 | 3 | ((comp: Component | null) => 2 | 3)
 }
 
 export const globalCompatConfig: CompatConfig = {
@@ -574,8 +575,13 @@ export function isCompatEnabled(
     return false
   }
 
-  const mode = getCompatConfigForKey('MODE', instance) || 2
+  const rawMode = getCompatConfigForKey('MODE', instance) || 2
   const val = getCompatConfigForKey(key, instance)
+
+  const mode = isFunction(rawMode)
+    ? rawMode(instance && instance.type)
+    : rawMode
+
   if (mode === 2) {
     return val !== false
   } else {
index eba512d0888d8adb7eacdf4789c12c7c2a75af6f..1986ae77db81f7af19a8da6f33078a82f795f119 100644 (file)
@@ -6,6 +6,7 @@ import {
   toggleDeprecationWarning
 } from '../../runtime-core/src/compat/compatConfig'
 import { triggerEvent } from './utils'
+import { h } from '@vue/runtime-core'
 
 beforeEach(() => {
   toggleDeprecationWarning(true)
@@ -20,6 +21,31 @@ afterEach(() => {
   Vue.configureCompat({ MODE: 3 })
 })
 
+test('mode as function', () => {
+  const Foo = {
+    name: 'Foo',
+    render: (h: any) => h('div', 'foo')
+  }
+
+  const Bar = {
+    name: 'Bar',
+    data: () => ({ msg: 'bar' }),
+    render: (ctx: any) => h('div', ctx.msg)
+  }
+
+  toggleDeprecationWarning(false)
+  Vue.configureCompat({
+    MODE: comp => (comp && comp.name === 'Bar' ? 3 : 2)
+  })
+
+  const vm = new Vue({
+    components: { Foo, Bar },
+    template: `<div><foo/><bar/></div>`
+  }).$mount()
+
+  expect(vm.$el.innerHTML).toBe(`<div>foo</div><div>bar</div>`)
+})
+
 test('WATCH_ARRAY', async () => {
   const spy = jest.fn()
   const vm = new Vue({