Skip to content

Commit 180a6ca

Browse files
authored
feat: inspect external component (#91)
1 parent b3acc19 commit 180a6ca

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

packages/core/src/Overlay.vue

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ const KEY_IGNORE = 'data-v-inspector-ignore'
88
const KEY_PROPS_DATA = '__v_inspector'
99
1010
function getData(el) {
11-
return el?.__vnode?.props?.[KEY_PROPS_DATA] ?? el?.getAttribute?.(KEY_DATA)
11+
return el?.__vnode?.props?.[KEY_PROPS_DATA] ?? getComponentData(el) ?? el?.getAttribute?.(KEY_DATA)
12+
}
13+
14+
function getComponentData(el) {
15+
const ctxVNode = el?.__vnode?.ctx?.vnode
16+
if (ctxVNode?.el === el)
17+
return ctxVNode?.props?.[KEY_PROPS_DATA]
1218
}
1319
1420
export default {

packages/core/src/compiler/template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function compileSFCTemplate(
3030
nodeTransforms: [
3131
(node) => {
3232
if (node.type === 1) {
33-
if (node.tagType === 0 && !EXCLUDE_TAG.includes(node.tag)) {
33+
if ((node.tagType === 0 || node.tagType === 1) && !EXCLUDE_TAG.includes(node.tag)) {
3434
if (node.loc.source.includes(KEY_DATA))
3535
return
3636

packages/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
259259
const fn = new Set<string>()
260260
const s = new MagicString(code)
261261

262-
s.replace(/(createElementVNode|createVNode|createElementBlock) as _\1,?/g, (_, name) => {
262+
s.replace(/(createElementVNode|createVNode|createElementBlock|createBlock) as _\1,?/g, (_, name) => {
263263
fn.add(name)
264264
return ''
265265
})

packages/playground/vue3/src/App.vue

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<script lang="ts">
22
import Hi from './Hi.vue'
33
import Welcome from './Welcome'
4+
import ExternalComp from './ExternalComp.vue'
5+
46
export default {
57
name: 'App',
68
components: {
79
Hi,
810
Welcome,
11+
ExternalComp,
912
},
1013
}
1114
</script>
@@ -15,6 +18,7 @@ export default {
1518
<div>
1619
<Hi />
1720
<Welcome />
21+
<ExternalComp />
1822
<!-- -->
1923
<!-- -->
2024
<!-- -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!-- eslint-disable vue/one-component-per-file -->
2+
<script lang="ts" setup>
3+
import { defineComponent, h } from 'vue'
4+
5+
// Mock external import like this: import { Card, Title,Content } from '@ui-lib'
6+
const Card = defineComponent({
7+
render() {
8+
return h('div', {
9+
style: 'border: 1px solid #eee;padding: 32px',
10+
}, this.$slots)
11+
},
12+
})
13+
14+
const Title = defineComponent({
15+
render() {
16+
return h('div', {
17+
style: 'color: darkolivegreen',
18+
}, this.$slots)
19+
},
20+
})
21+
22+
const Content = defineComponent({
23+
render() {
24+
return h('p', {
25+
style: 'color: darkseagreen',
26+
}, this.$slots)
27+
},
28+
})
29+
</script>
30+
31+
<template>
32+
<Card>
33+
<Title>External component</Title>
34+
<Content>content</Content>
35+
</Card>
36+
</template>

0 commit comments

Comments
 (0)