-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathload.mjs
More file actions
28 lines (22 loc) · 786 Bytes
/
load.mjs
File metadata and controls
28 lines (22 loc) · 786 Bytes
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
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { send } from './ipc.mjs';
const require = createRequire(import.meta.url);
let connectedPort;
export async function initialize({ port } = {}) {
connectedPort = port;
}
export async function load(url, context, nextLoad) {
const required = url.startsWith('file://') ? fileURLToPath(url) : url;
send({ required });
if (connectedPort) connectedPort.postMessage({ required });
try {
return await nextLoad(url, context);
} catch (error) {
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
return require('get-package-type')(required).then(format => {
if (!['builtin', 'commonjs'].includes(format)) throw error;
return { format };
});
}
}