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

fix: helpful error when `output: export` and `next export -o` (#51191)

We already have a warning when using `next export` with `output: export` because the `next export` is effectively a no-op (the exported output was already generated during `next build`).

However, this PR ensures that `next export -o <dir>` throws an error when `output: export` is defined because the files were already emitted to the `distDir` in the `next build` step and we can't safely no-op at this point.

We also guide the user to the correct solution which is to use `output: export` along with `distDir: <dir>` in your next.config.js (instead of mixing legacy behavior with the new behavior).

fix #48207
fix NEXT-980
parent de83bf55
...@@ -63,6 +63,7 @@ const nextExport: CliCommand = (argv) => { ...@@ -63,6 +63,7 @@ const nextExport: CliCommand = (argv) => {
silent: args['--silent'] || false, silent: args['--silent'] || false,
threads: args['--threads'], threads: args['--threads'],
outdir: args['--outdir'] ? resolve(args['--outdir']) : join(dir, 'out'), outdir: args['--outdir'] ? resolve(args['--outdir']) : join(dir, 'out'),
hasOutdirFromCli: Boolean(args['--outdir']),
isInvokedFromCli: true, isInvokedFromCli: true,
hasAppDir: false, hasAppDir: false,
buildExport: false, buildExport: false,
......
...@@ -154,6 +154,7 @@ export interface ExportOptions { ...@@ -154,6 +154,7 @@ export interface ExportOptions {
exportAppPageWorker?: typeof import('./worker').default exportAppPageWorker?: typeof import('./worker').default
endWorker?: () => Promise<void> endWorker?: () => Promise<void>
nextConfig?: NextConfigComplete nextConfig?: NextConfigComplete
hasOutdirFromCli?: boolean
} }
export default async function exportApp( export default async function exportApp(
...@@ -184,6 +185,11 @@ export default async function exportApp( ...@@ -184,6 +185,11 @@ export default async function exportApp(
// Running 'next export' // Running 'next export'
if (options.isInvokedFromCli) { if (options.isInvokedFromCli) {
if (isExportOutput) { if (isExportOutput) {
if (options.hasOutdirFromCli) {
throw new ExportError(
'"next export -o <dir>" cannot be used when "output: export" is configured in next.config.js. Instead add "distDir" in next.config.js https://nextjs.org/docs/advanced-features/static-html-export'
)
}
Log.warn( Log.warn(
'"next export" is no longer needed when "output: export" is configured in next.config.js https://nextjs.org/docs/advanced-features/static-html-export' '"next export" is no longer needed when "output: export" is configured in next.config.js https://nextjs.org/docs/advanced-features/static-html-export'
) )
......
import fs from 'fs-extra' import fs from 'fs-extra'
import { nextBuild, nextExport } from 'next-test-utils' import { nextBuild, nextExport, nextExportDefault } from 'next-test-utils'
import { join } from 'path' import { join } from 'path'
import { import {
appDir, appDir,
...@@ -39,24 +39,50 @@ describe('app dir with output export (next dev / next build)', () => { ...@@ -39,24 +39,50 @@ describe('app dir with output export (next dev / next build)', () => {
expect(await getFiles()).toEqual(expectedFiles) expect(await getFiles()).toEqual(expectedFiles)
let stdout = '' let stdout = ''
let stderr = '' let stderr = ''
await nextExport( await nextExportDefault(appDir, {
appDir, onStdout(msg) {
{ outdir: exportDir }, stdout += msg
{ },
onStdout(msg) { onStderr(msg) {
stdout += msg stderr += msg
}, },
onStderr(msg) { })
stderr += msg
},
}
)
expect(stderr).toContain( expect(stderr).toContain(
'- warn "next export" is no longer needed when "output: export" is configured in next.config.js' '- warn "next export" is no longer needed when "output: export" is configured in next.config.js'
) )
expect(stdout).toContain('Export successful. Files written to') expect(stdout).toContain('Export successful. Files written to')
expect(await getFiles()).toEqual(expectedFiles) expect(await getFiles()).toEqual(expectedFiles)
}) })
it('should error when "next export -o <dir>" is used with config', async () => {
await fs.remove(distDir)
await fs.remove(exportDir)
await nextBuild(appDir)
expect(await getFiles()).toEqual(expectedFiles)
let stdout = ''
let stderr = ''
let error = undefined
try {
await nextExport(
appDir,
{ outdir: exportDir },
{
onStdout(msg) {
stdout += msg
},
onStderr(msg) {
stderr += msg
},
}
)
} catch (e) {
error = e
}
expect(error).toBeDefined()
expect(stderr).toContain(
'- error "next export -o <dir>" cannot be used when "output: export" is configured in next.config.js. Instead add "distDir" in next.config.js'
)
expect(stdout).not.toContain('Export successful. Files written to')
})
it('should error when no config.output detected for next export', async () => { it('should error when no config.output detected for next export', async () => {
await fs.remove(distDir) await fs.remove(distDir)
await fs.remove(exportDir) await fs.remove(exportDir)
......
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