index.js
2.76 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use strict";
const assert = require("assert");
function findModuleNotFound(err, name) {
// Check the first line of the error message
const msg = err.message.split("\n")[0];
return msg && (
// Check for "Cannot find module 'foo'"
msg.includes(`'${name}'`)
// Check for "Your application tried to access foo (a peer dependency) ..." (Yarn v2 PnP)
// https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L680
|| msg.includes(` ${name} `)
// Check for "Your application tried to access foo. While ..." (Yarn v2 PnP)
// https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L704
|| msg.includes(` ${name}. `)
// Check for "Your application tried to access foo, but ..." (Yarn v2 PnP)
// https://github.com/yarnpkg/berry/blob/e81dc0d29bb2f41818d9c5c1c74bab1406fb979b/packages/yarnpkg-pnp/sources/loader/makeApi.ts#L718
|| msg.includes(` ${name}, `)
);
}
function _optionalRequire(callerRequire, resolve, path, message) {
let opts;
if (typeof message === "object") {
opts = message;
assert(
!(opts.hasOwnProperty("notFound") && opts.hasOwnProperty("default")),
"optionalRequire: options set with both `notFound` and `default`"
);
} else {
opts = { message };
}
try {
return resolve ? callerRequire.resolve(path) : callerRequire(path);
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND" || !findModuleNotFound(e, path)) {
// if the module we are requiring fail because it try to require a
// module that's not found, then we have to report this as failed.
if (typeof opts.fail === "function") {
return opts.fail(e);
}
throw e;
}
if (opts.message) {
const message = typeof opts.message === "string" ? `${opts.message} - ` : "";
const r = resolve ? "resolved" : "found";
optionalRequire.log(`${message}optional module not ${r}`, path);
}
if (typeof opts.notFound === "function") {
return opts.notFound(e);
}
return opts.default;
}
}
const tryRequire = (callerRequire, path, message) => _optionalRequire(callerRequire, false, path, message);
const tryResolve = (callerRequire, path, message) => _optionalRequire(callerRequire, true, path, message);
function optionalRequire(callerRequire) {
const x = (path, message) => tryRequire(callerRequire, path, message);
x.resolve = (path, message) => tryResolve(callerRequire, path, message);
return x;
}
optionalRequire.try = tryRequire;
optionalRequire.tryResolve = tryResolve;
optionalRequire.resolve = tryResolve;
optionalRequire.log = (message, path) => console.log(`Just FYI: ${message}; Path "${path}"`);
module.exports = optionalRequire;