Skip to content

Commit

Permalink
feat(runtime-core): support config.optionMergeStrategies
Browse files Browse the repository at this point in the history
Note the behavior is different from Vue 2:
- merge strategies no longer apply to built-in options.
- the default value is now an empty object and no longer exposes merge
  strategies for built-in options.
  • Loading branch information
yyx990803 committed Mar 24, 2020
1 parent 1237387 commit 528621b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
25 changes: 24 additions & 1 deletion packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
nextTick,
renderToString,
ref,
defineComponent
defineComponent,
createApp
} from '@vue/runtime-test'
import { mockWarn } from '@vue/shared'

Expand Down Expand Up @@ -562,6 +563,28 @@ describe('api: options', () => {
expect(serializeInner(root)).toBe(`<div>1,1,3</div>`)
})

test('optionMergeStrategies', () => {
let merged: string
const App = defineComponent({
render() {},
mixins: [{ foo: 'mixin' }],
extends: { foo: 'extends' },
foo: 'local',
mounted() {
merged = this.$options.foo
}
})

const app = createApp(App)
app.mixin({
foo: 'global'
})
app.config.optionMergeStrategies.foo = (a, b) => (a ? `${a},` : ``) + b

app.mount(nodeOps.createElement('div'))
expect(merged!).toBe('global,extends,mixin,local')
})

describe('warnings', () => {
mockWarn()

Expand Down
17 changes: 14 additions & 3 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ export interface App<HostElement = any> {
_context: AppContext
}

export type OptionMergeFunction = (
to: unknown,
from: unknown,
instance: any,
key: string
) => any

export interface AppConfig {
// @private
readonly isNativeTag?: (tag: string) => boolean

devtools: boolean
performance: boolean
readonly isNativeTag?: (tag: string) => boolean
isCustomElement?: (tag: string) => boolean
optionMergeStrategies: Record<string, OptionMergeFunction>
isCustomElement: (tag: string) => boolean
errorHandler?: (
err: unknown,
instance: ComponentPublicInstance | null,
Expand Down Expand Up @@ -73,9 +83,10 @@ export type Plugin =
export function createAppContext(): AppContext {
return {
config: {
isNativeTag: NO,
devtools: true,
performance: false,
isNativeTag: NO,
optionMergeStrategies: {},
isCustomElement: NO,
errorHandler: undefined,
warnHandler: undefined
Expand Down
43 changes: 39 additions & 4 deletions packages/runtime-core/src/apiOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isObject,
isArray,
EMPTY_OBJ,
NOOP
NOOP,
hasOwn
} from '@vue/shared'
import { computed } from './apiComputed'
import { watch, WatchOptions, WatchCallback } from './apiWatch'
Expand Down Expand Up @@ -75,11 +76,16 @@ export interface ComponentOptionsBase<
directives?: Record<string, Directive>
inheritAttrs?: boolean

// Internal ------------------------------------------------------------------

// marker for AsyncComponentWrapper
__asyncLoader?: () => Promise<Component>
// cache for merged $options
__merged?: ComponentOptions

// type-only differentiator to separate OptionWithoutProps from a constructor
// type returned by defineComponent() or FunctionalComponent
call?: never
// marker for AsyncComponentWrapper
__asyncLoader?: () => Promise<Component>
// type-only differentiators for built-in Vnode types
__isFragment?: never
__isPortal?: never
Expand Down Expand Up @@ -161,7 +167,8 @@ export interface LegacyOptions<
C extends ComputedOptions,
M extends MethodOptions
> {
el?: any
// allow any custom options
[key: string]: any

// state
// Limitation: we cannot expose RawBindings on the `this` context for data
Expand Down Expand Up @@ -501,3 +508,31 @@ function createWatcher(
warn(`Invalid watch option: "${key}"`)
}
}

export function resolveMergedOptions(
instance: ComponentInternalInstance
): ComponentOptions {
const raw = instance.type as ComponentOptions
const { __merged, mixins, extends: extendsOptions } = raw
if (__merged) return __merged
const globalMixins = instance.appContext.mixins
if (!globalMixins && !mixins && !extendsOptions) return raw
const options = {}
globalMixins && globalMixins.forEach(m => mergeOptions(options, m, instance))
extendsOptions && mergeOptions(options, extendsOptions, instance)
mixins && mixins.forEach(m => mergeOptions(options, m, instance))
mergeOptions(options, raw, instance)
return (raw.__merged = options)
}

function mergeOptions(to: any, from: any, instance: ComponentInternalInstance) {
const strats = instance.appContext.config.optionMergeStrategies
for (const key in from) {
const strat = strats && strats[key]
if (strat) {
to[key] = strat(to[key], from[key], instance.proxy, key)
} else if (!hasOwn(to, key)) {
to[key] = from[key]
}
}
}
5 changes: 3 additions & 2 deletions packages/runtime-core/src/componentProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ExtractComputedReturns,
ComponentOptionsBase,
ComputedOptions,
MethodOptions
MethodOptions,
resolveMergedOptions
} from './apiOptions'
import { ReactiveEffect, UnwrapRef } from '@vue/reactivity'
import { warn } from './warning'
Expand Down Expand Up @@ -61,7 +62,7 @@ const publicPropertiesMap: Record<
$parent: i => i.parent,
$root: i => i.root,
$emit: i => i.emit,
$options: i => i.type,
$options: i => (__FEATURE_OPTIONS__ ? resolveMergedOptions(i) : i.type),
$forceUpdate: i => () => queueJob(i.update),
$nextTick: () => nextTick,
$watch: __FEATURE_OPTIONS__ ? i => instanceWatch.bind(i) : NOOP
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export {
AppConfig,
AppContext,
Plugin,
CreateAppFunction
CreateAppFunction,
OptionMergeFunction
} from './apiCreateApp'
export {
VNode,
Expand Down

0 comments on commit 528621b

Please sign in to comment.