-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathimport-or-require.js
38 lines (35 loc) · 1.18 KB
/
import-or-require.js
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
// This fixes weird behavior I was seeing where calls to require(path) would
// fail the first time and then fetch via dynamic import which is correct, but
// then subsequent requires for the same path would return an empty object. Not
// sure if a bug or I'm doing something wrong but since the require/imports here
// are short lived, it is safe to create our own cache and use that.
const { pathToFileURL } = require('url')
const importOrRequireCache = new Map()
const importOrRequire = async path => {
if (importOrRequireCache.has(path)) {
return importOrRequireCache.get(path)
}
let content = {}
try {
content = require(path)
// this is for node 22+
// istanbul ignore next
if (content.__esModule) {
return content.default
}
} catch {
// istanbul ignore next
try {
// this is for node under 20
const results = await import(pathToFileURL(path))
// istanbul ignore next
return results.default
} catch {
// its ok if this fails since the content dir might only be to provide
// other files. the index.js is optional
}
}
importOrRequireCache.set(path, content)
return content
}
module.exports = importOrRequire