Skip to content

Commit d35902b

Browse files
fix(webapp): preserve Vite source maps for library files without @/ imports
The apollon-alias-resolver load hook rewrites the library's `@/` imports (which collide with the webapp's own `@/`). It previously intercepted every file under library/ and returned `map: null`, discarding source maps for all of them. It now only intercepts files that actually rewrite a `@/…` import; every other library file falls through to Vite's native loader and keeps its source map (and Fast Refresh). The rewrite logic is unchanged. Verified: webapp build resolves `@/` (0 raw `@/` left, rewritten to absolute paths), and a non-`@/` library component is served with both a source map and React Fast Refresh. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 39c55d0 commit d35902b

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

standalone/webapp/vite.config.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,36 @@ const apollonAliases = [
2424
]
2525

2626
// The editor library uses a `@/` alias -> library/lib, which collides with the
27-
// webapp's own `@/` -> src. Rewrite `@/…` to an absolute library/lib path only
28-
// inside files under the library directory; the webapp's `@/` keeps mapping to src.
27+
// webapp's own `@/` -> src. Rewrite `@/…` imports in library files to an absolute
28+
// library/lib path (the webapp's `@/` keeps mapping to src). Only files that
29+
// actually import `@/…` are intercepted, so every other library file keeps Vite's
30+
// native source maps; the rewrite is line-preserving, so only the import lines of
31+
// the rewritten files shift.
2932
const apollonAliasResolver = {
3033
name: "apollon-alias-resolver",
3134
enforce: "pre" as const,
3235
async load(id: string) {
3336
const libraryRoot = resolve(__dirname, "../../library").replace(/\\/g, "/")
3437
if (!id.replace(/\\/g, "/").includes(libraryRoot)) return null
38+
let original: string
3539
try {
36-
let code = await fs.promises.readFile(id, "utf-8")
37-
const libRoot = resolve(__dirname, "../../library/lib").replace(
38-
/\\/g,
39-
"/"
40-
)
41-
code = code.replace(
40+
original = await fs.promises.readFile(id, "utf-8")
41+
} catch {
42+
return null
43+
}
44+
if (!original.includes("@/")) return null
45+
const libRoot = resolve(__dirname, "../../library/lib").replace(/\\/g, "/")
46+
const code = original
47+
.replace(
4248
/from\s+["']@\/([^"']+)["']/g,
4349
(_m, p) => `from "${resolve(libRoot, p).replace(/\\/g, "/")}"`
4450
)
45-
code = code.replace(
51+
.replace(
4652
/import\s+["']@\/([^"']+)["']/g,
4753
(_m, p) => `import "${resolve(libRoot, p).replace(/\\/g, "/")}"`
4854
)
49-
return { code, map: null }
50-
} catch {
51-
return null
52-
}
55+
if (code === original) return null
56+
return { code, map: null }
5357
},
5458
}
5559

0 commit comments

Comments
 (0)