Skip to content

Commit

Permalink
fix: resolved URL construction on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
cyco130 committed Feb 27, 2023
1 parent acccc2d commit 795bc3f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/node-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { fileURLToPath, pathToFileURL } from "node:url";
import {
fileURLToPath,
pathToFileURL as originalPathToFileURL,
} from "node:url";
import { ViteDevServer } from "vite";

declare global {
Expand Down Expand Up @@ -89,6 +92,14 @@ function unwrapSpecifier(
const viteUrlMap = new WeakMap<ViteDevServer, Set<string>>();
let projectRoot = "/";

function pathToFileURL(path: string): string {
const qmarkPos = path.indexOf("?");
const base = qmarkPos === -1 ? path : path.slice(0, qmarkPos);
const search = qmarkPos === -1 ? "" : path.slice(qmarkPos);

return originalPathToFileURL(base).href + search;
}

export async function resolve(
specifier: string,
context: NodeResolveContext,
Expand Down Expand Up @@ -121,9 +132,9 @@ export async function resolve(
const id = resolved[1];

let url =
(id.startsWith("/@id/") || id[0] === "\0" ? "vite:" : "file://") +
id +
timestamp(id);
(id.startsWith("/@id/") || id[0] === "\0"
? "vite:" + id
: pathToFileURL(id)) + timestamp(id);

url = url.replace(/\0/g, "__x00__");

Expand Down Expand Up @@ -161,7 +172,8 @@ export async function resolve(
if (resolved && !resolved.external) {
const id = resolved.id.replace(/\0/g, "__x00__");
const url =
(id.startsWith("/@id/") ? "vite:" : "file://") + id + timestamp(id);
(id.startsWith("/@id/") ? "vite:" + id : pathToFileURL(id)) +
timestamp(id);
map?.add(url);

return {
Expand All @@ -184,7 +196,7 @@ export async function resolve(
}
const nextContext = {
...context,
parentURL: pathToFileURL(parentFile).href,
parentURL: pathToFileURL(parentFile),
};

return await nextResolve(specifier, nextContext);
Expand Down

0 comments on commit 795bc3f

Please sign in to comment.