Unverified Commit b1770137 authored by Steven's avatar Steven Committed by GitHub

Add error if `app` detected but config is missing (#41696)

Previously, creating the `app` dir without enabling the config would
print an error about `pages` missing:

> Couldn't find a `pages` directory. Please create one under the project
root

This PR prints a better error message:

> The `app` dir is experimental. Please add `{experimental:{appDir:
true}}` to your `next.config.js` to enable it
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
parent 383ec5a6
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import * as Log from '../build/output/log'
export const existsSync = (f: string): boolean => { export const existsSync = (f: string): boolean => {
try { try {
...@@ -29,21 +30,25 @@ export function findPagesDir( ...@@ -29,21 +30,25 @@ export function findPagesDir(
appDir: string | undefined appDir: string | undefined
} { } {
const pagesDir = findDir(dir, 'pages') || undefined const pagesDir = findDir(dir, 'pages') || undefined
let appDir: undefined | string const appDir = findDir(dir, 'app') || undefined
if (isAppDirEnabled) { if (isAppDirEnabled && appDir == null && pagesDir == null) {
appDir = findDir(dir, 'app') || undefined
}
const hasAppDir =
!!appDir && fs.existsSync(appDir) && fs.statSync(appDir).isDirectory()
if (hasAppDir && appDir == null && pagesDir == null) {
throw new Error( throw new Error(
"> Couldn't find any `pages` or `app` directory. Please create one under the project root" "> Couldn't find any `pages` or `app` directory. Please create one under the project root"
) )
} }
if (!isAppDirEnabled) { if (!isAppDirEnabled) {
if (appDir != null && pagesDir == null) {
throw new Error(
'> The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
)
}
if (appDir != null && pagesDir != null) {
Log.warn(
'The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
)
}
if (pagesDir == null) { if (pagesDir == null) {
throw new Error( throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root" "> Couldn't find a `pages` directory. Please create one under the project root"
...@@ -53,6 +58,6 @@ export function findPagesDir( ...@@ -53,6 +58,6 @@ export function findPagesDir(
return { return {
pagesDir, pagesDir,
appDir, appDir: isAppDirEnabled ? appDir : undefined,
} }
} }
export default function Root({ children }) {
return (
<html>
<head>
<title>Missing Config</title>
</head>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello from app</p>
}
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import {
killApp,
findPort,
launchApp,
nextBuild,
waitFor,
} from 'next-test-utils'
const dir = path.join(__dirname, '..')
const nextConfig = path.join(dir, 'next.config.js')
const pagesIndex = path.join(dir, 'pages', 'index.js')
const msg =
'The `app` dir is experimental. Please add `{experimental:{appDir: true}}` to your `next.config.js` to enable it'
function runTests(justPutIt: () => Promise<string>) {
it('should print error when missing config with app', async () => {
const output = await justPutIt()
expect(output).toMatch(`Error: > ${msg}`)
})
it('should print warning when missing config with app and pages', async () => {
await fs.outputFile(pagesIndex, 'module.exports = "index"')
const output = await justPutIt()
expect(output).toMatch(`warn - ${msg}`)
})
it('should not print when config found with app', async () => {
await fs.writeFile(
nextConfig,
'module.exports = {experimental:{appDir: true}}'
)
const output = await justPutIt()
expect(output).not.toMatch(`Error: > ${msg}`)
expect(output).not.toMatch(`warn - ${msg}`)
})
it('should not print when config found with app and pages', async () => {
await fs.outputFile(pagesIndex, 'module.exports = "index"')
await fs.writeFile(
nextConfig,
'module.exports = {experimental:{appDir: true}}'
)
const output = await justPutIt()
expect(output).not.toMatch(`Error: > ${msg}`)
expect(output).not.toMatch(`warn - ${msg}`)
})
}
describe('Error when app dir is present without experimental.appDir', () => {
describe('next dev', () => {
const justPutIt = async () => {
let app
try {
const appPort = await findPort()
let output = ''
app = await launchApp(dir, appPort, {
onStdout(data: string) {
output += data
},
onStderr(data: string) {
output += data
},
})
await waitFor(200)
return output
} finally {
if (app?.pid) killApp(app)
await fs.remove(nextConfig)
await fs.remove(path.join(dir, 'pages'))
}
}
runTests(justPutIt)
})
describe('next build', () => {
const justPutIt = async () => {
let app
try {
app = await nextBuild(dir, [], {
stdout: true,
stderr: true,
env: { NEXT_SKIP_APP_REACT_INSTALL: '1' },
})
return app.stdout + app.stderr
} finally {
if (app?.pid) killApp(app)
await fs.remove(nextConfig)
await fs.remove(path.join(dir, 'pages'))
}
}
runTests(justPutIt)
})
})
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