|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const { cwd: getCwd } = require('process') |
3 | 4 | const { resolve } = require('path')
|
4 | 5 |
|
5 | 6 | const moize = require('moize')
|
6 | 7 |
|
7 | 8 | // We used to cache nextConfig for any cwd. Now we pass process.cwd() to cache
|
8 | 9 | // (or memoize) nextConfig per cwd.
|
9 |
| -const getNextConfig = async function (cwd, failBuild = defaultFailBuild) { |
| 10 | +const getNextConfig = async function (failBuild = defaultFailBuild, cwd = getCwd()) { |
10 | 11 | // We cannot load `next` at the top-level because we validate whether the
|
11 | 12 | // site is using `next` inside `onPreBuild`.
|
12 | 13 | const { PHASE_PRODUCTION_BUILD } = require('next/constants')
|
13 | 14 | const loadConfig = require('next/dist/next-server/server/config').default
|
14 | 15 |
|
15 | 16 | try {
|
16 |
| - return await loadConfig(PHASE_PRODUCTION_BUILD, resolve('.')) |
| 17 | + return await loadConfig(PHASE_PRODUCTION_BUILD, cwd) |
17 | 18 | } catch (error) {
|
18 | 19 | return failBuild('Error loading your next.config.js.', { error })
|
19 | 20 | }
|
20 | 21 | }
|
21 | 22 |
|
22 |
| -const moizedGetNextConfig = moize(getNextConfig, { maxSize: 1e3, isPromise: true }) |
| 23 | +const moizedGetNextConfig = moize(getNextConfig, { |
| 24 | + maxSize: 1e3, |
| 25 | + isPromise: true, |
| 26 | + // Memoization cache key. We need to use `transformArgs` so `process.cwd()` |
| 27 | + // default value is assigned |
| 28 | + transformArgs: ([, cwd = getCwd()]) => [cwd], |
| 29 | +}) |
23 | 30 |
|
24 | 31 | const defaultFailBuild = function (message, { error }) {
|
25 | 32 | throw new Error(`${message}\n${error.stack}`)
|
26 | 33 | }
|
27 | 34 |
|
28 |
| -// module.exports = process.env.NODE_ENV === 'test' ? getNextConfig : moizedGetNextConfig |
29 | 35 | module.exports = moizedGetNextConfig
|
0 commit comments