1 | import { pickExportFromModule, pickExportFromModuleSync } from './exports.js';
|
2 |
|
3 |
|
4 |
|
5 | export async function tryToLoadFromExport(rawFilePath) {
|
6 | try {
|
7 | const filepath = ensureFilepath(rawFilePath);
|
8 | const mod = await import(filepath);
|
9 | return await pickExportFromModule({ module: mod, filepath });
|
10 | }
|
11 | catch (e) {
|
12 | throw new Error(`Unable to load from file "${rawFilePath}": ${e.stack || e.message}`);
|
13 | }
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 | export function tryToLoadFromExportSync(rawFilePath) {
|
19 | try {
|
20 | const filepath = ensureFilepath(rawFilePath);
|
21 | const mod = require(filepath);
|
22 | return pickExportFromModuleSync({ module: mod, filepath });
|
23 | }
|
24 | catch (e) {
|
25 | throw new Error(`Unable to load from file "${rawFilePath}": ${e.stack || e.message}`);
|
26 | }
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 | function ensureFilepath(filepath) {
|
32 | if (typeof require !== 'undefined' && require.cache) {
|
33 | filepath = require.resolve(filepath);
|
34 | if (require.cache[filepath]) {
|
35 | delete require.cache[filepath];
|
36 | }
|
37 | }
|
38 | return filepath;
|
39 | }
|