Zheng Feng ddd9aec558
fix(frontend): default to Webpack over Turbopack in dev to avoid PostCSS worker leak on macOS (#5133)
* fix(frontend): default to Webpack over Turbopack in dev to avoid PostCSS worker leak on macOS

On macOS arm64, Turbopack + Next.js 16.2.11 + Tailwind CSS v4 causes an
unbounded spawn of PostCSS evaluator processes that consume high CPU and
memory and never return a response. Webpack is unaffected.

Change the no-override default in getDevBundler() from platform-dependent
Turbopack (all non-Windows) to Webpack. DEER_FLOW_DEV_BUNDLER=turbo
continues to work as an explicit opt-in for local diagnosis.

Fixes #5132

* docs(frontend): address webpack default review feedback

* docs(frontend): clarify webpack default rationale
2026-09-01 22:56:23 +08:00

76 lines
2.0 KiB
JavaScript

#!/usr/bin/env node
import { spawn } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
/**
* @param {string} _platform
* @param {Record<string, string | undefined>} env
*/
export function getDevBundler(_platform = process.platform, env = process.env) {
const override = env.DEER_FLOW_DEV_BUNDLER?.trim();
if (override) {
if (override !== "turbo" && override !== "webpack") {
throw new Error(
'DEER_FLOW_DEV_BUNDLER must be either "turbo" or "webpack"',
);
}
return override;
}
// Keep Webpack as the cross-platform default while #5132's Turbopack
// PostCSS worker leak remains unfixed in a stable Next.js release. Retain
// the platform parameter so restoring the platform-aware default stays a
// small change once the upstream fix is stable and verified on macOS/Linux.
return "webpack";
}
/**
* @param {string} platform
* @param {string[]} extraArgs
* @param {Record<string, string | undefined>} env
*/
export function getNextDevArgs(
platform = process.platform,
extraArgs = [],
env = process.env,
) {
const nextArgs = extraArgs[0] === "--" ? extraArgs.slice(1) : extraArgs;
return ["dev", `--${getDevBundler(platform, env)}`, ...nextArgs];
}
function startDevServer() {
const frontendDir = fileURLToPath(new URL("..", import.meta.url));
const nextBin = fileURLToPath(
new URL("../node_modules/next/dist/bin/next", import.meta.url),
);
const child = spawn(
process.execPath,
[nextBin, ...getNextDevArgs(process.platform, process.argv.slice(2))],
{
cwd: frontendDir,
env: process.env,
stdio: "inherit",
},
);
child.on("error", (error) => {
console.error(`Failed to start Next.js: ${error.message}`);
process.exitCode = 1;
});
child.on("exit", (code, signal) => {
if (signal) {
process.kill(process.pid, signal);
return;
}
process.exitCode = code ?? 1;
});
}
if (
process.argv[1] &&
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
) {
startDevServer();
}