-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathcompile-init.ts
More file actions
38 lines (34 loc) · 1.39 KB
/
compile-init.ts
File metadata and controls
38 lines (34 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import path from "node:path";
import { fileURLToPath } from "node:url";
import { loadConfig } from "@opennextjs/aws/adapters/config/util.js";
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
import { build } from "esbuild";
import type { Unstable_Config } from "wrangler";
/**
* Compiles the initialization code for the workerd runtime
*/
export async function compileInit(options: BuildOptions, wranglerConfig: Unstable_Config): Promise<void> {
const currentDir = path.join(path.dirname(fileURLToPath(import.meta.url)));
const templatesDir = path.join(currentDir, "../../templates");
const initPath = path.join(templatesDir, "init.js");
const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
const basePath = nextConfig.basePath ?? "";
const deploymentId = nextConfig.deploymentId ?? "";
const trailingSlash = nextConfig.trailingSlash ?? false;
await build({
entryPoints: [initPath],
outdir: path.join(options.outputDir, "cloudflare"),
bundle: false,
minify: false,
format: "esm",
target: "esnext",
platform: "node",
define: {
__BUILD_TIMESTAMP_MS__: JSON.stringify(Date.now()),
__NEXT_BASE_PATH__: JSON.stringify(basePath),
__ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
__DEPLOYMENT_ID__: JSON.stringify(deploymentId),
__TRAILING_SLASH__: JSON.stringify(trailingSlash),
},
});
}