diff --git a/.gitignore b/.gitignore index 97866d6522..4dd535be31 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ test/sample/netlify-automatic-functions test/sample/my-publish-dir test/sample/.next test/sample/.netlify +next.config.js # Logs logs diff --git a/.prettierignore b/.prettierignore index 6b72abd42d..2af9f26ac8 100644 --- a/.prettierignore +++ b/.prettierignore @@ -20,3 +20,4 @@ node_modules __mocks__ test/fixtures test/sample +next.config.js diff --git a/helpers/isStaticExportProject.js b/helpers/isStaticExportProject.js index ef8b9e88da..584c5a9e61 100644 --- a/helpers/isStaticExportProject.js +++ b/helpers/isStaticExportProject.js @@ -4,9 +4,14 @@ const isStaticExportProject = ({ build, scripts }) => { const NEXT_EXPORT_COMMAND = 'next export' - const isSetInNetlifyConfig = build && build.command && build.command.includes(NEXT_EXPORT_COMMAND) + + if (!build.command) return false + + const isSetInNetlifyConfig = build.command.includes(NEXT_EXPORT_COMMAND) + const isSetInNpmScript = Object.keys(scripts).find((script) => { - return scripts[script].includes(NEXT_EXPORT_COMMAND) + const scriptValue = scripts[script] + return build.command.includes(script) && scriptValue.includes(NEXT_EXPORT_COMMAND) }) return isSetInNetlifyConfig || isSetInNpmScript } diff --git a/index.js b/index.js index fe5e5ec2c0..74c9601d65 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,9 @@ module.exports = { const { name, scripts = {}, dependencies = {} } = packageJson if (isStaticExportProject({ build, scripts })) { - return failBuild(`** Static HTML export next.js projects do not require this plugin **`) + return failBuild( + `Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`, + ) } const hasNextOnNetlifyInstalled = dependencies['next-on-netlify'] !== undefined diff --git a/test/index.js b/test/index.js index 78de72ad59..4170cc67a9 100644 --- a/test/index.js +++ b/test/index.js @@ -58,17 +58,31 @@ afterEach(async () => { }) const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' } +const netlifyConfig = { build: {} } describe('preBuild()', () => { test('fail build if the app has static html export in npm script', async () => { await expect( plugin.onPreBuild({ - netlifyConfig: {}, + netlifyConfig: { build: { command: 'npm run build' } }, packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } }, utils, constants: { FUNCTIONS_SRC: 'out_functions' }, }), - ).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **') + ).rejects.toThrow( + `Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`, + ) + }) + + test('do not fail build if the app has next export in an unused script', async () => { + await expect( + plugin.onPreBuild({ + netlifyConfig, + packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { export: 'next export' } }, + utils, + constants: {}, + }), + ).resolves }) test('fail build if the app has static html export in toml/ntl config', async () => { @@ -79,7 +93,9 @@ describe('preBuild()', () => { utils, constants: { FUNCTIONS_SRC: 'out_functions' }, }), - ).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **') + ).rejects.toThrow( + `Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`, + ) }) test('fail build if app has next-on-netlify installed', async () => { @@ -88,7 +104,7 @@ describe('preBuild()', () => { } await expect( plugin.onPreBuild({ - netlifyConfig: {}, + netlifyConfig, packageJson, utils, }), @@ -103,7 +119,7 @@ describe('preBuild()', () => { } await expect( plugin.onPreBuild({ - netlifyConfig: {}, + netlifyConfig, packageJson, utils, }), @@ -115,7 +131,7 @@ describe('preBuild()', () => { test('fail build if the app has no package.json', async () => { await expect( plugin.onPreBuild({ - netlifyConfig: {}, + netlifyConfig, packageJson: {}, utils, constants: { FUNCTIONS_SRC: 'out_functions' }, @@ -125,7 +141,7 @@ describe('preBuild()', () => { test('create next.config.js with correct target if file does not exist', async () => { await plugin.onPreBuild({ - netlifyConfig: {}, + netlifyConfig, packageJson: DUMMY_PACKAGE_JSON, utils, constants: { FUNCTIONS_SRC: 'out_functions' }, @@ -140,7 +156,7 @@ describe('preBuild()', () => { await useFixture(fixtureName) await expect( plugin.onPreBuild({ - netlifyConfig: {}, + netlifyConfig, packageJson: DUMMY_PACKAGE_JSON, utils, constants: { FUNCTIONS_SRC: 'out_functions' },