Skip to content

Commit 627f8fe

Browse files
authored
feat: append attribute at the end (#44)
1 parent 58b3bbe commit 627f8fe

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

packages/core/src/Overlay.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const host = hostOpts && hostOpts !== true ? hostOpts : importMetaUrl?.hostname
88
const port = inspectorOptions.serverOptions?.port ?? importMetaUrl?.port
99
const baseUrl = isClient ? `${protocol}//${host}:${port}` : ''
1010
11-
const KEY_DATA = 'data-v-inspector-options'
11+
const KEY_DATA = 'data-v-inspector'
1212
const KEY_IGNORE = 'data-v-inspector-ignore'
1313
1414
export default {

packages/core/src/compiler/template.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import importMeta from '@babel/plugin-syntax-import-meta'
88
import { parseJSXIdentifier } from '../utils'
99

1010
const EXCLUDE_TAG = ['template', 'script', 'style']
11+
const KEY_DATA = 'data-v-inspector'
12+
1113
interface CompileSFCTemplateOptions {
1214
code: string
1315
id: string
@@ -27,17 +29,18 @@ export async function compileSFCTemplate(
2729
(node) => {
2830
if (node.type === 1) {
2931
if (node.tagType === 0 && !EXCLUDE_TAG.includes(node.tag)) {
30-
if (node.loc.source.includes('data-v-inspector-options'))
32+
if (node.loc.source.includes(KEY_DATA))
3133
return
3234

33-
const insertPosition = node.loc.start.offset + node.tag.length + 1
35+
const insertPosition = node.props.length ? Math.max(...node.props.map(i => i.loc.end.offset)) : node.loc.start.offset + node.tag.length + 1
3436
const { line, column } = node.loc.start
3537

36-
const content = ` data-v-inspector-options="${relativePath}:${line}:${column}"`
38+
const content = ` ${KEY_DATA}="${relativePath}:${line}:${column}"`
3739

3840
s.prependLeft(
3941
insertPosition,
40-
content)
42+
content,
43+
)
4144
}
4245
}
4346
},
@@ -64,14 +67,14 @@ export async function compileSFCTemplate(
6467
babelTraverse(ast, {
6568
enter({ node }) {
6669
if (node.type === 'JSXElement') {
67-
if (node.openingElement.attributes.some(attr => attr.type !== 'JSXSpreadAttribute' && attr.name.name === 'data-v-inspector-options',
70+
if (node.openingElement.attributes.some(attr => attr.type !== 'JSXSpreadAttribute' && attr.name.name === KEY_DATA,
6871
))
6972
return
7073

71-
const insertPosition = node.start + parseJSXIdentifier(node.openingElement.name as any).length + 1
74+
const insertPosition = node.openingElement.end - 1
7275
const { line, column } = node.loc.start
7376

74-
const content = ` data-v-inspector-options="${relativePath}:${line}:${column}"`
77+
const content = ` ${KEY_DATA}="${relativePath}:${line}:${column}"`
7578

7679
s.prependLeft(
7780
insertPosition,

0 commit comments

Comments
 (0)