forked from firebase/firebase-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicImport.js
22 lines (20 loc) · 933 Bytes
/
dynamicImport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const { pathToFileURL } = require("url");
// If being compiled with webpack, use non webpack require for these calls.
// (VSCode plugin uses webpack which by default replaces require calls
// with its own require, which doesn't work on files)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const requireFunc =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore prevent VSCE webpack from erroring on non_webpack_require
// eslint-disable-next-line camelcase
typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
exports.dynamicImport = function(mod) {
if (mod.startsWith("file://")) return import(mod);
if (mod.startsWith("/")) return import(pathToFileURL(mod).toString());
try {
const path = requireFunc.resolve(mod);
return import(pathToFileURL(path).toString());
} catch(e) {
return Promise.reject(e);
}
}