]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: compat configuration
authorEvan You <yyx990803@gmail.com>
Wed, 7 Apr 2021 15:22:56 +0000 (11:22 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 7 Apr 2021 20:19:24 +0000 (16:19 -0400)
packages/runtime-core/src/compat/compatConfig.ts [new file with mode: 0644]
packages/runtime-core/src/compat/deprecations.ts
packages/runtime-core/src/compat/global.ts
packages/runtime-core/src/index.ts
packages/runtime-dom/src/directives/vOn.ts
packages/runtime-dom/src/index.ts
packages/vue-compat/src/index.ts
packages/vue-compat/src/runtime.ts

diff --git a/packages/runtime-core/src/compat/compatConfig.ts b/packages/runtime-core/src/compat/compatConfig.ts
new file mode 100644 (file)
index 0000000..f639588
--- /dev/null
@@ -0,0 +1,26 @@
+import { extend } from '@vue/shared'
+import { DeprecationTypes } from './deprecations'
+
+export type CompatConfig = Partial<
+  Record<DeprecationTypes, DeprecationConfigItem>
+>
+
+export interface DeprecationConfigItem {
+  warning?: boolean // defaults to true
+  mode?: 2 | 3 // defaults to 2
+}
+
+const globalCompatConfig: CompatConfig = {}
+
+export function configureCompat(config: CompatConfig) {
+  extend(globalCompatConfig, config)
+}
+
+/**
+ * @internal
+ */
+export function getCompatConfig(
+  key: DeprecationTypes
+): DeprecationConfigItem | undefined {
+  return globalCompatConfig[key]
+}
index 73eca67205d524339e759b0c84b25770a4140f32..e345a0b26c32e6fe199a91658e9f856b1c52f164 100644 (file)
@@ -1,37 +1,38 @@
 import { isRuntimeOnly } from '../component'
 import { warn } from '../warning'
+import { getCompatConfig } from './compatConfig'
 
 export const enum DeprecationTypes {
-  CONFIG_SILENT,
-  CONFIG_DEVTOOLS,
-  CONFIG_KEY_CODES,
-  CONFIG_PRODUCTION_TIP,
-  CONFIG_IGNORED_ELEMENTS,
-
-  GLOBAL_PROTOTYPE,
-  GLOBAL_SET,
-  GLOBAL_DELETE,
-  GLOBAL_OBSERVABLE,
-  GLOBAL_DOM_TEMPLATE_MOUNT,
-
-  INSTANCE_SET,
-  INSTANCE_DELETE,
-  INSTANCE_MOUNT,
-  INSTANCE_DESTROY,
-  INSTANCE_EVENT_EMITTER,
-  INSTANCE_EVENT_HOOKS,
-  INSTANCE_CHILDREN,
-
-  OPTIONS_DATA_FN,
-  OPTIONS_DATA_MERGE,
-  OPTIONS_BEFORE_DESTROY,
-  OPTIONS_DESTROYED,
-
-  PROPS_DEFAULT_THIS,
-
-  CUSTOM_DIR,
-
-  V_ON_KEYCODE_MODIFIER
+  CONFIG_SILENT = 'CONFIG_SILENT',
+  CONFIG_DEVTOOLS = 'CONFIG_DEVTOOLS',
+  CONFIG_KEY_CODES = 'CONFIG_KEY_CODES',
+  CONFIG_PRODUCTION_TIP = 'CONFIG_PRODUCTION_TIP',
+  CONFIG_IGNORED_ELEMENTS = 'CONFIG_IGNORED_ELEMENTS',
+
+  GLOBAL_PROTOTYPE = 'GLOBAL_PROTOTYPE',
+  GLOBAL_SET = 'GLOBAL_SET',
+  GLOBAL_DELETE = 'GLOBAL_DELETE',
+  GLOBAL_OBSERVABLE = 'GLOBAL_OBSERVABLE',
+  GLOBAL_MOUNT_CONTAINER = 'GLOBAL_MOUNT_CONTAINER',
+
+  INSTANCE_SET = 'INSTANCE_SET',
+  INSTANCE_DELETE = 'INSTANCE_DELETE',
+  INSTANCE_MOUNT = 'INSTANCE_MOUNT',
+  INSTANCE_DESTROY = 'INSTANCE_DESTROY',
+  INSTANCE_EVENT_EMITTER = 'INSTANCE_EVENT_EMITTER',
+  INSTANCE_EVENT_HOOKS = 'INSTANCE_EVENT_HOOKS',
+  INSTANCE_CHILDREN = 'INSTANCE_CHILDREN',
+
+  OPTIONS_DATA_FN = 'OPTIONS_DATA_FN',
+  OPTIONS_DATA_MERGE = 'OPTIONS_DATA_MERGE',
+  OPTIONS_BEFORE_DESTROY = 'OPTIONS_BEFORE_DESTROY',
+  OPTIONS_DESTROYED = 'OPTIONS_DESTROYED',
+
+  PROPS_DEFAULT_THIS = 'PROPS_DEFAULT_THIS',
+
+  CUSTOM_DIR = 'CUSTOM_DIR',
+
+  V_ON_KEYCODE_MODIFIER = 'V_ON_KEYCODE_MODIFIER'
 }
 
 type DeprecationData = {
@@ -39,7 +40,7 @@ type DeprecationData = {
   link?: string
 }
 
-const deprecations: Record<DeprecationTypes, DeprecationData> = {
+const deprecationMessages: Record<DeprecationTypes, DeprecationData> = {
   [DeprecationTypes.CONFIG_SILENT]: {
     message:
       `config.silent has been removed because it is not good practice to ` +
@@ -105,7 +106,7 @@ const deprecations: Record<DeprecationTypes, DeprecationData> = {
     link: `https://v3.vuejs.org/api/basic-reactivity.html`
   },
 
-  [DeprecationTypes.GLOBAL_DOM_TEMPLATE_MOUNT]: {
+  [DeprecationTypes.GLOBAL_MOUNT_CONTAINER]: {
     message:
       `Vue detected directives on the mount container. ` +
       `In Vue 3, the container is no longer considered part of the template ` +
@@ -204,18 +205,30 @@ const deprecations: Record<DeprecationTypes, DeprecationData> = {
 
 const hasWarned: Record<string, boolean> = {}
 
+/**
+ * @internal
+ */
 export function warnDeprecation(key: DeprecationTypes, ...args: any[]) {
-  if (!__COMPAT__ || !__DEV__) {
+  if (!__DEV__) {
     return
   }
+
+  // check user config
+  const config = getCompatConfig(key)
+  if (config && config.warning === false) {
+    return
+  }
+
+  // avoid spamming the same message
   const dupKey = key + args.join('')
   if (hasWarned[dupKey]) {
     return
   }
+
   hasWarned[dupKey] = true
-  const { message, link } = deprecations[key]
+  const { message, link } = deprecationMessages[key]
   warn(
-    `[DEPRECATION] ${
+    `(DEPRECATION ${key}) ${
       typeof message === 'function' ? message(...args) : message
     }${link ? `\n  Details: ${link}` : ``}`
   )
index b475a6595a191ba2897b29a0c4f6902695d03d6d..b94ac23c17847c29f1eb0987f4ad02ffa8ae6088 100644 (file)
@@ -29,6 +29,7 @@ import { warnDeprecation, DeprecationTypes } from './deprecations'
 import { version } from '..'
 import { LegacyConfig } from './globalConfig'
 import { LegacyDirective } from './customDirective'
+import { configureCompat } from './compatConfig'
 
 /**
  * @deprecated the default `Vue` export has been removed in Vue 3. The type for
@@ -73,6 +74,8 @@ export type CompatVue = Pick<App, 'version' | 'component' | 'directive'> & {
    * @deprecated filters have been removed from Vue 3.
    */
   filter(name: string, arg: any): null
+
+  configureCompat: typeof configureCompat
 }
 
 export let isCopyingConfig = false
@@ -81,11 +84,6 @@ export let isCopyingConfig = false
 export function createCompatVue(
   createApp: CreateAppFunction<Element>
 ): CompatVue {
-  if (!__COMPAT__) {
-    // @ts-ignore this function will never be called in non-compat mode
-    return
-  }
-
   const Vue: CompatVue = function Vue(options: ComponentOptions = {}) {
     return createCompatApp(options, Vue)
   } as any
@@ -215,6 +213,8 @@ export function createCompatVue(
     // TODO compiler warning for filters (maybe behavior compat?)
   }) as any
 
+  Vue.configureCompat = configureCompat
+
   return Vue
 }
 
@@ -306,7 +306,7 @@ export function installCompatMount(
           for (let i = 0; i < container.attributes.length; i++) {
             const attr = container.attributes[i]
             if (attr.name !== 'v-cloak' && /^(v-|:|@)/.test(attr.name)) {
-              warnDeprecation(DeprecationTypes.GLOBAL_DOM_TEMPLATE_MOUNT)
+              warnDeprecation(DeprecationTypes.GLOBAL_MOUNT_CONTAINER)
               break
             }
           }
index 54a589f83b41011b2dbcf9a2bb9c3959830eff15..569b51abab219b1ef1407aabb8a8c27c333777fa 100644 (file)
@@ -282,8 +282,20 @@ export const ssrUtils = (__NODE_JS__ ? _ssrUtils : null) as typeof _ssrUtils
 
 // 2.x COMPAT ------------------------------------------------------------------
 
-// Important: every function exported here must have `if (!__COMPAT__) return`
-// checks
-export { warnDeprecation, DeprecationTypes } from './compat/deprecations'
-export { createCompatVue, CompatVue } from './compat/global'
+export { DeprecationTypes } from './compat/deprecations'
+export { CompatVue } from './compat/global'
 export { LegacyConfig } from './compat/globalConfig'
+
+import { warnDeprecation } from './compat/deprecations'
+import { createCompatVue } from './compat/global'
+import { getCompatConfig } from './compat/compatConfig'
+
+const _compatUtils = {
+  warnDeprecation,
+  createCompatVue,
+  getCompatConfig
+}
+
+export const compatUtils = (__COMPAT__
+  ? _compatUtils
+  : null) as typeof _compatUtils
index 93d62ec6b36d3afbe6f86ce01133817a1e26535b..a9118961f6b730e9750517260203e8bb3aba6fab 100644 (file)
@@ -1,8 +1,8 @@
 import {
-  DeprecationTypes,
-  warnDeprecation,
   getCurrentInstance,
-  LegacyConfig
+  DeprecationTypes,
+  LegacyConfig,
+  compatUtils
 } from '@vue/runtime-core'
 import { hyphenate, isArray } from '@vue/shared'
 
@@ -62,7 +62,7 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
     keyCodes = ((getCurrentInstance()!.appContext
       .config as any) as LegacyConfig).keyCodes
     if (__DEV__ && modifiers.some(m => /^\d+$/.test(m))) {
-      warnDeprecation(DeprecationTypes.V_ON_KEYCODE_MODIFIER)
+      compatUtils.warnDeprecation(DeprecationTypes.V_ON_KEYCODE_MODIFIER)
     }
   }
 
index 77adc6aa16b67bace141416117bde22ccda49c53..767b70074e6262f3c62c88f71d71a18a72effed9 100644 (file)
@@ -9,8 +9,8 @@ import {
   App,
   RootHydrateFunction,
   isRuntimeOnly,
-  warnDeprecation,
-  DeprecationTypes
+  DeprecationTypes,
+  compatUtils
 } from '@vue/runtime-core'
 import { nodeOps } from './nodeOps'
 import { patchProp, forcePatchProp } from './patchProp'
@@ -78,7 +78,7 @@ export const createApp = ((...args) => {
         for (let i = 0; i < container.attributes.length; i++) {
           const attr = container.attributes[i]
           if (attr.name !== 'v-cloak' && /^(v-|:|@)/.test(attr.name)) {
-            warnDeprecation(DeprecationTypes.GLOBAL_DOM_TEMPLATE_MOUNT)
+            compatUtils.warnDeprecation(DeprecationTypes.GLOBAL_MOUNT_CONTAINER)
             break
           }
         }
index d766d248720745da69ea838b53fd2d8e791287f1..09036c5d4f79ead4fdf04e32a8ff97fdb430a739 100644 (file)
@@ -7,7 +7,7 @@ import {
   RenderFunction,
   warn,
   createApp,
-  createCompatVue
+  compatUtils
 } from '@vue/runtime-dom'
 import { isString, NOOP, generateCodeFrame, extend } from '@vue/shared'
 import { InternalRenderFunction } from 'packages/runtime-core/src/component'
@@ -92,9 +92,10 @@ function compileToFunction(
 
 registerRuntimeCompiler(compileToFunction)
 
-const Vue = createCompatVue(createApp)
+const Vue = compatUtils.createCompatVue(createApp)
 
 Vue.compile = compileToFunction
+
 extend(Vue, runtimeDom)
 
 export default Vue
index 04b60b36b7df902ede194a9d483bec9e70a441a8..a9f19cf7067004e67b8ca625e364d6a5b02ac0ae 100644 (file)
@@ -1,15 +1,18 @@
 // This entry exports the runtime only, and is built as
 // `dist/vue.esm-bundler.js` which is used by default for bundlers.
 import { initDev } from './dev'
-import { warn } from '@vue/runtime-dom'
+import { compatUtils, createApp, warn } from '@vue/runtime-dom'
+import { extend } from '@vue/shared'
 
 if (__DEV__) {
   initDev()
 }
 
-export * from '@vue/runtime-dom'
+import * as runtimeDom from '@vue/runtime-dom'
 
-export const compile = () => {
+const Vue = compatUtils.createCompatVue(createApp)
+
+Vue.compile = (() => {
   if (__DEV__) {
     warn(
       `Runtime compilation is not supported in this build of Vue.` +
@@ -22,4 +25,8 @@ export const compile = () => {
               : ``) /* should not happen */
     )
   }
-}
+}) as any
+
+extend(Vue, runtimeDom)
+
+export default Vue