Skip to content

Commit a75b8a2

Browse files
committed
fix(build): fix component resolution when disabling options API
fix #1688
1 parent ba17c87 commit a75b8a2

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

packages/runtime-core/src/component.ts

+14-23
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { Slots, initSlots, InternalSlots } from './componentSlots'
2424
import { warn } from './warning'
2525
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
2626
import { AppContext, createAppContext, AppConfig } from './apiCreateApp'
27-
import { Directive, validateDirectiveName } from './directives'
27+
import { validateDirectiveName } from './directives'
2828
import { applyOptions, ComponentOptions } from './componentOptions'
2929
import {
3030
EmitsOptions,
@@ -223,17 +223,6 @@ export interface ComponentInternalInstance {
223223
*/
224224
renderCache: (Function | VNode)[]
225225

226-
/**
227-
* Asset hashes that prototypally inherits app-level asset hashes for fast
228-
* resolution
229-
* @internal
230-
*/
231-
components: Record<string, Component>
232-
/**
233-
* @internal
234-
*/
235-
directives: Record<string, Directive>
236-
237226
// the rest are only for stateful components ---------------------------------
238227

239228
// main proxy that serves as the public instance (`this`)
@@ -354,15 +343,17 @@ export function createComponentInstance(
354343
parent: ComponentInternalInstance | null,
355344
suspense: SuspenseBoundary | null
356345
) {
346+
const type = vnode.type as Component
357347
// inherit parent app context - or - if root, adopt from root vnode
358348
const appContext =
359349
(parent ? parent.appContext : vnode.appContext) || emptyAppContext
350+
360351
const instance: ComponentInternalInstance = {
361352
uid: uid++,
362353
vnode,
354+
type,
363355
parent,
364356
appContext,
365-
type: vnode.type as Component,
366357
root: null!, // to be immediately set
367358
next: null,
368359
subTree: null!, // will be set synchronously right after creation
@@ -385,10 +376,6 @@ export function createComponentInstance(
385376
setupState: EMPTY_OBJ,
386377
setupContext: null,
387378

388-
// per-instance asset storage (mutable during options resolution)
389-
components: Object.create(appContext.components),
390-
directives: Object.create(appContext.directives),
391-
392379
// suspense related
393380
suspense,
394381
asyncDep: null,
@@ -727,14 +714,18 @@ export function formatComponentName(
727714
}
728715

729716
if (!name && instance && instance.parent) {
730-
// try to infer the name based on local resolution
731-
const registry = instance.parent.components
732-
for (const key in registry) {
733-
if (registry[key] === Component) {
734-
name = key
735-
break
717+
// try to infer the name based on reverse resolution
718+
const inferFromRegistry = (registry: Record<string, any> | undefined) => {
719+
for (const key in registry) {
720+
if (registry[key] === Component) {
721+
return key
722+
}
736723
}
737724
}
725+
name =
726+
inferFromRegistry(
727+
(instance.parent.type as ComponentOptions).components
728+
) || inferFromRegistry(instance.appContext.components)
738729
}
739730

740731
return name ? classify(name) : isRoot ? `App` : `Anonymous`

packages/runtime-core/src/componentOptions.ts

-11
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,6 @@ export function applyOptions(
381381
watch: watchOptions,
382382
provide: provideOptions,
383383
inject: injectOptions,
384-
// assets
385-
components,
386-
directives,
387384
// lifecycle
388385
beforeMount,
389386
mounted,
@@ -570,14 +567,6 @@ export function applyOptions(
570567
}
571568
}
572569

573-
// asset options
574-
if (components) {
575-
extend(instance.components, components)
576-
}
577-
if (directives) {
578-
extend(instance.directives, directives)
579-
}
580-
581570
// lifecycle options
582571
if (!asMixin) {
583572
callSyncHook('created', options, publicThis, globalMixins)

packages/runtime-core/src/helpers/resolveAssets.ts

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { currentRenderingInstance } from '../componentRenderUtils'
2-
import { currentInstance, Component, FunctionalComponent } from '../component'
2+
import {
3+
currentInstance,
4+
Component,
5+
FunctionalComponent,
6+
ComponentOptions
7+
} from '../component'
38
import { Directive } from '../directives'
49
import { camelize, capitalize, isString } from '@vue/shared'
510
import { warn } from '../warning'
@@ -58,24 +63,27 @@ function resolveAsset(
5863
) {
5964
const instance = currentRenderingInstance || currentInstance
6065
if (instance) {
61-
let camelized, capitalized
62-
const registry = instance[type]
63-
let res =
64-
registry[name] ||
65-
registry[(camelized = camelize(name))] ||
66-
registry[(capitalized = capitalize(camelized))]
67-
if (!res && type === COMPONENTS) {
68-
const self = instance.type
69-
const selfName = (self as FunctionalComponent).displayName || self.name
66+
const Component = instance.type
67+
68+
// self name has highest priority
69+
if (type === COMPONENTS) {
70+
const selfName =
71+
(Component as FunctionalComponent).displayName || Component.name
7072
if (
7173
selfName &&
7274
(selfName === name ||
73-
selfName === camelized ||
74-
selfName === capitalized)
75+
selfName === camelize(name) ||
76+
selfName === capitalize(camelize(name)))
7577
) {
76-
res = self
78+
return Component
7779
}
7880
}
81+
82+
const res =
83+
// local registration
84+
resolve((Component as ComponentOptions)[type], name) ||
85+
// global registration
86+
resolve(instance.appContext[type], name)
7987
if (__DEV__ && warnMissing && !res) {
8088
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
8189
}
@@ -87,3 +95,12 @@ function resolveAsset(
8795
)
8896
}
8997
}
98+
99+
function resolve(registry: Record<string, any> | undefined, name: string) {
100+
return (
101+
registry &&
102+
(registry[name] ||
103+
registry[camelize(name)] ||
104+
registry[capitalize(camelize(name))])
105+
)
106+
}

0 commit comments

Comments
 (0)