Skip to content

Commit 8400f7a

Browse files
committed
fix: sourcemaps
1 parent e88f2d5 commit 8400f7a

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

.changeset/crazy-jars-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@marko/vite": patch
3+
---
4+
5+
Fix sourcemap generation.

src/index.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,21 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
128128
const resolveVirtualDependency: compiler.Config["resolveVirtualDependency"] =
129129
(from, dep) => {
130130
const normalizedFrom = normalizePath(from);
131-
const query = `${virtualFileQuery}&id=${encodeURIComponent(dep.virtualPath)}`;
131+
const query = `${virtualFileQuery}&id=${dep.virtualPath.replace(/^\.\//, "").replace(/[^a-z0-9_.-]+/gi, "_")}`;
132132
const id = normalizedFrom + query;
133+
const virtualFile = {
134+
code: dep.code,
135+
map: stripSourceRoot(dep.map),
136+
};
133137

134138
if (devServer) {
135139
const prev = virtualFiles.get(id);
136140
if (isDeferredPromise(prev)) {
137-
prev.resolve(dep);
141+
prev.resolve(virtualFile);
138142
}
139143
}
140144

141-
virtualFiles.set(id, dep);
145+
virtualFiles.set(id, virtualFile);
142146
return `./${path.posix.basename(normalizedFrom) + query}`;
143147
};
144148

@@ -840,7 +844,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
840844

841845
return {
842846
code,
843-
map: toSourceMap(map),
847+
map: stripSourceRoot(map),
844848
meta: { arcSourceCode: source, arcScanIds: meta.analyzedTags },
845849
};
846850
}
@@ -896,7 +900,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
896900
}
897901
return {
898902
code,
899-
map: toSourceMap(compiled.map),
903+
map: stripSourceRoot(compiled.map),
900904
meta: isBuild
901905
? { arcSourceCode: source, arcScanIds: meta.analyzedTags }
902906
: undefined,
@@ -1170,17 +1174,12 @@ function getKnownTemplates(cwd: string) {
11701174
return knownTemplates;
11711175
}
11721176

1173-
/**
1174-
* Vitest does not properly handle sourcemaps with a sourceRoot and sources property (like Marko's).
1175-
* For most tooling we can pass Marko's sourcemaps right on through, however all that's really needed is
1176-
* the mappings property, so we are just passing that to side step the Vitest issue.
1177-
* https://github.com/vitest-dev/vitest/blob/c84a396431062dd0f445270b3f331d57714b4cd0/packages/coverage-v8/src/provider.ts#L358
1178-
*/
1179-
function toSourceMap(map: any) {
1180-
return map
1181-
? {
1182-
names: map.names,
1183-
mappings: map.mappings,
1184-
}
1185-
: null;
1177+
function stripSourceRoot(map: any) {
1178+
if (map && map.sourceRoot) {
1179+
// Always strip the sourceRoot path since otherwise file system information
1180+
// is sent to the browser.
1181+
// Without `sourceRoot` everything implicitly becomes relative to the project root.
1182+
map.sourceRoot = undefined;
1183+
}
1184+
return map;
11861185
}

0 commit comments

Comments
 (0)