Skip to content

Commit 458e183

Browse files
committed
fix: resolve modules as array
1 parent 81a938a commit 458e183

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { overrides } = require('@netlify/eslint-config-node/react_config')
33
module.exports = {
44
extends: '@netlify/eslint-config-node/react_config',
55
rules: {
6+
'max-depth': 0,
67
complexity: 0,
78
'fp/no-let': 0,
89
'fp/no-loops': 0,

helpers/resolveNextModule.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
/**
2+
* Try one or more Next.js imports until one is found.
23
* We can't require() these normally, because the "next" package might not be resolvable from the root of a monorepo
34
*/
4-
const resolveNextModule = (module, nextRoot) => {
5-
// Get the default list of require paths...
6-
const paths = require.resolve.paths(module)
7-
// ...add the root of the Next site to the beginning of that list so we try it first...
8-
paths.unshift(nextRoot)
9-
// ...then resolve the module using that list of paths.
10-
return require.resolve(module, { paths })
5+
const resolveNextModule = (modules, nextRoot) => {
6+
if (!Array.isArray(modules)) {
7+
// eslint-disable-next-line no-param-reassign
8+
modules = [modules]
9+
}
10+
for (const key in modules) {
11+
const module = modules[key]
12+
// Get the default list of require paths...
13+
const paths = require.resolve.paths(module)
14+
// ...add the root of the Next site to the beginning of that list so we try it first...
15+
paths.unshift(nextRoot)
16+
// ...then resolve the module using that list of paths.
17+
try {
18+
const resolved = require.resolve(module, { paths })
19+
if (resolved) {
20+
console.log('resolved', resolved)
21+
return resolved
22+
}
23+
} catch (error) {
24+
// Failed. Trying next.
25+
}
26+
}
27+
28+
throw new Error(`Could not resolve Next module. Tried "${modules.join(', ')}"`)
1129
}
1230

1331
module.exports = resolveNextModule

0 commit comments

Comments
 (0)