Skip to content

Commit 07444b3

Browse files
committed
feat(runtime-vapor): try to support devtools
1 parent 7d90c88 commit 07444b3

File tree

8 files changed

+60
-18
lines changed

8 files changed

+60
-18
lines changed

packages/runtime-vapor/src/apiCreateVaporApp.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NO, isFunction, isObject } from '@vue/shared'
1+
import { NO, getGlobalThis, isFunction, isObject } from '@vue/shared'
22
import {
33
type Component,
44
type ComponentInternalInstance,
@@ -16,7 +16,9 @@ import {
1616
import type { InjectionKey } from './apiInject'
1717
import type { RawProps } from './componentProps'
1818
import { validateDirectiveName } from './directives'
19+
import { devtoolsInitApp, setDevtoolsHook } from './devtools'
1920

21+
let uid = 0
2022
export function createVaporApp(
2123
rootComponent: Component,
2224
rootProps: RawProps | null = null,
@@ -27,14 +29,24 @@ export function createVaporApp(
2729
rootProps = null
2830
}
2931

32+
const target = getGlobalThis()
33+
target.__VUE__ = true
34+
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
35+
setDevtoolsHook(target.__VUE_DEVTOOLS_GLOBAL_HOOK__, target)
36+
}
37+
3038
const context = createAppContext()
3139
const installedPlugins = new WeakSet()
3240

3341
let instance: ComponentInternalInstance
3442

35-
const app: App = {
36-
_context: context,
43+
const app: App = (context.app = {
44+
_uid: uid++,
45+
_component: rootComponent,
46+
_props: rootProps,
3747
_container: null,
48+
_context: context,
49+
_instance: null,
3850

3951
version,
4052

@@ -122,6 +134,11 @@ export function createVaporApp(
122134
// for devtools and telemetry
123135
;(rootContainer as any).__vue_app__ = app
124136

137+
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
138+
app._instance = instance
139+
devtoolsInitApp(app, version)
140+
}
141+
125142
return instance
126143
} else if (__DEV__) {
127144
warn(
@@ -161,14 +178,15 @@ export function createVaporApp(
161178
currentApp = lastApp
162179
}
163180
},
164-
}
181+
})
165182

166183
return app
167184
}
168185

169186
export function createAppContext(): AppContext {
170187
return {
171188
app: null as any,
189+
mixins: [],
172190
config: {
173191
isNativeTag: NO,
174192
performance: false,
@@ -219,8 +237,13 @@ export interface App {
219237
provide<T>(key: string | InjectionKey<T>, value: T): App
220238
runWithContext<T>(fn: () => T): T
221239

222-
_context: AppContext
240+
// internal, but we need to expose these for the server-renderer and devtools
241+
_uid: number
242+
_component: Component
243+
_props: RawProps | null
223244
_container: ParentNode | null
245+
_context: AppContext
246+
_instance: ComponentInternalInstance | null
224247
}
225248

226249
export interface AppConfig {
@@ -244,6 +267,7 @@ export interface AppConfig {
244267
export interface AppContext {
245268
app: App // for devtools
246269
config: AppConfig
270+
mixins: never[] // for devtools, but no longer supported
247271
provides: Record<string | symbol, any>
248272

249273
/**

packages/runtime-vapor/src/apiRender.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { isArray, isFunction, isObject } from '@vue/shared'
1919
import { fallThroughAttrs } from './componentAttrs'
2020
import { VaporErrorCodes, callWithErrorHandling } from './errorHandling'
2121
import { endMeasure, startMeasure } from './profiling'
22+
import { devtoolsComponentAdded } from './devtools'
2223

2324
export const fragmentKey = Symbol(__DEV__ ? `fragmentKey` : ``)
2425

@@ -38,7 +39,7 @@ export function setupComponent(
3839
}
3940
const reset = setCurrentInstance(instance)
4041
instance.scope.run(() => {
41-
const { component, props } = instance
42+
const { type: component, props } = instance
4243

4344
if (__DEV__) {
4445
if (component.name) {
@@ -140,6 +141,10 @@ function mountComponent(
140141
true,
141142
)
142143

144+
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
145+
devtoolsComponentAdded(instance)
146+
}
147+
143148
if (__DEV__) {
144149
endMeasure(instance, 'mount')
145150
}

packages/runtime-vapor/src/component.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,14 @@ export interface ComponentInternalInstance {
154154
vapor: true
155155
appContext: AppContext
156156

157+
type: Component
157158
block: Block | null
158159
container: ParentNode
159160
parent: ComponentInternalInstance | null
161+
root: ComponentInternalInstance
160162

161163
provides: Data
162164
scope: BlockEffectScope
163-
component: Component
164165
comps: Set<ComponentInternalInstance>
165166

166167
rawProps: NormalizedRawProps
@@ -280,10 +281,11 @@ export function createComponentInstance(
280281
container: null!,
281282

282283
parent,
284+
root: null!, // set later
283285

284286
scope: null!,
285287
provides: parent ? parent.provides : Object.create(_appContext.provides),
286-
component,
288+
type: component,
287289
comps: new Set(),
288290

289291
// resolved props and emits options
@@ -355,6 +357,7 @@ export function createComponentInstance(
355357
*/
356358
// [VaporLifecycleHooks.SERVER_PREFETCH]: null,
357359
}
360+
instance.root = parent ? parent.root : instance
358361
instance.scope = new BlockEffectScope(instance, parent && parent.scope)
359362
initProps(instance, rawProps, !isFunction(component), once)
360363
initSlots(instance, slots)

packages/runtime-vapor/src/componentAttrs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function patchAttrs(instance: ComponentInternalInstance) {
5555

5656
export function withAttrs(props: RawProps): RawProps {
5757
const instance = currentInstance!
58-
if (instance.component.inheritAttrs === false) return props
58+
if (instance.type.inheritAttrs === false) return props
5959
const attrsGetter = () => instance.attrs
6060
if (!props) return [attrsGetter]
6161
if (isArray(props)) {
@@ -67,7 +67,7 @@ export function withAttrs(props: RawProps): RawProps {
6767
export function fallThroughAttrs(instance: ComponentInternalInstance) {
6868
const {
6969
block,
70-
component: { inheritAttrs },
70+
type: { inheritAttrs },
7171
} = instance
7272
if (inheritAttrs === false) return
7373

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function resolveAsset(
4040
) {
4141
const instance = currentInstance
4242
if (instance) {
43-
const Component = instance.component
43+
const Component = instance.type
4444

4545
// explicit self name has highest priority
4646
if (type === COMPONENTS) {

packages/runtime-vapor/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,17 @@ export {
146146
vModelDynamic,
147147
} from './directives/vModel'
148148
export { vShow } from './directives/vShow'
149+
150+
// For devtools
151+
import {
152+
type DevtoolsHook,
153+
devtools as _devtools,
154+
setDevtoolsHook as _setDevtoolsHook,
155+
} from './devtools'
156+
157+
export const devtools = (
158+
__DEV__ || __ESM_BUNDLER__ ? _devtools : undefined
159+
) as DevtoolsHook
160+
export const setDevtoolsHook = (
161+
__DEV__ || __ESM_BUNDLER__ ? _setDevtoolsHook : NOOP
162+
) as typeof _setDevtoolsHook

packages/runtime-vapor/src/profiling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function endMeasure(instance: ComponentInternalInstance, type: string) {
2727
const endTag = startTag + `:end`
2828
perf.mark(endTag)
2929
perf.measure(
30-
`<${formatComponentName(instance, instance.component)}> ${type}`,
30+
`<${formatComponentName(instance, instance.type)}> ${type}`,
3131
startTag,
3232
endTag,
3333
)

packages/runtime-vapor/src/warning.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function warn(msg: string, ...args: any[]) {
3838
trace
3939
.map(
4040
({ instance }) =>
41-
`at <${formatComponentName(instance, instance.component)}>`,
41+
`at <${formatComponentName(instance, instance.type)}>`,
4242
)
4343
.join('\n'),
4444
trace,
@@ -97,11 +97,7 @@ function formatTraceEntry({ instance, recurseCount }: TraceEntry): any[] {
9797
const postfix =
9898
recurseCount > 0 ? `... (${recurseCount} recursive calls)` : ``
9999
const isRoot = instance ? instance.parent == null : false
100-
const open = ` at <${formatComponentName(
101-
instance,
102-
instance.component,
103-
isRoot,
104-
)}`
100+
const open = ` at <${formatComponentName(instance, instance.type, isRoot)}`
105101
const close = `>` + postfix
106102
return instance.rawProps.length
107103
? [open, ...formatProps(instance.rawProps), close]

0 commit comments

Comments
 (0)