From 41d1757b4ecebafa989586932366a29a6b99eefd Mon Sep 17 00:00:00 2001 From: Dmitry Rybin Date: Wed, 29 Jan 2020 09:12:30 +0100 Subject: [PATCH] =?UTF-8?q?fix:=209919=20Add=20warning=20when=20no=20confi?= =?UTF-8?q?g=20is=20exported=20from=20next.con=E2=80=A6=20(#10228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 9919 no exported config found * fix: 9919 remove isolated test, add integration * fix: 9919 add check for successfull compilation and fix warnin check * Add test for development output * fix: 9919 add error page and link to it in warning * Update empty-configuration.md Co-authored-by: JJ Kasper Co-authored-by: Tim Neutkens --- errors/empty-configuration.md | 19 ++++++++ packages/next/next-server/server/config.ts | 8 ++++ test/integration/config-empty/next.config.js | 6 +++ test/integration/config-empty/pages/index.js | 1 + .../config-empty/test/index.test.js | 44 +++++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 errors/empty-configuration.md create mode 100644 test/integration/config-empty/next.config.js create mode 100644 test/integration/config-empty/pages/index.js create mode 100644 test/integration/config-empty/test/index.test.js diff --git a/errors/empty-configuration.md b/errors/empty-configuration.md new file mode 100644 index 000000000000000..316f928d6d1de35 --- /dev/null +++ b/errors/empty-configuration.md @@ -0,0 +1,19 @@ +# Detected next.config.js, no exported configuration found + +#### Why This Warning Occurred + +There is no object exported from next.config.js or the object is empty. + +#### Possible Ways to Fix It + +Check if you correctly export configuration in `next.config.js` file: + +``` +module.exports = { + /* config options here */ +} +``` + +### Useful Links + +- [Introduction to next.config.js](https://nextjs.org/docs/api-reference/next.config.js/introduction) diff --git a/packages/next/next-server/server/config.ts b/packages/next/next-server/server/config.ts index 5fe42e75d34340d..eee2087ff1f03e7 100644 --- a/packages/next/next-server/server/config.ts +++ b/packages/next/next-server/server/config.ts @@ -229,6 +229,14 @@ export default function loadConfig( phase, userConfigModule.default || userConfigModule ) + + if (Object.keys(userConfig).length === 0) { + console.warn( + chalk.yellow.bold('Warning: ') + + 'Detected next.config.js, no exported configuration found. https://err.sh/zeit/next.js/empty-configuration' + ) + } + if (userConfig.target && !targets.includes(userConfig.target)) { throw new Error( `Specified target is invalid. Provided: "${ diff --git a/test/integration/config-empty/next.config.js b/test/integration/config-empty/next.config.js new file mode 100644 index 000000000000000..0c04cc72881a0fa --- /dev/null +++ b/test/integration/config-empty/next.config.js @@ -0,0 +1,6 @@ +/* eslint-disable */ +{ + experimental: { + basePath: '/docs' + } +} diff --git a/test/integration/config-empty/pages/index.js b/test/integration/config-empty/pages/index.js new file mode 100644 index 000000000000000..02d90896bf336a9 --- /dev/null +++ b/test/integration/config-empty/pages/index.js @@ -0,0 +1 @@ +export default () =>
Hello World
diff --git a/test/integration/config-empty/test/index.test.js b/test/integration/config-empty/test/index.test.js new file mode 100644 index 000000000000000..f0f3eb5ed11a050 --- /dev/null +++ b/test/integration/config-empty/test/index.test.js @@ -0,0 +1,44 @@ +/* eslint-env jest */ +/* global jasmine */ +import { join } from 'path' +import { + nextBuild, + launchApp, + findPort, + killApp, + waitFor, +} from 'next-test-utils' + +jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 + +const appDir = join(__dirname, '..') + +describe('Empty configuration', () => { + it('should show relevant warning and compile successfully for next build', async () => { + const { stderr, stdout } = await nextBuild(appDir, [], { + stderr: true, + stdout: true, + }) + expect(stdout).toMatch(/Compiled successfully./) + expect(stderr).toMatch( + /Warning: Detected next.config.js, no exported configuration found. https:\/\/err.sh\/zeit\/next.js\/empty-configuration/ + ) + }) + + it('should show relevant warning and compile successfully for next dev', async () => { + let stderr = '' + + const appPort = await findPort() + const app = await launchApp(appDir, appPort, { + onStderr(msg) { + stderr += msg || '' + }, + }) + await waitFor(1000) + await killApp(app) + + expect(stderr).toMatch( + /Warning: Detected next.config.js, no exported configuration found. https:\/\/err.sh\/zeit\/next.js\/empty-configuration/ + ) + }) +})