Skip to content

Commit 821070d

Browse files
committed
fix #150; require default compat
1 parent 5e4d085 commit 821070d

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/runtime/stdlib/require.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require.resolve = resolve;
22

3-
const cache = new WeakMap<object, object>();
3+
const cache = new WeakMap<object, unknown>();
44

55
export function require(...specifiers: unknown[]): unknown {
66
return specifiers.length === 1
@@ -36,10 +36,16 @@ function resolve(_specifier: unknown): string {
3636
return `https://cdn.jsdelivr.net/npm/${name}${range}${path + (isFile(path) || isDirectory(path) ? "" : "/+esm")}`;
3737
}
3838

39+
/** Promote exclusive default export to module. */
40+
function defaultify(module: object): unknown {
41+
for (const key in module) if (key !== "default") return {...module}; // any named export
42+
return "default" in module ? module.default : {...module}; // promote exclusive default export
43+
}
44+
3945
/** Allow mutation of required modules while maintaining a canonical instance; ugly! */
40-
function objectify(module: object): object {
46+
function objectify(module: object): unknown {
4147
let object = cache.get(module);
42-
if (!object) cache.set(module, (object = {...module}));
48+
if (!object) cache.set(module, (object = defaultify(module)));
4349
return object;
4450
}
4551

0 commit comments

Comments
 (0)