UNPKG

1.12 kBJavaScriptView Raw
1import { pickExportFromModule, pickExportFromModuleSync } from './exports.js';
2/**
3 * @internal
4 */
5export 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 * @internal
17 */
18export 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 * @internal
30 */
31function 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}