Skip to content

Commit

Permalink
feat(runtime-core): support app.config.globalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 25, 2020
1 parent 394fd4c commit 27873db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
34 changes: 34 additions & 0 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,38 @@ describe('api: createApp', () => {
).toHaveBeenWarned()
})
})

test('config.optionMergeStrategies', () => {
let merged: string
const App = defineComponent({
render() {},
mixins: [{ foo: 'mixin' }],
extends: { foo: 'extends' },
foo: 'local',
beforeCreate() {
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')
})

test('config.globalProperties', () => {
const app = createApp({
render() {
return this.foo
}
})
app.config.globalProperties.foo = 'hello'
const root = nodeOps.createElement('div')
app.mount(root)
expect(serializeInner(root)).toBe('hello')
})
})
22 changes: 0 additions & 22 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,28 +563,6 @@ 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',
beforeCreate() {
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
2 changes: 2 additions & 0 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface AppConfig {
devtools: boolean
performance: boolean
optionMergeStrategies: Record<string, OptionMergeFunction>
globalProperties: Record<string, any>
isCustomElement: (tag: string) => boolean
errorHandler?: (
err: unknown,
Expand Down Expand Up @@ -86,6 +87,7 @@ export function createAppContext(): AppContext {
isNativeTag: NO,
devtools: true,
performance: false,
globalProperties: {},
optionMergeStrategies: {},
isCustomElement: NO,
errorHandler: undefined,
Expand Down
21 changes: 17 additions & 4 deletions packages/runtime-core/src/componentProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ const enum AccessTypes {

export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
get(target: ComponentInternalInstance, key: string) {
const { renderContext, data, propsProxy, accessCache, type, sink } = target
const {
renderContext,
data,
propsProxy,
accessCache,
type,
sink,
appContext
} = target

// data / props / renderContext
// This getter gets called for every property access on the render context
Expand Down Expand Up @@ -118,19 +126,24 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {

// public $xxx properties & user-attached properties (sink)
const publicGetter = publicPropertiesMap[key]
let cssModule
let cssModule, globalProperties
if (publicGetter) {
if (__DEV__ && key === '$attrs') {
markAttrsAccessed()
}
return publicGetter(target)
} else if (hasOwn(sink, key)) {
return sink[key]
} else if (
(cssModule = type.__cssModules) &&
(cssModule = cssModule[key])
) {
return cssModule
} else if (hasOwn(sink, key)) {
return sink[key]
} else if (
((globalProperties = appContext.config.globalProperties),
hasOwn(globalProperties, key))
) {
return globalProperties[key]
} else if (__DEV__ && currentRenderingInstance) {
warn(
`Property ${JSON.stringify(key)} was accessed during render ` +
Expand Down

0 comments on commit 27873db

Please sign in to comment.