Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions test/integration/handle-non-page-in-pages/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { target: 'serverless' }
2 changes: 2 additions & 0 deletions test/integration/handle-non-page-in-pages/pages/invalid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const Invalid = (): JSX.Element => <p>Hello world</p>
export { Invalid }
1 change: 1 addition & 0 deletions test/integration/handle-non-page-in-pages/pages/valid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (): JSX.Element => <p>Hello world</p>
18 changes: 18 additions & 0 deletions test/integration/handle-non-page-in-pages/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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/)
})
})
29 changes: 29 additions & 0 deletions test/integration/handle-non-page-in-pages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"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"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down