Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions packages/cloudflare/src/cli/build/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { cpSync } from "node:fs";
import { createRequire } from "node:module";
import { dirname, join } 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 "../config.js";
import { containsDotNextDir, getConfig } from "../config.js";
import { bundleServer } from "./bundle-server.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,
} from "./utils/index.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);
buildHelper.printOpenNextVersion(options);

if (projectOpts.skipNextBuild) {
logger.warn("Skipping Next.js build");
} else {
// Build the next app
printHeader("Building Next.js app");
setStandaloneBuildMode(options);
buildNextjsApp(options);
}

if (!containsDotNextDir(projectOpts.sourceDir)) {
throw new Error(`.next folder not found in ${projectOpts.sourceDir}`);
}

// 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) {
createCacheAssets(options);
copyCacheAssets(options);
}

await createServerBundle(options);

// TODO: drop this copy.
// Copy the .next directory to the output directory so it can be mutated.
cpSync(join(projectOpts.sourceDir, ".next"), join(projectOpts.outputDir, ".next"), { recursive: true });

const projConfig = getConfig(projectOpts);

// TODO: rely on options only.
await bundleServer(projConfig, options);

if (!projectOpts.skipWranglerConfigCheck) {
await createWranglerConfigIfNotExistent(projectOpts);
}

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);
}
}
33 changes: 1 addition & 32 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { build, Plugin } from "esbuild";

import { Config } from "../config.js";
import * as patches from "./patches/index.js";
import { normalizePath } from "./utils/index.js";
import { normalizePath, patchCodeWithValidations } from "./utils/index.js";

/** The dist directory of the Cloudflare adapter package */
const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../..");
Expand Down Expand Up @@ -190,37 +190,6 @@ function createFixRequiresESBuildPlugin(config: Config): Plugin {
};
}

/**
* Applies multiple code patches in order to a given piece of code, at each step it validates that the code
* has actually been patched/changed, if not an error is thrown
*
* @param code the code to apply the patches to
* @param patches array of tuples, containing a string indicating the target of the patching (for logging) and
* a patching function that takes a string (pre-patch code) and returns a string (post-patch code)
* @returns the patched code
*/
async function patchCodeWithValidations(
code: string,
patches: [string, (code: string) => string | Promise<string>, opts?: { isOptional?: boolean }][]
): Promise<string> {
console.log(`Applying code patches:`);
let patchedCode = code;

for (const [target, patchFunction, opts] of patches) {
console.log(` - patching ${target}`);

const prePatchCode = patchedCode;
patchedCode = await patchFunction(patchedCode);

if (!opts?.isOptional && prePatchCode === patchedCode) {
throw new Error(`Failed to patch ${target}`);
}
}

console.log(`All ${patches.length} patches applied\n`);
return patchedCode;
}

/**
* Gets the path of the worker.js file generated by the build process
*
Expand Down
Loading
Loading