feat: asset resolution
This commit is contained in:
parent
67fd5b6091
commit
015d5dd0f1
19
packages/runtime-core/__tests__/apiCreateApp.spec.ts
Normal file
19
packages/runtime-core/__tests__/apiCreateApp.spec.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
describe('api: createApp', () => {
|
||||||
|
test('mount', () => {})
|
||||||
|
|
||||||
|
test('provide', () => {})
|
||||||
|
|
||||||
|
test('component', () => {})
|
||||||
|
|
||||||
|
test('directive', () => {})
|
||||||
|
|
||||||
|
test('use', () => {})
|
||||||
|
|
||||||
|
test.todo('mixin')
|
||||||
|
|
||||||
|
test('config.errorHandler', () => {})
|
||||||
|
|
||||||
|
test('config.warnHandler', () => {})
|
||||||
|
|
||||||
|
test.todo('config.optionsMergeStrategies')
|
||||||
|
})
|
@ -3,12 +3,14 @@ import {
|
|||||||
Component,
|
Component,
|
||||||
ComponentRenderProxy,
|
ComponentRenderProxy,
|
||||||
Data,
|
Data,
|
||||||
ComponentInstance
|
ComponentInstance,
|
||||||
|
currentRenderingInstance,
|
||||||
|
currentInstance
|
||||||
} from './component'
|
} from './component'
|
||||||
import { Directive } from './directives'
|
import { Directive } from './directives'
|
||||||
import { HostNode, RootRenderFunction } from './createRenderer'
|
import { HostNode, RootRenderFunction } from './createRenderer'
|
||||||
import { InjectionKey } from './apiInject'
|
import { InjectionKey } from './apiInject'
|
||||||
import { isFunction } from '@vue/shared'
|
import { isFunction, camelize, capitalize } from '@vue/shared'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
import { createVNode } from './vnode'
|
import { createVNode } from './vnode'
|
||||||
|
|
||||||
@ -42,15 +44,6 @@ export interface AppConfig {
|
|||||||
instance: ComponentRenderProxy,
|
instance: ComponentRenderProxy,
|
||||||
trace: string
|
trace: string
|
||||||
) => void
|
) => void
|
||||||
ignoredElements: Array<string | RegExp>
|
|
||||||
keyCodes: Record<string, number | number[]>
|
|
||||||
optionMergeStrategies: {
|
|
||||||
[key: string]: (
|
|
||||||
parent: any,
|
|
||||||
child: any,
|
|
||||||
instance: ComponentRenderProxy
|
|
||||||
) => any
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AppContext {
|
export interface AppContext {
|
||||||
@ -76,10 +69,7 @@ export function createAppContext(): AppContext {
|
|||||||
devtools: true,
|
devtools: true,
|
||||||
performance: false,
|
performance: false,
|
||||||
errorHandler: undefined,
|
errorHandler: undefined,
|
||||||
warnHandler: undefined,
|
warnHandler: undefined
|
||||||
ignoredElements: [],
|
|
||||||
keyCodes: {},
|
|
||||||
optionMergeStrategies: {}
|
|
||||||
},
|
},
|
||||||
mixins: [],
|
mixins: [],
|
||||||
components: {},
|
components: {},
|
||||||
@ -168,3 +158,29 @@ export function createAppAPI(render: RootRenderFunction): () => App {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveAsset(type: 'components' | 'directives', name: string) {
|
||||||
|
const instance = currentRenderingInstance || currentInstance
|
||||||
|
if (instance) {
|
||||||
|
let camelized
|
||||||
|
let capitalized
|
||||||
|
const local = (instance.type as any)[type]
|
||||||
|
const global = instance.appContext[type]
|
||||||
|
const res =
|
||||||
|
local[name] ||
|
||||||
|
local[(camelized = camelize(name))] ||
|
||||||
|
local[(capitalized = capitalize(name))] ||
|
||||||
|
global[name] ||
|
||||||
|
global[camelized] ||
|
||||||
|
global[capitalized]
|
||||||
|
if (__DEV__ && !res) {
|
||||||
|
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
} else if (__DEV__) {
|
||||||
|
warn(
|
||||||
|
`resolve${capitalize(type.slice(0, -1))} ` +
|
||||||
|
`can only be used in render() or setup().`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -13,7 +13,7 @@ import {
|
|||||||
callWithErrorHandling,
|
callWithErrorHandling,
|
||||||
callWithAsyncErrorHandling
|
callWithAsyncErrorHandling
|
||||||
} from './errorHandling'
|
} from './errorHandling'
|
||||||
import { AppContext, createAppContext } from './apiCreateApp'
|
import { AppContext, createAppContext, resolveAsset } from './apiCreateApp'
|
||||||
|
|
||||||
export type Data = { [key: string]: unknown }
|
export type Data = { [key: string]: unknown }
|
||||||
|
|
||||||
@ -472,3 +472,7 @@ function hasPropsChanged(prevProps: Data, nextProps: Data): boolean {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveComponent(name: string): Component | undefined {
|
||||||
|
return resolveAsset('components', name)
|
||||||
|
}
|
||||||
|
@ -22,6 +22,7 @@ import {
|
|||||||
} from './component'
|
} from './component'
|
||||||
import { callWithAsyncErrorHandling, ErrorTypes } from './errorHandling'
|
import { callWithAsyncErrorHandling, ErrorTypes } from './errorHandling'
|
||||||
import { HostNode } from './createRenderer'
|
import { HostNode } from './createRenderer'
|
||||||
|
import { resolveAsset } from './apiCreateApp'
|
||||||
|
|
||||||
export interface DirectiveBinding {
|
export interface DirectiveBinding {
|
||||||
instance: ComponentRenderProxy | null
|
instance: ComponentRenderProxy | null
|
||||||
@ -119,11 +120,6 @@ export function applyDirectives(
|
|||||||
return vnode
|
return vnode
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveDirective(name: string): Directive {
|
|
||||||
// TODO
|
|
||||||
return {} as any
|
|
||||||
}
|
|
||||||
|
|
||||||
export function invokeDirectiveHook(
|
export function invokeDirectiveHook(
|
||||||
hook: Function | Function[],
|
hook: Function | Function[],
|
||||||
instance: ComponentInstance | null,
|
instance: ComponentInstance | null,
|
||||||
@ -144,3 +140,7 @@ export function invokeDirectiveHook(
|
|||||||
callWithAsyncErrorHandling(hook, instance, ErrorTypes.DIRECTIVE_HOOK, args)
|
callWithAsyncErrorHandling(hook, instance, ErrorTypes.DIRECTIVE_HOOK, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveDirective(name: string): Directive | undefined {
|
||||||
|
return resolveAsset('directives', name)
|
||||||
|
}
|
||||||
|
@ -37,6 +37,7 @@ export {
|
|||||||
} from './errorHandling'
|
} from './errorHandling'
|
||||||
|
|
||||||
// For the compiler
|
// For the compiler
|
||||||
|
export { resolveComponent } from './component'
|
||||||
export { applyDirectives, resolveDirective } from './directives'
|
export { applyDirectives, resolveDirective } from './directives'
|
||||||
|
|
||||||
// Types -----------------------------------------------------------------------
|
// Types -----------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user