-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathresolveNextModule.js
31 lines (29 loc) · 1 KB
/
resolveNextModule.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
/**
* Try one or more Next.js imports until one is found.
* We can't require() these normally, because the "next" package might not be resolvable from the root of a monorepo
*/
const resolveNextModule = (modules, nextRoot) => {
if (!Array.isArray(modules)) {
// eslint-disable-next-line no-param-reassign
modules = [modules]
}
for (const key in modules) {
const module = modules[key]
// Get the default list of require paths...
const paths = require.resolve.paths(module)
// ...add the root of the Next site to the beginning of that list so we try it first...
paths.unshift(nextRoot)
// ...then resolve the module using that list of paths.
try {
const resolved = require.resolve(module, { paths })
if (resolved) {
console.log('resolved', resolved)
return resolved
}
} catch (error) {
// Failed. Trying next.
}
}
throw new Error(`Could not resolve Next module. Tried "${modules.join(', ')}"`)
}
module.exports = resolveNextModule