Skip to content

Commit

Permalink
fix(nextjs): Running nx build --configuration=development should not …
Browse files Browse the repository at this point in the history
…throw an error (#17866)

(cherry picked from commit 3008be1)
  • Loading branch information
ndcunningham authored and FrozenPandaz committed Jun 30, 2023
1 parent 544a100 commit 39c4f03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
16 changes: 16 additions & 0 deletions e2e/next/src/next.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { detectPackageManager, joinPathFragments } from '@nx/devkit';
import { capitalize } from '@nx/devkit/src/utils/string-utils';
import {
checkFilesDoNotExist,
checkFilesExist,
cleanupProject,
getPackageManagerCommand,
Expand Down Expand Up @@ -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');

Expand Down
14 changes: 8 additions & 6 deletions packages/next/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <app>`.
if (options.outputPath.replace(/\/$/, '') !== projectRoot) {
createNextConfigFile(options, context);
copySync(join(projectRoot, 'public'), join(options.outputPath, 'public'), {
dereference: true,
});
}
return { success: true };
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 39c4f03

Please sign in to comment.