main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | /**
|
---|
2 | * Utility for resolving a module relative to another module
|
---|
3 | * @author Teddy Katz
|
---|
4 | */
|
---|
5 |
|
---|
6 | import Module from "module";
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * `Module.createRequire` is added in v12.2.0. It supports URL as well.
|
---|
10 | * We only support the case where the argument is a filepath, not a URL.
|
---|
11 | */
|
---|
12 | const createRequire = Module.createRequire;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Resolves a Node module relative to another module
|
---|
16 | * @param {string} moduleName The name of a Node module, or a path to a Node module.
|
---|
17 | * @param {string} relativeToPath An absolute path indicating the module that `moduleName` should be resolved relative to. This must be
|
---|
18 | * a file rather than a directory, but the file need not actually exist.
|
---|
19 | * @returns {string} The absolute path that would result from calling `require.resolve(moduleName)` in a file located at `relativeToPath`
|
---|
20 | */
|
---|
21 | function resolve(moduleName, relativeToPath) {
|
---|
22 | try {
|
---|
23 | return createRequire(relativeToPath).resolve(moduleName);
|
---|
24 | } catch (error) {
|
---|
25 |
|
---|
26 | // This `if` block is for older Node.js than 12.0.0. We can remove this block in the future.
|
---|
27 | if (
|
---|
28 | typeof error === "object" &&
|
---|
29 | error !== null &&
|
---|
30 | error.code === "MODULE_NOT_FOUND" &&
|
---|
31 | !error.requireStack &&
|
---|
32 | error.message.includes(moduleName)
|
---|
33 | ) {
|
---|
34 | error.message += `\nRequire stack:\n- ${relativeToPath}`;
|
---|
35 | }
|
---|
36 | throw error;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | export {
|
---|
41 | resolve
|
---|
42 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.