-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathbuild.ts
More file actions
124 lines (98 loc) · 4.12 KB
/
build.ts
File metadata and controls
124 lines (98 loc) · 4.12 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { createRequire } from "node:module";
import { dirname } from "node:path";
import { buildNextjsApp, setStandaloneBuildMode } from "@opennextjs/aws/build/buildNextApp.js";
import { compileCache } from "@opennextjs/aws/build/compileCache.js";
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.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, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
import logger from "@opennextjs/aws/logger.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 { copyCacheAssets } from "./open-next/copyCacheAssets.js";
import { createServerBundle } from "./open-next/createServerBundle.js";
import {
createOpenNextConfigIfNotExistent,
createWranglerConfigIfNotExistent,
ensureCloudflareConfig,
populateCache,
} 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 projectOpts The options for the project
*/
export async function build(projectOpts: ProjectOptions): Promise<void> {
printHeader("Cloudflare build");
showWarningOnWindows();
const baseDir = projectOpts.sourceDir;
const require = createRequire(import.meta.url);
const openNextDistDir = dirname(require.resolve("@opennextjs/aws/index.js"));
await createOpenNextConfigIfNotExistent(projectOpts);
const { config, buildDir } = await compileOpenNextConfig(baseDir);
ensureCloudflareConfig(config);
// Initialize options
const options = buildHelper.normalizeOptions(config, openNextDistDir, buildDir);
logger.setLevel(options.debug ? "debug" : "info");
// Do not minify the code so that we can apply string replacement patch.
// Note that wrangler will still minify the bundle.
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.populateCache?.onlyPopulateWithoutBuilding) {
populateCache(options, config, projectOpts.populateCache.mode);
return;
}
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);
// Compile cache.ts
compileCache(options);
// Compile .env files
compileEnvFiles(options);
// Compile middleware
await createMiddleware(options, { forceOnlyBuildOnce: true });
createStaticAssets(options);
if (config.dangerous?.disableIncrementalCache !== true) {
const { useTagCache, metaFiles } = createCacheAssets(options);
copyCacheAssets(options);
if (useTagCache) {
compileCacheAssetsManifestSqlFile(options, metaFiles);
}
}
await createServerBundle(options);
await bundleServer(options);
if (!projectOpts.skipWranglerConfigCheck) {
await createWranglerConfigIfNotExistent(projectOpts);
}
if (projectOpts.populateCache) {
populateCache(options, config, projectOpts.populateCache.mode);
}
logger.info("OpenNext build complete.");
}
function ensureNextjsVersionSupported(options: buildHelper.BuildOptions) {
if (buildHelper.compareSemver(options.nextVersion, "14.0.0") < 0) {
logger.error("Next.js version unsupported, please upgrade to version 14 or greater.");
process.exit(1);
}
}