22 lines
733 B
1
/*
2
For tree shaking reasons, sometimes we need to require() instead of an import
3
statement at the top of the module (like config, which runs node *and* web).
4
5
require() doesn't seem to carry the types from @types/node, so this allows us
6
to requireImport("fs") and still keep the types of fs.
7
8
In the future, I'd like to automate ImportTypes, but I think the type is only
9
cemented if import is passed a string literal.
10
*/
11
12
const _canRequire = ["path", "fs"] as const;
13
type CanRequire = (typeof _canRequire)[number];
14
15
type ImportTypes = {
16
path: typeof import("path");
17
fs: typeof import("fs");
18
};
19
20
export default function requireImport<T extends CanRequire>(type: T): Awaited<ImportTypes[T]> {
21
return require(type);
22
}
23