Skip to content

Commit

Permalink
fix(nextjs): Respect CLI output path when provided
Browse files Browse the repository at this point in the history
closes: nrwl#17881
  • Loading branch information
ndcunningham committed Jul 4, 2023
1 parent 8f0ec5c commit 9c9041d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
13 changes: 13 additions & 0 deletions e2e/next/src/next.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ describe('Next.js Applications', () => {
});
}, 1_000_000);

it('should build app and .next artifacts at the outputPath if provided by the CLI', () => {
const appName = uniq('app');
runCLI(`generate @nx/next:app ${appName} --no-interactive --style=css`);

runCLI(`build ${appName} --outputPath="dist/foo"`);

checkFilesExist('dist/foo/package.json');
checkFilesExist('dist/foo/next.config.js');
// Next Files
checkFilesExist('dist/foo/.next/package.json');
checkFilesExist('dist/foo/.next/build-manifest.json');
}, 600_000);

// TODO(jack): re-enable this test
xit('should be able to serve with a proxy configuration', async () => {
const appName = uniq('app');
Expand Down
4 changes: 4 additions & 0 deletions packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ function withNx(
}
});

// process.env.NX_NEXT_OUTPUT_PATH is set when running @nx/next:build
options.outputPath =
process.env.NX_NEXT_OUTPUT_PATH || options.outputPath;

// outputPath may be undefined if using run-commands or other executors other than @nx/next:build.
// In this case, the user should set distDir in their next.config.js.
if (options.outputPath) {
Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export default async function buildExecutor(
process.env['__NEXT_REACT_ROOT'] ||= 'true';
}

const { experimentalAppOnly, profile, debug } = options;
const { experimentalAppOnly, profile, debug, outputPath } = options;

// Set output path here since it can also be set via CLI
// We can retrieve it inside plugins/with-nx
process.env.NX_NEXT_OUTPUT_PATH ??= outputPath;

const args = createCliOptions({ experimentalAppOnly, profile, debug });
const command = `npx next build ${args.join(' ')}`;
Expand Down

0 comments on commit 9c9041d

Please sign in to comment.