Skip to content

Commit a10c88e

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

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const warn = console.warn
2+
console.warn = function(...data) {
3+
const msg = data[0] || ""
4+
if (msg.startsWith("[Vue warn]: Extraneous non-props attributes (data-v-inspector-file, data-v-inspector-line, data-v-inspector-column, data-v-inspector-title)")) return
5+
warn(...data)
6+
}

src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from "path"
22
import { fileURLToPath } from "url"
33
import fs from "fs"
44
import { yellow, red } from "kolorist"
5-
import { normalizePath } from "vite"
5+
import { IndexHtmlTransformResult, normalizePath } from "vite"
66
import type { PluginOption } from "vite"
77
import { compileSFCTemplate } from "./compiler"
88
import { parseVueRequest } from "./utils"
@@ -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,7 +111,7 @@ 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

109116
if (normalizedOptions.appendTo && filename.endsWith(normalizedOptions.appendTo))
110117
return { code: `${code}\nimport 'virtual:vue-inspector-path:load.js'` }
@@ -124,7 +131,7 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
124131
transformIndexHtml(html) {
125132
if (normalizedOptions.appendTo)
126133
return
127-
return {
134+
const result: IndexHtmlTransformResult = {
128135
html,
129136
tags: [
130137
{
@@ -137,6 +144,17 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
137144
},
138145
],
139146
}
147+
if (normalizedOptions.withComponent) {
148+
result.tags.push({
149+
tag: "script",
150+
injectTo: "body",
151+
attrs: {
152+
type: "module",
153+
src: "/@id/virtual:vue-inspector-path:console.js",
154+
},
155+
})
156+
}
157+
return result
140158
},
141159
}
142160
}

0 commit comments

Comments
 (0)