diff --git a/index.js b/index.js index 325922606b..a941f2d22f 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const util = require('util') const nextOnNetlify = require('next-on-netlify') const { PHASE_PRODUCTION_BUILD } = require('next/constants') const { default: loadConfig } = require('next/dist/next-server/server/config') +const findUp = require('find-up') const makeDir = require('make-dir') const pathExists = require('path-exists') const cpx = require('cpx') @@ -52,8 +53,8 @@ module.exports = { ) } - const hasNextConfig = await pathExists('next.config.js') - if (hasNextConfig) { + const nextConfigPath = await findUp('next.config.js') + if (nextConfigPath !== undefined) { // If the next config exists, fail build if target isnt in acceptableTargets const acceptableTargets = ['serverless', 'experimental-serverless-trace'] const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.')) diff --git a/package.json b/package.json index 269998345c..33dfbcc0df 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme", "dependencies": { "cpx": "^1.5.0", + "find-up": "^4.1.0", "make-dir": "^3.1.0", "next": "^9.5.3", "next-on-netlify": "^2.6.0", diff --git a/test/fixtures/deep_invalid_next_config/base/.gitkeep b/test/fixtures/deep_invalid_next_config/base/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/deep_invalid_next_config/next.config.js b/test/fixtures/deep_invalid_next_config/next.config.js new file mode 100644 index 0000000000..f72b6cf52f --- /dev/null +++ b/test/fixtures/deep_invalid_next_config/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + target: 'server', +} diff --git a/test/index.js b/test/index.js index 53c8d910d9..d345887643 100644 --- a/test/index.js +++ b/test/index.js @@ -117,17 +117,20 @@ describe('preBuild()', () => { expect(await pathExists('next.config.js')).toBeTruthy() }) - test(`fail build if the app's next config has an invalid target`, async () => { - await useFixture('invalid_next_config') - await expect( - plugin.onPreBuild({ - netlifyConfig: {}, - packageJson: DUMMY_PACKAGE_JSON, - utils, - constants: { FUNCTIONS_SRC: 'out_functions' }, - }), - ).rejects.toThrow(`next.config.js must be one of: serverless, experimental-serverless-trace`) - }) + test.each(['invalid_next_config', 'deep_invalid_next_config'])( + `fail build if the app's next config has an invalid target`, + async (fixtureName) => { + await useFixture(fixtureName) + await expect( + plugin.onPreBuild({ + netlifyConfig: {}, + packageJson: DUMMY_PACKAGE_JSON, + utils, + constants: { FUNCTIONS_SRC: 'out_functions' }, + }), + ).rejects.toThrow(`next.config.js must be one of: serverless, experimental-serverless-trace`) + }, + ) }) describe('onBuild()', () => {