Skip to content

Commit 983483d

Browse files
committed
Improve build output handling and update distDir config
The build command now moves or symlinks the .next output directory to the project root after building, improving compatibility with pnpm and preserving internal symlinks. Also, the Next.js config sets distDir to '.next' instead of an absolute path for better portability.
1 parent 07489e0 commit 983483d

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

packages/cli/src/commands/build.mjs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,42 @@ export function registerBuildCommand(cli) {
3939
const child = spawn(nextCmd, args, {
4040
stdio: 'inherit',
4141
env,
42-
// cwd: nextAppDir // CRITICAL: Run in the Next.js app directory
42+
cwd: nextAppDir // CRITICAL: Run in the Next.js app directory
43+
});
44+
45+
child.on('close', (code) => {
46+
if (code === 0) {
47+
// Copy output to project root
48+
const src = path.join(nextAppDir, '.next');
49+
const dest = path.join(process.cwd(), '.next');
50+
51+
if (fs.existsSync(src)) {
52+
console.log(`\nMoving build output to ${dest}...`);
53+
if (fs.existsSync(dest)) {
54+
fs.rmSync(dest, { recursive: true, force: true });
55+
}
56+
fs.cpSync(src, dest, { recursive: true });
57+
console.log(`Build successfully output to: ${dest}`);
58+
} else {
59+
// Check for .next directory (dynamic build)
60+
const srcNext = path.join(nextAppDir, '.next');
61+
const destNext = path.join(process.cwd(), '.next');
62+
63+
if (fs.existsSync(srcNext) && srcNext !== destNext) {
64+
console.log(`\nLinking .next build output to ${destNext}...`);
65+
if (fs.existsSync(destNext)) {
66+
fs.rmSync(destNext, { recursive: true, force: true });
67+
}
68+
// Use symlink instead of copy to preserve internal symlinks in .next (pnpm support)
69+
fs.symlinkSync(srcNext, destNext, 'dir');
70+
console.log(`Build successfully linked to: ${destNext}`);
71+
} else {
72+
console.log(`\nNo 'out' directory generated in ${src}.`);
73+
console.log(`This is expected if 'output: export' is disabled.`);
74+
}
75+
}
76+
}
77+
process.exit(code);
4378
});
4479

4580
});

packages/site/next.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const withMDX = createMDX();
66
/** @type {import('next').NextConfig} */
77
const nextConfig = {
88
reactStrictMode: true,
9-
distDir: path.join(process.cwd(), '.next'),
9+
distDir: '.next',
1010
images: { unoptimized: true },
1111
};
1212

0 commit comments

Comments
 (0)