-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathloader.ts
56 lines (50 loc) · 2.29 KB
/
loader.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { LoaderResult, LoaderSync } from "cosmiconfig";
import { createJiti } from "jiti";
type Jiti = ReturnType<typeof createJiti>;
type JitiOptions = Parameters<typeof createJiti>[1];
import { TypeScriptCompileError } from "./typescript-compile-error.js";
type LoaderAsync = (filepath: string, content: string) => Promise<LoaderResult>;
export function TypeScriptLoader(options?: JitiOptions): LoaderAsync {
const loader: Jiti = createJiti("", { interopDefault: true, ...options });
return async (path: string, _content: string): Promise<LoaderResult> => {
try {
// Because the import resolved as `unknown`, in the union of `unknown & { default?: unknown }`
// `unknown` is the loosest type, however, we know it's an imported module possibly with a
// default export set.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const result = (await loader.import(path)) as { default?: unknown };
// `default` is used when exporting using export default, some modules
// may still use `module.exports` or if in TS `export = `
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return result.default || result;
} catch (error) {
if (error instanceof Error) {
// Coerce generic error instance into typed error with better logging.
throw TypeScriptCompileError.fromError(error);
}
throw error;
}
};
}
/**
* @deprecated use `TypeScriptLoader`
*/
export function TypeScriptLoaderSync(options?: JitiOptions): LoaderSync {
const loader: Jiti = createJiti("", { interopDefault: true, ...options });
return (path: string, _content: string): LoaderResult => {
try {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const result = loader(path) as { default?: unknown };
// `default` is used when exporting using export default, some modules
// may still use `module.exports` or if in TS `export = `
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return result.default || result;
} catch (error) {
if (error instanceof Error) {
// Coerce generic error instance into typed error with better logging.
throw TypeScriptCompileError.fromError(error);
}
throw error;
}
};
}