Skip to content

Commit 42320e7

Browse files
move all cloudflare adapter generated files under the .worker-next directory (#26)
* move all cloudflare adapter generated files under the `.worker-next` directory In particular, rather than mutating the original `.next` files, we always make a copy to the `.worker-next/.next` directory and then mutate those. * fix formatting of settings.json
1 parent e2c4535 commit 42320e7

File tree

4 files changed

+26
-38
lines changed

4 files changed

+26
-38
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"editor.formatOnSave": true
2+
"editor.formatOnSave": true,
3+
"cSpell.words": ["nextjs"]
34
}

packages/cloudflare/src/build/build-worker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { NextjsAppPaths } from "../nextjs-paths";
22
import { build, Plugin } from "esbuild";
33
import { existsSync, readFileSync } from "node:fs";
44
import { cp, readFile, writeFile } from "node:fs/promises";
5+
import path from "node:path";
6+
import { fileURLToPath } from "node:url";
57

68
import { patchRequire } from "./patches/investigated/patch-require";
79
import { copyTemplates } from "./patches/investigated/copy-templates";
@@ -13,17 +15,19 @@ import { inlineEvalManifest } from "./patches/to-investigate/inline-eval-manifes
1315
import { patchWranglerDeps } from "./patches/to-investigate/wrangler-deps";
1416
import { updateWebpackChunksFile } from "./patches/investigated/update-webpack-chunks-file";
1517

18+
/** The directory containing the Cloudflare template files. */
19+
const templateSrcDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "templates");
20+
1621
/**
1722
* Using the Next.js build output in the `.next` directory builds a workerd compatible output
1823
*
1924
* @param outputDir the directory where to save the output
2025
* @param nextjsAppPaths
2126
*/
2227
export async function buildWorker(
23-
inputNextAppDir: string,
28+
appDir: string,
2429
outputDir: string,
25-
nextjsAppPaths: NextjsAppPaths,
26-
templateSrcDir: string
30+
nextjsAppPaths: NextjsAppPaths
2731
): Promise<void> {
2832
const templateDir = copyTemplates(templateSrcDir, nextjsAppPaths);
2933

@@ -132,8 +136,8 @@ Request = globalThis.Request;
132136
});
133137

134138
// Copy over any static files (e.g. images) from the source project
135-
if (existsSync(`${inputNextAppDir}/public`)) {
136-
await cp(`${inputNextAppDir}/public`, `${outputDir}/assets`, {
139+
if (existsSync(`${appDir}/public`)) {
140+
await cp(`${appDir}/public`, `${outputDir}/assets`, {
137141
recursive: true,
138142
});
139143
}

packages/cloudflare/src/build/build.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,33 @@ import { rm } from "node:fs/promises";
22
import { buildNextjsApp } from "./build-next-app";
33
import { buildWorker } from "./build-worker";
44
import { getNextjsAppPaths } from "../nextjs-paths";
5-
import path from "node:path";
6-
import { fileURLToPath } from "node:url";
7-
import { cpSync, rmSync } from "node:fs";
8-
9-
const SAVE_DIR = ".save.next";
5+
import { cpSync } from "node:fs";
6+
import { resolve } from "node:path";
107

118
/**
129
* Builds the application in a format that can be passed to workerd
1310
*
1411
* It saves the output in a `.worker-next` directory
1512
*
16-
* @param inputNextAppDir the directory of the Next.js app to build
13+
* @param appDir the directory of the Next.js app to build
1714
* @param opts.outputDir the directory where to save the output (defaults to the app's directory)
1815
* @param opts.skipBuild boolean indicating whether the Next.js build should be skipped (i.e. if the `.next` dir is already built)
1916
*/
20-
export async function build(inputNextAppDir: string, opts: BuildOptions): Promise<void> {
17+
export async function build(appDir: string, opts: BuildOptions): Promise<void> {
2118
if (!opts.skipBuild) {
22-
// Build the next app and save a copy in .save.next
23-
buildNextjsApp(inputNextAppDir);
24-
rmSync(`${inputNextAppDir}/${SAVE_DIR}`, {
25-
recursive: true,
26-
force: true,
27-
});
28-
cpSync(`${inputNextAppDir}/.next`, `${inputNextAppDir}/${SAVE_DIR}`, {
29-
recursive: true,
30-
});
31-
} else {
32-
// Skip the next build and restore the copy from .next.save
33-
rmSync(`${inputNextAppDir}/.next`, { recursive: true, force: true });
34-
cpSync(`${inputNextAppDir}/${SAVE_DIR}`, `${inputNextAppDir}/.next`, {
35-
recursive: true,
36-
});
19+
// Build the next app
20+
buildNextjsApp(appDir);
3721
}
3822

39-
const outputDir = `${opts.outputDir ?? inputNextAppDir}/.worker-next`;
23+
// Create a clean output directory
24+
const outputDir = resolve(opts.outputDir ?? appDir, ".worker-next");
4025
await cleanDirectory(outputDir);
4126

42-
const nextjsAppPaths = getNextjsAppPaths(inputNextAppDir);
43-
44-
const templateDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "templates");
27+
// Copy the .next directory to the output directory so it can be mutated.
28+
cpSync(resolve(`${appDir}/.next`), resolve(`${outputDir}/.next`), { recursive: true });
29+
const nextjsAppPaths = getNextjsAppPaths(outputDir);
4530

46-
await buildWorker(inputNextAppDir, outputDir, nextjsAppPaths, templateDir);
31+
await buildWorker(appDir, outputDir, nextjsAppPaths);
4732
}
4833

4934
type BuildOptions = {

packages/cloudflare/src/nextjs-paths.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import path, { relative } from "node:path";
77
* NOTE: WIP, we still need to discern which paths are relevant here!
88
*/
99
export type NextjsAppPaths = {
10-
appDir: string;
1110
/**
1211
* The path to the application's `.next` directory (where `next build` saves the build output)
1312
*/
@@ -32,18 +31,17 @@ export type NextjsAppPaths = {
3231
/**
3332
* Collects all the paths necessary for dealing with the Next.js applications output
3433
*
35-
* @param nextAppDir The path to the Next.js app
34+
* @param baseDir The path to the directory that contains the .next directory
3635
* @returns the various paths.
3736
*/
38-
export function getNextjsAppPaths(nextAppDir: string): NextjsAppPaths {
39-
const dotNextDir = getDotNextDirPath(nextAppDir);
37+
export function getNextjsAppPaths(baseDir: string): NextjsAppPaths {
38+
const dotNextDir = getDotNextDirPath(baseDir);
4039

4140
const appPath = getNextjsApplicationPath(dotNextDir).replace(/\/$/, "");
4241

4342
const standaloneAppDir = path.join(dotNextDir, "standalone", appPath);
4443

4544
return {
46-
appDir: nextAppDir,
4745
dotNextDir,
4846
standaloneAppDir,
4947
standaloneAppDotNextDir: path.join(standaloneAppDir, ".next"),

0 commit comments

Comments
 (0)