Files
openclaw/src/plugins/import-specifier.ts
T
2026-04-27 08:35:45 +01:00

23 lines
713 B
TypeScript

import path from "node:path";
/**
* On Windows, Node's ESM loader requires absolute paths to be expressed as
* file:// URLs. Raw drive-letter paths like C:\... are parsed as URL schemes.
*/
export function toSafeImportPath(specifier: string): string {
if (process.platform !== "win32") {
return specifier;
}
if (specifier.startsWith("file://")) {
return specifier;
}
if (path.win32.isAbsolute(specifier)) {
const normalizedSpecifier = specifier.replaceAll("\\", "/");
if (normalizedSpecifier.startsWith("//")) {
return new URL(`file:${encodeURI(normalizedSpecifier)}`).href;
}
return new URL(`file:///${encodeURI(normalizedSpecifier)}`).href;
}
return specifier;
}