-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathbuild.ts
More file actions
109 lines (93 loc) · 3.9 KB
/
build.ts
File metadata and controls
109 lines (93 loc) · 3.9 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { buildNextjsApp, setStandaloneBuildMode } from "@opennextjs/aws/build/buildNextApp.js";
import { compileCache } from "@opennextjs/aws/build/compileCache.js";
import { createCacheAssets, createStaticAssets } from "@opennextjs/aws/build/createAssets.js";
import { createMiddleware } from "@opennextjs/aws/build/createMiddleware.js";
import * as buildHelper from "@opennextjs/aws/build/helper.js";
import { printHeader } from "@opennextjs/aws/build/utils.js";
import logger from "@opennextjs/aws/logger.js";
import type { Unstable_Config } from "wrangler";
import { OpenNextConfig } from "../../api/config.js";
import type { ProjectOptions } from "../project-options.js";
import { bundleServer } from "./bundle-server.js";
import { compileCacheAssetsManifestSqlFile } from "./open-next/compile-cache-assets-manifest.js";
import { compileEnvFiles } from "./open-next/compile-env-files.js";
import { compileImages } from "./open-next/compile-images.js";
import { compileInit } from "./open-next/compile-init.js";
import { compileSkewProtection } from "./open-next/compile-skew-protection.js";
import { compileDurableObjects } from "./open-next/compileDurableObjects.js";
import { createServerBundle } from "./open-next/createServerBundle.js";
import { createWranglerConfigIfNotExistent } from "./utils/index.js";
import { getVersion } from "./utils/version.js";
/**
* Builds the application in a format that can be passed to workerd
*
* It saves the output in a `.worker-next` directory
*
* @param options The OpenNext options
* @param config The OpenNext config
* @param projectOpts The options for the project
*/
export async function build(
options: buildHelper.BuildOptions,
config: OpenNextConfig,
projectOpts: ProjectOptions,
wranglerConfig: Unstable_Config
): Promise<void> {
// Do not minify the code so that we can apply string replacement patch.
options.minify = false;
// Pre-build validation
buildHelper.checkRunningInsideNextjsApp(options);
logger.info(`App directory: ${options.appPath}`);
buildHelper.printNextjsVersion(options);
ensureNextjsVersionSupported(options);
const { aws, cloudflare } = getVersion();
logger.info(`@opennextjs/cloudflare version: ${cloudflare}`);
logger.info(`@opennextjs/aws version: ${aws}`);
if (projectOpts.skipNextBuild) {
logger.warn("Skipping Next.js build");
} else {
// Build the next app
printHeader("Building Next.js app");
setStandaloneBuildMode(options);
buildNextjsApp(options);
}
// Generate deployable bundle
printHeader("Generating bundle");
buildHelper.initOutputDir(options);
compileCache(options);
compileEnvFiles(options);
compileInit(options, wranglerConfig);
compileImages(options);
compileSkewProtection(options, config);
// Compile middleware
await createMiddleware(options, { forceOnlyBuildOnce: true });
createStaticAssets(options, { useBasePath: true });
if (config.dangerous?.disableIncrementalCache !== true) {
const { useTagCache, metaFiles } = createCacheAssets(options);
if (useTagCache) {
compileCacheAssetsManifestSqlFile(options, metaFiles);
}
}
await createServerBundle(options);
await compileDurableObjects(options);
await bundleServer(options, projectOpts);
if (!projectOpts.skipWranglerConfigCheck) {
await createWranglerConfigIfNotExistent(projectOpts);
}
logger.info("OpenNext build complete.");
}
function ensureNextjsVersionSupported(options: buildHelper.BuildOptions) {
if (buildHelper.compareSemver(options.nextVersion, "<", "14.2.0")) {
logger.error("Next.js version unsupported, please upgrade to version 14.2 or greater.");
process.exit(1);
}
// TODO: remove when 15.4 is supported
// Note: `e2e/experimental` is on 15.4.0-canary.14 which works
if (
!options.appPath.endsWith("opennextjs-cloudflare/examples/e2e/experimental") &&
buildHelper.compareSemver(options.nextVersion, ">=", "15.4.0")
) {
logger.error("Next.js version unsupported, the latest supported version is 15.3");
process.exit(1);
}
}