import { isUrl } from "../utils/path.ts"; import { isPlainObject } from "../utils/object.ts"; import { env } from "../utils/env.ts"; import type { RawData } from "../file.ts"; /** Load a JavaScript/TypeScript file. Use a random hash to prevent caching */ export default async function module(path: string): Promise { const url = isUrl(path) ? path : `file://${path}`; const specifier = env("LUME_LIVE_RELOAD") ? `${url}#${Date.now()}` : url; const mod = await import(specifier); return toData(mod); } /** Transform the imported module to RawData */ export function toData(mod: Record): RawData { const data: RawData = {}; for (const [name, value] of Object.entries(mod)) { if (name === "default") { if (isPlainObject(value)) { Object.assign(data, value); } else { data.content = value; } continue; } data[name] = value; } return data; }