Skip to content

Commit f120f04

Browse files
committed
inline build-time env vars in the worker script
1 parent 3cd27c4 commit f120f04

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function bundleServer(config: Config, openNextOptions: BuildOptions
3131
// Copy over prerendered assets (e.g. SSG routes)
3232
copyPrerenderedRoutes(config);
3333

34-
copyPackageCliFiles(packageDistDir, config, openNextOptions);
34+
await copyPackageCliFiles(packageDistDir, config, openNextOptions);
3535

3636
const nextConfigStr =
3737
fs

packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@ import fs from "node:fs";
22
import path from "node:path";
33

44
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
5+
import { build } from "esbuild";
56

67
import { Config } from "../../../config";
8+
import { extractProjectEnvVars } from "../../utils";
79

810
/**
9-
* Copies the template files present in the cloudflare adapter package into the standalone node_modules folder
11+
* Copies the template files present in the cloudflare adapter package into the
12+
* standalone node_modules folder and applies necessary transformations.
1013
*/
11-
export function copyPackageCliFiles(packageDistDir: string, config: Config, openNextConfig: BuildOptions) {
14+
export async function copyPackageCliFiles(
15+
packageDistDir: string,
16+
config: Config,
17+
openNextConfig: BuildOptions
18+
) {
1219
console.log("# copyPackageTemplateFiles");
1320
const sourceDir = path.join(packageDistDir, "cli");
1421
const destinationDir = path.join(config.paths.internal.package, "cli");
1522

1623
fs.cpSync(sourceDir, destinationDir, { recursive: true });
1724

18-
fs.copyFileSync(
19-
path.join(packageDistDir, "cli", "templates", "worker.ts"),
20-
path.join(openNextConfig.outputDir, "worker.ts")
21-
);
25+
const envVars = extractProjectEnvVars(openNextConfig);
26+
await build({
27+
entryPoints: [path.join(packageDistDir, "cli", "templates", "worker.ts")],
28+
outfile: path.join(openNextConfig.outputDir, "worker.js"),
29+
format: "esm",
30+
target: "esnext",
31+
bundle: false,
32+
minify: false,
33+
define: {
34+
__OPENNEXT_BUILD_TIME_ENV: JSON.stringify(envVars),
35+
},
36+
});
2237
}

0 commit comments

Comments
 (0)