Skip to content

Commit 379d6fd

Browse files
committed
Fix next.config.js resolution
1 parent ea07d97 commit 379d6fd

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const util = require('util')
55
const nextOnNetlify = require('next-on-netlify')
66
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
77
const { default: loadConfig } = require('next/dist/next-server/server/config')
8+
const findUp = require('find-up')
89
const makeDir = require('make-dir')
910
const pathExists = require('path-exists')
1011
const cpx = require('cpx')
@@ -53,8 +54,8 @@ module.exports = {
5354
)
5455
}
5556

56-
const hasNextConfig = await pathExists('next.config.js')
57-
if (hasNextConfig) {
57+
const nextConfigPath = await findUp('next.config.js')
58+
if (nextConfigPath !== undefined) {
5859
// If the next config exists, fail build if target isnt in acceptableTargets
5960
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
6061
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme",
2626
"dependencies": {
2727
"cpx": "^1.5.0",
28+
"find-up": "^4.1.0",
2829
"make-dir": "^3.1.0",
2930
"next": "^9.5.3",
3031
"next-on-netlify": "^2.6.0",

test/fixtures/deep_invalid_next_config/base/.gitkeep

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
target: 'server',
3+
}

test/index.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,24 @@ describe('preBuild()', () => {
120120
expect(await pathExists('next.config.js')).toBeTruthy()
121121
})
122122

123-
test(`fail build if the app's next config has an invalid target`, async () => {
124-
const { restoreCwd } = useFixture('invalid_next_config')
125-
await plugin.onPreBuild({
126-
netlifyConfig: {},
127-
packageJson: DUMMY_PACKAGE_JSON,
128-
utils,
129-
constants: { FUNCTIONS_SRC: 'out_functions' },
130-
})
131-
restoreCwd()
132-
133-
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
134-
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
135-
`next.config.js must be one of: ${acceptableTargets.join(', ')}`,
136-
)
137-
})
123+
test.each(['invalid_next_config', 'deep_invalid_next_config'])(
124+
`fail build if the app's next config has an invalid target`,
125+
async (fixtureName) => {
126+
const { restoreCwd } = useFixture(fixtureName)
127+
await plugin.onPreBuild({
128+
netlifyConfig: {},
129+
packageJson: DUMMY_PACKAGE_JSON,
130+
utils,
131+
constants: { FUNCTIONS_SRC: 'out_functions' },
132+
})
133+
restoreCwd()
134+
135+
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
136+
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
137+
`next.config.js must be one of: ${acceptableTargets.join(', ')}`,
138+
)
139+
},
140+
)
138141
})
139142

140143
describe('onBuild()', () => {

0 commit comments

Comments
 (0)