diff --git a/e2e/next/src/next.test.ts b/e2e/next/src/next.test.ts index 1c709fe4a2133..c65e134767698 100644 --- a/e2e/next/src/next.test.ts +++ b/e2e/next/src/next.test.ts @@ -1,6 +1,7 @@ import { detectPackageManager, joinPathFragments } from '@nx/devkit'; import { capitalize } from '@nx/devkit/src/utils/string-utils'; import { + checkFilesDoNotExist, checkFilesExist, cleanupProject, getPackageManagerCommand, @@ -355,6 +356,21 @@ describe('Next.js Applications', () => { checkFilesExist(`dist/apps/${appName}/next.config.js`); }, 300_000); + it('should build in dev mode without errors', async () => { + const appName = uniq('app'); + + runCLI( + `generate @nx/next:app ${appName} --no-interactive --style=css --appDir=false` + ); + + checkFilesDoNotExist(`apps/${appName}/.next/build-manifest.json`); + checkFilesDoNotExist(`apps/${appName}/.nx-helpers/with-nx.js`); + + expect(() => { + runCLI(`build ${appName} --configuration=development`); + }).not.toThrow(); + }, 300_000); + it('should support --js flag', async () => { const appName = uniq('app'); diff --git a/packages/next/src/executors/build/build.impl.ts b/packages/next/src/executors/build/build.impl.ts index a0a93036313c6..3cf9848161edd 100644 --- a/packages/next/src/executors/build/build.impl.ts +++ b/packages/next/src/executors/build/build.impl.ts @@ -92,11 +92,13 @@ export default async function buildExecutor( }); } - createNextConfigFile(options, context); - - copySync(join(projectRoot, 'public'), join(options.outputPath, 'public'), { - dereference: true, - }); - + // If output path is different from source path, then copy over the config and public files. + // This is the default behavior when running `nx build `. + if (options.outputPath.replace(/\/$/, '') !== projectRoot) { + createNextConfigFile(options, context); + copySync(join(projectRoot, 'public'), join(options.outputPath, 'public'), { + dereference: true, + }); + } return { success: true }; } diff --git a/packages/next/src/executors/build/lib/create-next-config-file.ts b/packages/next/src/executors/build/lib/create-next-config-file.ts index 928bd9aa20cc6..3d6a8f506767f 100644 --- a/packages/next/src/executors/build/lib/create-next-config-file.ts +++ b/packages/next/src/executors/build/lib/create-next-config-file.ts @@ -24,6 +24,13 @@ export function createNextConfigFile( options: NextBuildBuilderOptions, context: ExecutorContext ) { + // Don't overwrite the next.config.js file if output path is the same as the source path. + if ( + options.outputPath.replace(/\/$/, '') === + context.projectGraph.nodes[context.projectName].data.root + ) { + return; + } const projectRoot = context.projectGraph.nodes[context.projectName].data.root; const configRelativeToProjectRoot = findNextConfigPath( projectRoot,