Skip to content

Commit feef223

Browse files
committed
fix(vite): windows path handling
1 parent 58f5ba9 commit feef223

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/vite/hmr/server/vite-plugin.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Plugin, ResolvedConfig } from 'vite';
22
import { createRequire } from 'node:module';
3+
import path from 'path';
34
const require = createRequire(import.meta.url);
45

56
const VIRTUAL_ID = 'virtual:ns-hmr-client';
@@ -20,7 +21,23 @@ export function nsHmrClientVitePlugin(opts: { platform: string; verbose?: boolea
2021
load(id) {
2122
if (id !== RESOLVED_ID) return null;
2223

23-
const clientPath = require.resolve('@nativescript/vite/hmr/client/index.js');
24+
/**
25+
* Use a POSIX-style import specifier for the client entry to avoid
26+
* Windows drive-letter paths (e.g. "D:\\...") accidentally
27+
* becoming bare import ids that Rollup/Vite cannot resolve.
28+
* We still resolve the real filesystem path for correctness, but convert it to a project-relative POSIX path before interpolating it into the generated module.
29+
**/
30+
const clientFsPath = require.resolve('@nativescript/vite/hmr/client/index.js');
31+
// Prefer project root when available; otherwise fall back to cwd.
32+
const projectRoot = config?.root || process.cwd();
33+
let clientImport = clientFsPath;
34+
try {
35+
const rel = path.relative(projectRoot, clientFsPath).replace(/\\/g, '/');
36+
clientImport = (rel.startsWith('.') ? rel : `/${rel}`).replace(/\/+/g, '/');
37+
} catch {
38+
// On any failure, keep the original path but normalize to POSIX
39+
clientImport = clientFsPath.replace(/\\/g, '/');
40+
}
2441

2542
// Build ws url from Vite server info
2643
let host = process.env.NS_HMR_HOST || (config?.server?.host as any);
@@ -38,7 +55,7 @@ export function nsHmrClientVitePlugin(opts: { platform: string; verbose?: boolea
3855
// Import client and start it with explicit ws URL
3956
const banner = opts.verbose ? `console.log('[ns-hmr-client] starting client -> ${wsUrl} (HTTP loader enabled via __NS_HTTP_ORIGIN__)');` : '';
4057
return `
41-
import startViteHMR from "${clientPath}";
58+
import startViteHMR from "${clientImport}";
4259
${banner}
4360
startViteHMR({ wsUrl: ${JSON.stringify(wsUrl)} });
4461
`;

0 commit comments

Comments
 (0)