Skip to content

Commit 1e2f530

Browse files
committed
bug fixes
1 parent 5cfedd1 commit 1e2f530

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-trace",
3-
"version": "0.1.5-beta.2",
3+
"version": "0.1.6-beta",
44
"description": "svelte-trace is a Svelte 5 preprocessor that enables 'click-to-open VS Code' functionality by adding metadata to HTML elements.",
55
"type": "module",
66
"main": "./dist/index.js",
@@ -12,7 +12,10 @@
1212
}
1313
},
1414
"files": [
15-
"dist"
15+
"dist",
16+
"README.md",
17+
"LICENSE",
18+
"package.json"
1619
],
1720
"keywords": [
1821
"svelte-trace",
@@ -55,7 +58,8 @@
5558
"clean": "rm -rf dist",
5659
"build:ts": "tsc -p tsconfig.build.json",
5760
"build:assets": "cp -r src/assets dist/",
58-
"build": "npm run clean && npm run build:ts && npm run build:assets",
61+
"build:docs": "cp README.md LICENSE package.json dist/",
62+
"build": "npm run clean && npm run build:ts && npm run build:assets && npm run build:docs",
5963
"test": "echo 'Tests coming soon'",
6064
"prepublishOnly": "npm run build"
6165
}

src/processors/core_processor.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
2323
*/
2424
const addDataAttribute = (node: AST.RegularElement): void => {
2525
if (node.type !== "RegularElement") return;
26-
if (node.name === "script" || node.name === "style") return;
26+
if (
27+
node.name === "script" ||
28+
node.name === "style" ||
29+
node.name === "meta" ||
30+
node.name === "link" ||
31+
node.name === "title"
32+
)
33+
return;
2734

2835
const classAttr = node.attributes.find(
2936
(attr): attr is AST.Attribute => attr.type === "Attribute" && attr.name === "class"
@@ -39,9 +46,21 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
3946
const encodedBase64Data = Buffer.from(dataAttrValue, "utf8").toString("base64");
4047
const dataAttribute = `data-svelte-trace="${encodedBase64Data}"`;
4148

42-
// Always insert before the closing '>' to make it the last attribute
43-
const tagEnd = content.indexOf(">", node.start);
44-
magicString.appendLeft(tagEnd, ` ${dataAttribute}`);
49+
// Find the correct insertion point based on whether it's self-closing or not
50+
let insertionPoint: number;
51+
52+
// If there are attributes, insert after the last attribute
53+
if (node.attributes.length > 0) {
54+
const lastAttr = node.attributes[node.attributes.length - 1];
55+
insertionPoint = lastAttr.end;
56+
}
57+
// If no attributes, insert after the tag name. Included +1 for '<'
58+
else {
59+
const tagNameEnd = node.start + 1 + node.name.length;
60+
insertionPoint = tagNameEnd;
61+
}
62+
63+
magicString.appendLeft(insertionPoint, ` ${dataAttribute}`);
4564
};
4665

4766
/**
@@ -52,6 +71,7 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
5271
addDataAttribute(node);
5372
}
5473

74+
// Handle different node structures
5575
if (Array.isArray(node.children)) {
5676
node.children.forEach(walk);
5777
}

0 commit comments

Comments
 (0)