|
1 | 1 | /**
|
| 2 | + * Try one or more Next.js imports until one is found. |
2 | 3 | * We can't require() these normally, because the "next" package might not be resolvable from the root of a monorepo
|
3 | 4 | */
|
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(', ')}"`) |
11 | 29 | }
|
12 | 30 |
|
13 | 31 | module.exports = resolveNextModule
|
0 commit comments