Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support inspect component #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ interface VitePluginInspectorOptions {
*/
vue?: 2 | 3

/**
* Inspect with vue component
* @default true
*/
withComponent?: boolean

/**
* Default enable state
* @default false
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"files": [
"dist",
"src/load.js",
"src/console.js",
"src/Overlay.vue"
],
"exports": {
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ interface CompileSFCTemplateOptions {
code: string
id: string
type: "template" | "jsx"
withComponent: boolean
}
export async function compileSFCTemplate(
{ code, id, type }: CompileSFCTemplateOptions,
{ code, id, type, withComponent }: CompileSFCTemplateOptions,
) {
const s = new MagicString(code)
const { base } = path.parse(id)
Expand All @@ -28,7 +29,7 @@ export async function compileSFCTemplate(
nodeTransforms: [
(node) => {
if (node.type === 1) {
if (node.tagType === 0 && !EXCLUDE_TAG.includes(node.tag)) {
if ((withComponent ? node.tagType < 2 : node.tagType === 0) && !EXCLUDE_TAG.includes(node.tag)) {
if (node.loc.source.includes("data-v-inspector-file")) return

const insertPosition = node.loc.start.offset + node.tag.length + 1
Expand Down
20 changes: 20 additions & 0 deletions src/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const warn = console.warn
console.warn = function (...data) {
const skipMsgPrefix = "[Vue warn]: Extraneous non-props attributes "
let msg = data[0] || ""
if (msg.startsWith(skipMsgPrefix)) {
msg = msg.replace(/\([^)]+\)/, function (match) {
const attributes = match
.slice(1, -1)
.split(', ')
.filter(function (item) {
return !/^data-v-inspector-(file|line|column|title)$/.test(item)
})
.join(', ')
return '(' + attributes + ')'
})
if (msg.slice(skipMsgPrefix.length).startsWith('()')) return
if (msg) data[0] = msg
}
warn(...data)
}
48 changes: 34 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export interface VitePluginInspectorOptions {
*/
vue?: 2 | 3

/**
* Inspect with vue component
* @default true
*/
withComponent?: boolean

/**
* Default enable state
* @default false
Expand Down Expand Up @@ -60,6 +66,7 @@ export interface VitePluginInspectorOptions {

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

if (isJsx || isTpl)
return compileSFCTemplate({ code, id: filename, type: isJsx ? "jsx" : "template" })
return compileSFCTemplate({ code, id: filename, type: isJsx ? "jsx" : "template", withComponent: normalizedOptions.withComponent })

if (normalizedOptions.appendTo && filename.endsWith(normalizedOptions.appendTo))
return { code: `${code}\nimport 'virtual:vue-inspector-path:load.js'` }
if (normalizedOptions.appendTo && filename.endsWith(normalizedOptions.appendTo)) {
const libs = ["import 'virtual:vue-inspector-path:load.js'"]
if (normalizedOptions.withComponent) {
libs.push("import 'virtual:vue-inspector-path:console.js'")
}
return `${code}\n${libs.join('\n')}`
}

return code
},
Expand All @@ -124,19 +136,27 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
transformIndexHtml(html) {
if (normalizedOptions.appendTo)
return
return {
html,
tags: [
{
tag: "script",
injectTo: "body",
attrs: {
type: "module",
src: "/@id/virtual:vue-inspector-path:load.js",
},
const tags = [
{
tag: "script",
injectTo: "body" as const,
attrs: {
type: "module",
src: "/@id/virtual:vue-inspector-path:load.js",
},
},
]
if (normalizedOptions.withComponent) {
tags.push({
tag: "script",
injectTo: "body",
attrs: {
type: "module",
src: "/@id/virtual:vue-inspector-path:console.js",
},
],
})
}
return { html, tags }
},
}
}
Expand Down