1 | /**
|
---|
2 | * @fileoverview Universal module importer
|
---|
3 | */
|
---|
4 |
|
---|
5 | //-----------------------------------------------------------------------------
|
---|
6 | // Imports
|
---|
7 | //-----------------------------------------------------------------------------
|
---|
8 |
|
---|
9 | const { createRequire } = require("module");
|
---|
10 | const { pathToFileURL } = require("url");
|
---|
11 |
|
---|
12 | //-----------------------------------------------------------------------------
|
---|
13 | // Helpers
|
---|
14 | //-----------------------------------------------------------------------------
|
---|
15 |
|
---|
16 | const SLASHES = new Set(["/", "\\"]);
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Normalizes directories to have a trailing slash.
|
---|
20 | * Resolve is pretty finicky -- if the directory name doesn't have
|
---|
21 | * a trailing slash then it tries to look in the parent directory.
|
---|
22 | * i.e., if the directory is "/usr/nzakas/foo" it will start the
|
---|
23 | * search in /usr/nzakas. However, if the directory is "/user/nzakas/foo/",
|
---|
24 | * then it will start the search in /user/nzakas/foo.
|
---|
25 | * @param {string} directory The directory to check.
|
---|
26 | * @returns {string} The normalized directory.
|
---|
27 | */
|
---|
28 | function normalizeDirectory(directory) {
|
---|
29 | if (!SLASHES.has(directory[directory.length-1])) {
|
---|
30 | return directory + "/";
|
---|
31 | }
|
---|
32 |
|
---|
33 | return directory;
|
---|
34 | }
|
---|
35 |
|
---|
36 | //-----------------------------------------------------------------------------
|
---|
37 | // Exports
|
---|
38 | //-----------------------------------------------------------------------------
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Class for importing both CommonJS and ESM modules in Node.js.
|
---|
42 | */
|
---|
43 | exports.ModuleImporter = class ModuleImporter {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates a new instance.
|
---|
47 | * @param {string} [cwd] The current working directory to resolve from.
|
---|
48 | */
|
---|
49 | constructor(cwd = process.cwd()) {
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * The base directory from which paths should be resolved.
|
---|
53 | * @type {string}
|
---|
54 | */
|
---|
55 | this.cwd = normalizeDirectory(cwd);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Resolves a module based on its name or location.
|
---|
60 | * @param {string} specifier Either an npm package name or
|
---|
61 | * relative file path.
|
---|
62 | * @returns {string|undefined} The location of the import.
|
---|
63 | * @throws {Error} If specifier cannot be located.
|
---|
64 | */
|
---|
65 | resolve(specifier) {
|
---|
66 | const require = createRequire(this.cwd);
|
---|
67 | return require.resolve(specifier);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Imports a module based on its name or location.
|
---|
72 | * @param {string} specifier Either an npm package name or
|
---|
73 | * relative file path.
|
---|
74 | * @returns {Promise<object>} The module's object.
|
---|
75 | */
|
---|
76 | import(specifier) {
|
---|
77 | const location = this.resolve(specifier);
|
---|
78 | return import(pathToFileURL(location).href);
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|