Skip to content

Commit

Permalink
feat: inspect external component (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
JianJroh authored Sep 2, 2024
1 parent b3acc19 commit 180a6ca
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/Overlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ const KEY_IGNORE = 'data-v-inspector-ignore'
const KEY_PROPS_DATA = '__v_inspector'
function getData(el) {
return el?.__vnode?.props?.[KEY_PROPS_DATA] ?? el?.getAttribute?.(KEY_DATA)
return el?.__vnode?.props?.[KEY_PROPS_DATA] ?? getComponentData(el) ?? el?.getAttribute?.(KEY_DATA)
}
function getComponentData(el) {
const ctxVNode = el?.__vnode?.ctx?.vnode
if (ctxVNode?.el === el)
return ctxVNode?.props?.[KEY_PROPS_DATA]
}
export default {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/compiler/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function compileSFCTemplate(
nodeTransforms: [
(node) => {
if (node.type === 1) {
if (node.tagType === 0 && !EXCLUDE_TAG.includes(node.tag)) {
if ((node.tagType === 0 || node.tagType === 1) && !EXCLUDE_TAG.includes(node.tag)) {
if (node.loc.source.includes(KEY_DATA))
return

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
const fn = new Set<string>()
const s = new MagicString(code)

s.replace(/(createElementVNode|createVNode|createElementBlock) as _\1,?/g, (_, name) => {
s.replace(/(createElementVNode|createVNode|createElementBlock|createBlock) as _\1,?/g, (_, name) => {
fn.add(name)
return ''
})
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/vue3/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<script lang="ts">
import Hi from './Hi.vue'
import Welcome from './Welcome'
import ExternalComp from './ExternalComp.vue'
export default {
name: 'App',
components: {
Hi,
Welcome,
ExternalComp,
},
}
</script>
Expand All @@ -15,6 +18,7 @@ export default {
<div>
<Hi />
<Welcome />
<ExternalComp />
<!-- -->
<!-- -->
<!-- -->
Expand Down
36 changes: 36 additions & 0 deletions packages/playground/vue3/src/ExternalComp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!-- eslint-disable vue/one-component-per-file -->
<script lang="ts" setup>
import { defineComponent, h } from 'vue'

// Mock external import like this: import { Card, Title,Content } from '@ui-lib'
const Card = defineComponent({
render() {
return h('div', {
style: 'border: 1px solid #eee;padding: 32px',
}, this.$slots)
},
})

const Title = defineComponent({
render() {
return h('div', {
style: 'color: darkolivegreen',
}, this.$slots)
},
})

const Content = defineComponent({
render() {
return h('p', {
style: 'color: darkseagreen',
}, this.$slots)
},
})
</script>

<template>
<Card>
<Title>External component</Title>
<Content>content</Content>
</Card>
</template>

0 comments on commit 180a6ca

Please sign in to comment.