Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

esm.ts 891 B

You have to be logged in to leave a comment. Sign In
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
  1. import * as path from 'path';
  2. import { pathToFileURL } from 'node:url';
  3. // esm-specific crap that needs to get mocked out in tests
  4. //import path from 'path';
  5. //import { fileURLToPath } from 'url';
  6. export function getDirectory(): string {
  7. /*
  8. // @ts-ignore: Jest chokes on this
  9. const __filename = fileURLToPath(import.meta.url);
  10. return path.dirname(__filename);
  11. */
  12. return __dirname;
  13. }
  14. export async function importModule(modulePath: string, functionName?: string) {
  15. // This is some hacky shit. It prevents typescript from transpiling `import` to `require`, which breaks mjs imports.
  16. const resolvedPath = pathToFileURL(path.resolve(modulePath));
  17. const importedModule = await eval(`import('${resolvedPath}')`);
  18. const mod = importedModule?.default?.default || importedModule?.default || importedModule;
  19. if (functionName) {
  20. return mod[functionName];
  21. }
  22. return mod;
  23. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...