Skip to content

Commit 1261bb5

Browse files
committed
feat: support inspect component
1 parent b1f50ab commit 1261bb5

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ interface VitePluginInspectorOptions {
9797
*/
9898
vue?: 2 | 3
9999

100+
/**
101+
* Inspect with vue component
102+
* @default true
103+
*/
104+
withComponent?: boolean
105+
100106
/**
101107
* Default enable state
102108
* @default false

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"files": [
2020
"dist",
2121
"src/load.js",
22+
"src/console.js",
2223
"src/Overlay.vue"
2324
],
2425
"exports": {

src/compiler/template.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ interface CompileSFCTemplateOptions {
1313
code: string
1414
id: string
1515
type: "template" | "jsx"
16+
withComponent: boolean
1617
}
1718
export async function compileSFCTemplate(
18-
{ code, id, type }: CompileSFCTemplateOptions,
19+
{ code, id, type, withComponent }: CompileSFCTemplateOptions,
1920
) {
2021
const s = new MagicString(code)
2122
const { base } = path.parse(id)
@@ -28,7 +29,7 @@ export async function compileSFCTemplate(
2829
nodeTransforms: [
2930
(node) => {
3031
if (node.type === 1) {
31-
if (node.tagType === 0 && !EXCLUDE_TAG.includes(node.tag)) {
32+
if ((withComponent ? node.tagType < 2 : node.tagType === 0) && !EXCLUDE_TAG.includes(node.tag)) {
3233
if (node.loc.source.includes("data-v-inspector-file")) return
3334

3435
const insertPosition = node.loc.start.offset + node.tag.length + 1

src/console.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const warn = console.warn
2+
console.warn = function (...data) {
3+
const skipMsgPrefix = "[Vue warn]: Extraneous non-props attributes "
4+
let msg = data[0] || ""
5+
if (msg.startsWith(skipMsgPrefix)) {
6+
msg = msg.replace(/\([^)]+\)/, function (match) {
7+
const attributes = match
8+
.slice(1, -1)
9+
.split(', ')
10+
.filter(function (item) {
11+
return !/^data-v-inspector-(file|line|column|title)$/.test(item)
12+
})
13+
.join(', ')
14+
return '(' + attributes + ')'
15+
})
16+
if (msg.slice(skipMsgPrefix.length).startsWith('()')) return
17+
if (msg) data[0] = msg
18+
}
19+
warn(...data)
20+
}

src/index.ts

+34-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export interface VitePluginInspectorOptions {
2020
*/
2121
vue?: 2 | 3
2222

23+
/**
24+
* Inspect with vue component
25+
* @default true
26+
*/
27+
withComponent?: boolean
28+
2329
/**
2430
* Default enable state
2531
* @default false
@@ -60,6 +66,7 @@ export interface VitePluginInspectorOptions {
6066

6167
const DEFAULT_INSPECTOR_OPTIONS: VitePluginInspectorOptions = {
6268
vue: 3,
69+
withComponent: true,
6370
enabled: false,
6471
toggleComboKey: process.platform === "win32" ? "control-shift" : "meta-shift",
6572
toggleButtonVisibility: "active",
@@ -104,10 +111,15 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
104111
const isTpl = filename.endsWith(".vue") && query.type !== "style"
105112

106113
if (isJsx || isTpl)
107-
return compileSFCTemplate({ code, id: filename, type: isJsx ? "jsx" : "template" })
114+
return compileSFCTemplate({ code, id: filename, type: isJsx ? "jsx" : "template", withComponent: normalizedOptions.withComponent })
108115

109-
if (normalizedOptions.appendTo && filename.endsWith(normalizedOptions.appendTo))
110-
return { code: `${code}\nimport 'virtual:vue-inspector-path:load.js'` }
116+
if (normalizedOptions.appendTo && filename.endsWith(normalizedOptions.appendTo)) {
117+
const libs = ["import 'virtual:vue-inspector-path:load.js'"]
118+
if (normalizedOptions.withComponent) {
119+
libs.push("import 'virtual:vue-inspector-path:console.js'")
120+
}
121+
return `${code}\n${libs.join('\n')}`
122+
}
111123

112124
return code
113125
},
@@ -124,19 +136,27 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
124136
transformIndexHtml(html) {
125137
if (normalizedOptions.appendTo)
126138
return
127-
return {
128-
html,
129-
tags: [
130-
{
131-
tag: "script",
132-
injectTo: "body",
133-
attrs: {
134-
type: "module",
135-
src: "/@id/virtual:vue-inspector-path:load.js",
136-
},
139+
const tags = [
140+
{
141+
tag: "script",
142+
injectTo: "body" as const,
143+
attrs: {
144+
type: "module",
145+
src: "/@id/virtual:vue-inspector-path:load.js",
146+
},
147+
},
148+
]
149+
if (normalizedOptions.withComponent) {
150+
tags.push({
151+
tag: "script",
152+
injectTo: "body",
153+
attrs: {
154+
type: "module",
155+
src: "/@id/virtual:vue-inspector-path:console.js",
137156
},
138-
],
157+
})
139158
}
159+
return { html, tags }
140160
},
141161
}
142162
}

0 commit comments

Comments
 (0)