Files
llmx/codex-cli/build.mjs

89 lines
3.0 KiB
JavaScript
Raw Normal View History

import * as esbuild from "esbuild";
import * as fs from "fs";
import * as path from "path";
const OUT_DIR = 'dist'
/**
* ink attempts to import react-devtools-core in an ESM-unfriendly way:
*
* https://github.com/vadimdemedes/ink/blob/eab6ef07d4030606530d58d3d7be8079b4fb93bb/src/reconciler.ts#L22-L45
*
* to make this work, we have to strip the import out of the build.
*/
const ignoreReactDevToolsPlugin = {
name: "ignore-react-devtools",
setup(build) {
// When an import for 'react-devtools-core' is encountered,
// return an empty module.
build.onResolve({ filter: /^react-devtools-core$/ }, (args) => {
return { path: args.path, namespace: "ignore-devtools" };
});
build.onLoad({ filter: /.*/, namespace: "ignore-devtools" }, () => {
return { contents: "", loader: "js" };
});
},
};
// ----------------------------------------------------------------------------
// Build mode detection (production vs development)
//
// • production (default): minified, external telemetry shebang handling.
// • development (--dev|NODE_ENV=development|CODEX_DEV=1):
// no minification
// inline source maps for better stacktraces
// shebang tweaked to enable Node's sourcemap support at runtime
// ----------------------------------------------------------------------------
const isDevBuild =
process.argv.includes("--dev") ||
process.env.CODEX_DEV === "1" ||
process.env.NODE_ENV === "development";
const plugins = [ignoreReactDevToolsPlugin];
// Build Hygiene, ensure we drop previous dist dir and any leftover files
const outPath = path.resolve(OUT_DIR);
if (fs.existsSync(outPath)) {
fs.rmSync(outPath, { recursive: true, force: true });
}
// Add a shebang that enables sourcemap support for dev builds so that stack
// traces point to the original TypeScript lines without requiring callers to
// remember to set NODE_OPTIONS manually.
if (isDevBuild) {
const devShebangLine =
"#!/usr/bin/env -S NODE_OPTIONS=--enable-source-maps node\n";
const devShebangPlugin = {
name: "dev-shebang",
setup(build) {
build.onEnd(async () => {
const outFile = path.resolve(isDevBuild ? `${OUT_DIR}/cli-dev.js` : `${OUT_DIR}/cli.js`);
let code = await fs.promises.readFile(outFile, "utf8");
if (code.startsWith("#!")) {
code = code.replace(/^#!.*\n/, devShebangLine);
await fs.promises.writeFile(outFile, code, "utf8");
}
});
},
};
plugins.push(devShebangPlugin);
}
esbuild
.build({
entryPoints: ["src/cli.tsx"],
fix: always load version from package.json at runtime (#909) Note the high-level motivation behind this change is to avoid the need to make temporary changes in the source tree in order to cut a release build since that runs the risk of leaving things in an inconsistent state in the event of a failure. The existing code: ``` import pkg from "../../package.json" assert { type: "json" }; ``` did not work as intended because, as written, ESBuild would bake the contents of the local `package.json` into the release build at build time whereas we want it to read the contents at runtime so we can use the `package.json` in the tree to build the code and later inject a modified version into the release package with a timestamped build version. Changes: * move `CLI_VERSION` out of `src/utils/session.ts` and into `src/version.ts` so `../package.json` is a correct relative path both from `src/version.ts` in the source tree and also in the final `dist/cli.js` build output * change `assert` to `with` in `import pkg` as apparently `with` became standard in Node 22 * mark `"../package.json"` as external in `build.mjs` so the version is not baked into the `.js` at build time After using `pnpm stage-release` to build a release version, if I use Node 22.0 to run Codex, I see the following printed to stderr at startup: ``` (node:71308) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) ``` Note it is a warning and does not prevent Codex from running. In Node 22.12, the warning goes away, but the warning still appears in Node 22.11. For Node 22, 22.15.0 is the current LTS version, so LTS users will not see this. Also, something about moving the definition of `CLI_VERSION` caused a problem with the mocks in `check-updates.test.ts`. I asked Codex to fix it, and it came up with the change to the test configs. I don't know enough about vitest to understand what it did, but the tests seem healthy again, so I'm going with it.
2025-05-12 21:27:15 -07:00
// Do not bundle the contents of package.json at build time: always read it
// at runtime.
external: ["../package.json"],
bundle: true,
format: "esm",
platform: "node",
tsconfig: "tsconfig.json",
outfile: isDevBuild ? `${OUT_DIR}/cli-dev.js` : `${OUT_DIR}/cli.js`,
minify: !isDevBuild,
sourcemap: isDevBuild ? "inline" : true,
plugins,
inject: ["./require-shim.js"],
})
.catch(() => process.exit(1));