Skip to content

Commit e6c280c

Browse files
author
LessJS CI
committed
fix(ssg): guard WORKSPACE_ROOT in build-client.ts for JSR consumers
Module-level fileURLToPath(import.meta.url) crashes when loaded from JSR (import.meta.url is https://..., not file://). Guard with startsWith('file:') check — same pattern used in build-ssg.ts's getLocalLessjsPackageRoot. Null-guard the later WORKSPACE_ROOT usage in lookupInDenoJson.
1 parent f2b7829 commit e6c280c

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

packages/adapter-vite/src/cli/build-client.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ const log = createLogger('ssg');
2626
const VIRTUAL_CLIENT_ENTRY_ID = 'virtual:less-client-entry';
2727
const RESOLVED_CLIENT_ENTRY_ID = '\0' + VIRTUAL_CLIENT_ENTRY_ID;
2828

29-
/** Workspace root derived from this module's location (packages/adapter-vite/src/cli/). */
30-
const WORKSPACE_ROOT = fileURLToPath(new URL('../../../..', import.meta.url)).replace(
31-
/\\/g,
32-
'/',
33-
);
29+
/** Workspace root derived from this module's location (packages/adapter-vite/src/cli/).
30+
* Only valid in local workspace (file:// import.meta.url). In JSR consumers, returns null. */
31+
const WORKSPACE_ROOT: string | null = (() => {
32+
if (!import.meta.url.startsWith('file:')) return null;
33+
try {
34+
return fileURLToPath(new URL('../../../..', import.meta.url)).replace(/\\/g, '/');
35+
} catch {
36+
return null;
37+
}
38+
})();
3439

3540
/**
3641
* Look up a bare specifier in a deno.json import map.
@@ -55,7 +60,7 @@ function lookupInDenoJson(
5560
}
5661

5762
// Also try workspace root (module-relative, for monorepo dev / testing)
58-
if (!denoJsonDirs.has(WORKSPACE_ROOT)) {
63+
if (WORKSPACE_ROOT && !denoJsonDirs.has(WORKSPACE_ROOT)) {
5964
const found = tryDenoJsonDir(id, WORKSPACE_ROOT);
6065
if (found) return found;
6166
}

0 commit comments

Comments
 (0)