Add support for --experimental-app-only (#45352)

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

Allows you to `next build --experimental-app-only` which excludes
`pages` altogether. Useful for quickly debugging while migrating.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
This commit is contained in:
Tim Neutkens 2023-01-28 19:48:32 +01:00 committed by GitHub
parent 67468f98c4
commit 4972f2308e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 14 deletions

View file

@ -260,7 +260,8 @@ export default async function build(
reactProductionProfiling = false,
debugOutput = false,
runLint = true,
noMangling = false
noMangling = false,
appDirOnly = false
): Promise<void> {
try {
const nextBuildSpan = trace('next-build', undefined, {
@ -481,16 +482,17 @@ export default async function build(
prefixText: `${Log.prefixes.info} Creating an optimized production build`,
})
const pagesPaths = pagesDir
? await nextBuildSpan
.traceChild('collect-pages')
.traceAsyncFn(() =>
recursiveReadDir(
pagesDir,
new RegExp(`\\.(?:${config.pageExtensions.join('|')})$`)
const pagesPaths =
!appDirOnly && pagesDir
? await nextBuildSpan
.traceChild('collect-pages')
.traceAsyncFn(() =>
recursiveReadDir(
pagesDir,
new RegExp(`\\.(?:${config.pageExtensions.join('|')})$`)
)
)
)
: []
: []
let appPaths: string[] | undefined

View file

@ -16,6 +16,7 @@ const nextBuild: CliCommand = (argv) => {
'--debug': Boolean,
'--no-lint': Boolean,
'--no-mangling': Boolean,
'--experimental-app-only': Boolean,
// Aliases
'-h': '--help',
'-d': '--debug',
@ -43,9 +44,10 @@ const nextBuild: CliCommand = (argv) => {
If no directory is provided, the current directory will be used.
Options
--profile Can be used to enable React Production Profiling
--no-lint Disable linting
--no-mangling Disable mangling
--profile Can be used to enable React Production Profiling
--no-lint Disable linting
--no-mangling Disable mangling
--experimental-app-only Only build 'app' routes
`,
0
)
@ -74,7 +76,8 @@ const nextBuild: CliCommand = (argv) => {
args['--profile'],
args['--debug'] || process.env.NEXT_DEBUG_BUILD,
!args['--no-lint'],
args['--no-mangling']
args['--no-mangling'],
args['--experimental-app-only']
).catch((err) => {
console.error('')
if (

View file

@ -0,0 +1,20 @@
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-only-flag',
{
files: __dirname,
buildCommand: 'pnpm next build --experimental-app-only',
},
({ next }) => {
it('should serve app route', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
it('should not serve about route', async () => {
const res = await next.fetch('/about')
expect(res.status).toBe(404)
})
}
)

View file

@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}

View file

@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}

View file

@ -0,0 +1,3 @@
module.exports = {
experimental: { appDir: true },
}

View file

@ -0,0 +1,3 @@
export default function About() {
return <h1>About</h1>
}