wip: support configuring compiler deprecations at runtime + warn invalid deprecation configs

This commit is contained in:
Evan You
2021-04-16 12:19:12 -04:00
parent 79cbf21c3e
commit bbf708dbe9
5 changed files with 72 additions and 22 deletions

View File

@@ -456,12 +456,48 @@ export type CompatConfig = Partial<
MODE?: 2 | 3
}
const globalCompatConfig: CompatConfig = {}
export const globalCompatConfig: CompatConfig = {}
export function configureCompat(config: CompatConfig) {
if (__DEV__) {
validateCompatConfig(config)
}
extend(globalCompatConfig, config)
}
const seenConfigObjects = /*#__PURE__*/ new WeakSet<CompatConfig>()
const warnedInvalidKeys: Record<string, boolean> = {}
// dev only
export function validateCompatConfig(config: CompatConfig) {
if (seenConfigObjects.has(config)) {
return
}
seenConfigObjects.add(config)
for (const key of Object.keys(config)) {
if (
key !== 'MODE' &&
!(key in deprecationData) &&
!(key in warnedInvalidKeys)
) {
if (key.startsWith('COMPILER_')) {
if (isRuntimeOnly()) {
warn(
`Depreaction config "${key}" is compiler-specific and you are ` +
`running a runtime-only build of Vue. This deprecation should be ` +
`configured via compiler options in your build setup instead.`
// TODO link to migration build docs on build setup
)
}
} else {
warn(`Invalid deprecation config "${key}".`)
}
warnedInvalidKeys[key] = true
}
}
}
export function getCompatConfigForKey(
key: DeprecationTypes | 'MODE',
instance: ComponentInternalInstance | null

View File

@@ -47,7 +47,8 @@ import {
NO,
makeMap,
isPromise,
ShapeFlags
ShapeFlags,
extend
} from '@vue/shared'
import { SuspenseBoundary } from './components/Suspense'
import { CompilerOptions } from '@vue/compiler-core'
@@ -55,6 +56,7 @@ import { markAttrsAccessed } from './componentRenderUtils'
import { currentRenderingInstance } from './componentRenderContext'
import { startMeasure, endMeasure } from './profiling'
import { convertLegacyRenderFn } from './compat/renderFn'
import { globalCompatConfig, validateCompatConfig } from './compat/compatConfig'
export type Data = Record<string, unknown>
@@ -692,6 +694,10 @@ export function finishComponentSetup(
if (__COMPAT__) {
convertLegacyRenderFn(instance)
if (__DEV__ && Component.compatConfig) {
validateCompatConfig(Component.compatConfig)
}
}
// template / render function normalization
@@ -710,10 +716,18 @@ export function finishComponentSetup(
if (__DEV__) {
startMeasure(instance, `compile`)
}
Component.render = compile(Component.template, {
const compilerOptions: CompilerOptions = {
isCustomElement: instance.appContext.config.isCustomElement,
delimiters: Component.delimiters
})
}
if (__COMPAT__) {
// pass runtime compat config into the compiler
compilerOptions.compatConfig = Object.create(globalCompatConfig)
if (Component.compatConfig) {
extend(compilerOptions.compatConfig, Component.compatConfig)
}
}
Component.render = compile(Component.template, compilerOptions)
if (__DEV__) {
endMeasure(instance, `compile`)
}