The best way to control Claude Code, Codex and more from any computer or phone

main
mjs 91 lines 4.43 KB
Raw
1 import fs from "node:fs";
2 // Alchemy modifications are licensed under Apache-2.0.
3 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
4 // Programmatic @opennextjs/cloudflare build runner — NO wrangler binary, NO
5 // wrangler.json. Executed as a disposable child process (see Build.ts): the
6 // upstream pipeline mutates cwd-coupled module state, spawns `next build`, and
7 // can process.exit(1), so it must never run inside the harness process.
8 //
9 // Instead of importing `dist/cli/commands/utils/utils.js` (which imports
10 // `unstable_readConfig` from "wrangler" at module scope), the two thin
11 // `compileConfig` / `getNormalizedOptions` wrappers are vendored here (~15
12 // lines) over `@opennextjs/aws` exports so that NOTHING on this path ever
13 // imports wrangler code. (`ensureNextjsVersionSupported` still imports
14 // `wrangler/package.json` — a JSON version read satisfied by the inert
15 // `wrangler-stub` package this integration ships.)
16 //
17 // Usage: node runner.mjs '<json>' where json is Build.ts's RunnerConfig:
18 // { appDir, configPath, compatibilityDate, skipNextBuild, minify, debug,
19 // buildCommand }
20 import { createRequire } from "node:module";
21 import path from "node:path";
22 import { pathToFileURL } from "node:url";
23
24 const runnerConfig = JSON.parse(process.argv[2] ?? "{}");
25 const appDir = path.resolve(runnerConfig.appDir ?? process.cwd());
26 process.chdir(appDir); // upstream reads process.cwd() at module scope
27
28 // Resolve the framework integration from the application's dependency tree.
29 const require = createRequire(path.join(appDir, "package.json"));
30 // The exports map doesn't expose "./package.json"; resolve the "." entry
31 // (dist/api/index.js) and walk up to the package root.
32 const cfApiIndex = require.resolve("@opennextjs/cloudflare");
33 const cfRoot = path.resolve(path.dirname(cfApiIndex), "..", "..");
34 const cfRequire = createRequire(path.join(cfRoot, "package.json"));
35
36 /** Import a file from the @opennextjs/cloudflare dist (bypasses the exports map). */
37 const importCf = (p) => import(pathToFileURL(path.join(cfRoot, p)).href);
38 /** Import a subpath of @opennextjs/aws resolved from the cloudflare package. */
39 const importAws = (p) => import(pathToFileURL(cfRequire.resolve(`@opennextjs/aws/${p}`)).href);
40
41 const { compileOpenNextConfig } = await importAws("build/compileConfig.js");
42 const { normalizeOptions } = await importAws("build/helper.js");
43 const { default: logger } = await importAws("logger.js");
44 const { ensureCloudflareConfig } = await importCf("dist/cli/build/utils/ensure-cf-config.js");
45 const { build } = await importCf("dist/cli/build/build.js");
46
47 const configPath = runnerConfig.configPath ?? runnerConfig.generatedConfigPath;
48 if (runnerConfig.generatedConfigPath) {
49 const resolveOverride = (name) =>
50 JSON.stringify(require.resolve(`@opennextjs/cloudflare/overrides/${name}`));
51 const writable = runnerConfig.cache === "kv";
52 fs.writeFileSync(
53 configPath,
54 `
55 import { defineCloudflareConfig } from ${JSON.stringify(cfApiIndex)};
56 import incrementalCache from ${resolveOverride(writable ? "incremental-cache/kv-incremental-cache" : "incremental-cache/static-assets-incremental-cache")};
57 ${writable ? `import queue from ${resolveOverride("queue/do-queue")};\nimport tagCache from ${resolveOverride("tag-cache/kv-next-tag-cache")};` : ""}
58 export default defineCloudflareConfig({ incrementalCache${writable ? ", queue, tagCache" : ""} });
59 `,
60 );
61 }
62 const { config, buildDir } = await compileOpenNextConfig(configPath, {
63 compileEdge: true,
64 });
65 ensureCloudflareConfig(config);
66 config.buildCommand = runnerConfig.buildCommand ?? config.buildCommand ?? "npx next build";
67
68 const openNextDistDir = path.dirname(cfRequire.resolve("@opennextjs/aws/index.js"));
69 const options = normalizeOptions(config, openNextDistDir, buildDir);
70 logger.setLevel(runnerConfig.debug ? "debug" : "info");
71
72 // Only these two Wrangler fields are read by the OpenNext build pipeline.
73 const wranglerConfig = {
74 compatibility_date: runnerConfig.compatibilityDate,
75 assets: { run_worker_first: true },
76 };
77 const projectOptions = {
78 sourceDir: appDir,
79 skipNextBuild: !!runnerConfig.skipNextBuild,
80 skipWranglerConfigCheck: true,
81 minify: !!runnerConfig.minify,
82 };
83 await build(options, config, projectOptions, wranglerConfig, false);
84 console.log("[@alchemy.run/frontend-frameworks/nextjs] OpenNext build finished OK");
85 fs.writeFileSync(
86 runnerConfig.outputPath,
87 JSON.stringify({
88 openNextDirectory: options.outputDir,
89 appBuildOutputPath: options.appBuildOutputPath,
90 }),
91 );