Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

scripts/ts-js-loader.mjs

Raw
import { existsSync } from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";

export async function resolve(specifier, context, defaultResolve) {
	if (
		specifier.endsWith(".js") &&
		(specifier.startsWith(".") || specifier.startsWith("/"))
	) {
		const parent = context.parentURL
			? new URL(context.parentURL)
			: pathToFileURL(`${process.cwd()}/`);
		const tsPath = fileURLToPath(new URL(specifier, parent)).replace(
			/\.js$/,
			".ts",
		);
		if (existsSync(tsPath))
			return { url: pathToFileURL(tsPath).href, shortCircuit: true };
	}

	return defaultResolve(specifier, context, defaultResolve);
}