-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
module.ts
36 lines (29 loc) 路 955 Bytes
/
module.ts
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
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<RawData> {
const url = isUrl(path) ? path : `file://${path}`;
const specifier = env<boolean>("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<string, unknown>): 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;
}