Commit e68a5a79 authored by Natalie Marleny's avatar Natalie Marleny Committed by Tim Neutkens

fix: Ensure ergonomic handling of non-page in `./pages/` (#7955)

* Update autoExport wording to automatic static optimization

* Add sufficiently informative error message

- Update other Next error message for consistency

* Implement feedback - add missing character
Co-Authored-By: 's avatarLukáš Huvar <lukas@huvar.cz>

* Fix TypeScript errors

* Update test from autoExport to automatic static optimization

- Also reflect new wording of error message for readability

* Add test for handle non-page in pages

- When target: severless
parent cdf4f0a2
......@@ -240,6 +240,19 @@ export default async function build(dir: string, conf = null): Promise<void> {
const error = result.errors.join('\n\n')
console.error(chalk.red('Failed to compile.\n'))
if (
error.indexOf('private-next-pages') > -1 &&
error.indexOf('does not contain a default export') > -1
) {
const page_name_regex = /\'private-next-pages\/(?<page_name>[^\']*)\'/
const parsed = page_name_regex.exec(error)
const page_name = parsed && parsed.groups && parsed.groups.page_name
throw new Error(
`webpack build failed: found page without a React Component as default export in pages/${page_name}\n\nSee https://err.sh/zeit/next.js/page-without-valid-component for more info.`
)
}
console.error(error)
console.error()
......@@ -371,9 +384,9 @@ export default async function build(dir: string, conf = null): Promise<void> {
if (invalidPages.size > 0) {
throw new Error(
`autoExport failed: found page${
`automatic static optimization failed: found page${
invalidPages.size === 1 ? '' : 's'
} without React Component as default export\n${[...invalidPages]
} without a React Component as default export in \n${[...invalidPages]
.map(pg => `pages${pg}`)
.join(
'\n'
......
module.exports = { target: 'serverless' }
const Invalid = (): JSX.Element => <p>Hello world</p>
export { Invalid }
export default (): JSX.Element => <p>Hello world</p>
/* eslint-env jest */
/* global jasmine, test */
import path from 'path'
import { nextBuild } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = path.join(__dirname, '..')
describe('Handle non-page in pages when target: serverless', () => {
it('Fails softly with descriptive error', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/webpack build failed: found page without a React Component as default export in/
)
expect(stderr).toMatch(/pages\/invalid/)
})
})
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}
......@@ -6,12 +6,12 @@ import { nextBuild } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = path.join(__dirname, '..')
describe('Invalid Page autoExport', () => {
describe('Invalid Page automatic static optimization', () => {
it('Fails softly with descriptive error', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/autoExport failed: found pages without React Component as default export/
/automatic static optimization failed: found pages without a React Component as default export in/
)
expect(stderr).toMatch(/pages\/invalid/)
expect(stderr).toMatch(/pages\/also-invalid/)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment