Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 2x 9x 6x 3x 3x 3x 14x 14x 14x 14x 16x 31x 15x 3x | /**
* Path normalization utilities for Vite plugin hooks.
* Centralizes logic for importer/module id resolution (file:, /, absolute).
*/
import * as path from 'path';
import * as fs from 'fs';
import { fileURLToPath } from 'url';
const EXTENSIONS = ['.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs'];
/**
* Converts an importer string (from resolveId) to an absolute filesystem path.
* Handles file: URLs, leading-slash paths (virtual), and absolute paths.
*/
export function normalizeImporterPath(importer: string, root: string): string {
if (importer.startsWith('file:')) {
return fileURLToPath(importer);
}
Eif (importer.startsWith('/')) {
const virtualPath = path.join(root, importer.replace(/^\//, ''));
return fs.existsSync(virtualPath) ? virtualPath : path.resolve(importer);
}
return path.resolve(importer);
}
/**
* Converts a module id (from transform/resolveId) to an absolute filesystem path.
* Handles file: URLs and leading-slash paths (virtual).
*/
export function normalizeModuleIdPath(id: string, root: string): string {
const idPath = id.startsWith('file:') ? fileURLToPath(id) : id;
Eif (idPath.startsWith('/')) {
const virtualPath = path.join(root, idPath.replace(/^\//, ''));
return fs.existsSync(virtualPath) ? virtualPath : path.resolve(idPath);
}
return path.resolve(idPath);
}
/**
* Resolves a path to an existing file, trying extensions if the path has none.
* Returns the resolved absolute path or null if not found.
*/
export function resolveWithExtension(filePath: string): string | null {
if (fs.existsSync(filePath)) return filePath;
const ext = EXTENSIONS.find((e) => fs.existsSync(filePath + e));
if (ext) return filePath + ext;
return null;
}
|