astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
54 lines (53 loc) • 2.06 kB
JavaScript
import path from "node:path";
import { fileURLToPath } from "node:url";
import { prependForwardSlash, slash } from "../core/path.js";
import { unwrapId, VALID_ID_PREFIX, viteID } from "./util.js";
export { resolvePath } from "@astrojs/internal-helpers/mdx";
const isWindows = typeof process !== "undefined" && process.platform === "win32";
/**
* Re-implementation of Vite's normalizePath that can be used without Vite
*/
export function normalizePath(id) {
return path.posix.normalize(isWindows ? slash(id) : id);
}
export function rootRelativePath(root, idOrUrl, shouldPrependForwardSlash = true) {
let id;
if (typeof idOrUrl !== "string") {
id = unwrapId(viteID(idOrUrl));
} else {
id = idOrUrl;
}
const normalizedRoot = normalizePath(fileURLToPath(root));
if (id.startsWith(normalizedRoot)) {
id = id.slice(normalizedRoot.length);
}
return shouldPrependForwardSlash ? prependForwardSlash(id) : id;
}
/**
* Simulate Vite's resolve and import analysis so we can import the id as an URL
* through a script tag or a dynamic import as-is.
*/
// NOTE: `/@id/` should only be used when the id is fully resolved
export async function resolveIdToUrl(loader, id, root) {
let resultId = await loader.resolveId(id, undefined);
// Try resolve jsx to tsx
if (!resultId && id.endsWith(".jsx")) {
resultId = await loader.resolveId(id.slice(0, -4), undefined);
}
if (!resultId) {
return VALID_ID_PREFIX + id;
}
if (path.isAbsolute(resultId)) {
const normalizedRoot = root && normalizePath(fileURLToPath(root));
// Convert to root-relative path if path is inside root
if (normalizedRoot && resultId.startsWith(normalizedRoot)) {
return resultId.slice(normalizedRoot.length - 1);
} else {
return "/@fs" + prependForwardSlash(resultId);
}
}
return VALID_ID_PREFIX + resultId;
}
// https://github.com/vitejs/vite/blob/2f9428d1ffd988e30cb253d5bb84844fb1654e86/packages/vite/src/node/constants.ts#L108
// Used by isCSSRequest() under the hood
export const CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;