diff --git a/.gitignore b/.gitignore index 3e9432bb10..6e1003cdb3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ test/sample/netlify/functions test/sample/my-publish-dir test/sample/.next test/sample/.netlify -next.config.js +/next.config.js # Logs logs diff --git a/helpers/doesNotNeedPlugin.js b/helpers/doesNotNeedPlugin.js index 6e86fbccc2..fd5889c9c8 100644 --- a/helpers/doesNotNeedPlugin.js +++ b/helpers/doesNotNeedPlugin.js @@ -5,7 +5,7 @@ const isStaticExportProject = require('./isStaticExportProject') const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify') const hasCorrectNextConfig = require('./hasCorrectNextConfig') -const doesNotNeedPlugin = async ({ netlifyConfig, packageJson }) => { +const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, utils }) => { const { build } = netlifyConfig const { name, scripts = {} } = packageJson const nextConfigPath = await findUp('next.config.js') @@ -13,7 +13,7 @@ const doesNotNeedPlugin = async ({ netlifyConfig, packageJson }) => { return ( isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson }) || - !hasCorrectNextConfig(nextConfigPath) + !hasCorrectNextConfig({ nextConfigPath, failBuild: utils.build.failBuild }) ) } diff --git a/helpers/hasCorrectNextConfig.js b/helpers/hasCorrectNextConfig.js index 076a2d01fd..64de96fd75 100644 --- a/helpers/hasCorrectNextConfig.js +++ b/helpers/hasCorrectNextConfig.js @@ -1,7 +1,7 @@ const path = require('path') // Checks if site has the correct next.cofig.js -const hasCorrectNextConfig = (nextConfigPath) => { +const hasCorrectNextConfig = ({ nextConfigPath, failBuild }) => { // In the plugin's case, no config is valid because we'll make it ourselves if (nextConfigPath === undefined) return true @@ -12,7 +12,12 @@ const hasCorrectNextConfig = (nextConfigPath) => { // If the next config exists, log warning if target isnt in acceptableTargets const acceptableTargets = ['serverless', 'experimental-serverless-trace'] - const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.')) + let nextConfig + try { + nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.')) + } catch (error) { + return failBuild('Error loading your next.config.js.', { error }) + } const isValidTarget = acceptableTargets.includes(nextConfig.target) if (!isValidTarget) { console.log( diff --git a/index.js b/index.js index ac1779b8b3..9b2b707e8e 100644 --- a/index.js +++ b/index.js @@ -24,7 +24,7 @@ module.exports = { return failBuild('Could not find a package.json for this project') } - if (await doesNotNeedPlugin({ netlifyConfig, packageJson })) { + if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) { return } @@ -39,8 +39,13 @@ module.exports = { await pWriteFile('next.config.js', nextConfig) } }, - async onBuild({ netlifyConfig, packageJson, constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) { - if (await doesNotNeedPlugin({ netlifyConfig, packageJson })) { + async onBuild({ + netlifyConfig, + packageJson, + constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC }, + utils, + }) { + if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) { return } diff --git a/test/fixtures/broken_next_config/next.config.js b/test/fixtures/broken_next_config/next.config.js new file mode 100644 index 0000000000..b7fff5557c --- /dev/null +++ b/test/fixtures/broken_next_config/next.config.js @@ -0,0 +1,6 @@ +module.exports = { + target: "serverless", + i18n: { + locales: ["en", "fr" + } +}; diff --git a/test/index.js b/test/index.js index b60b5f672e..5d9cf88902 100644 --- a/test/index.js +++ b/test/index.js @@ -141,6 +141,19 @@ describe('preBuild()', () => { }), ).rejects.toThrow(`Could not find a package.json for this project`) }) + + test('fail build if the app cant load the next.config.js', async () => { + await useFixture('broken_next_config') + + await expect( + plugin.onPreBuild({ + netlifyConfig, + packageJson: DUMMY_PACKAGE_JSON, + utils, + constants: { FUNCTIONS_SRC: 'out_functions' }, + }), + ).rejects.toThrow(`Error loading your next.config.js.`) + }) }) describe('onBuild()', () => { @@ -155,6 +168,7 @@ describe('onBuild()', () => { netlifyConfig, packageJson, constants: {}, + utils, }) expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy() @@ -170,6 +184,7 @@ describe('onBuild()', () => { packageJson: DUMMY_PACKAGE_JSON, utils, constants: { FUNCTIONS_SRC: 'out_functions' }, + utils, }) expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy() @@ -187,6 +202,7 @@ describe('onBuild()', () => { PUBLISH_DIR, FUNCTIONS_SRC: 'functions', }, + utils, }) expect(await pathExists(`${PUBLISH_DIR}/_redirects`)).toBeTruthy() @@ -206,6 +222,7 @@ describe('onBuild()', () => { FUNCTIONS_SRC, PUBLISH_DIR: '.', }, + utils, }) expect(await pathExists(`${resolvedFunctions}/next_api_test/next_api_test.js`)).toBeTruthy()