Skip to content

Commit cc44647

Browse files
committed
refactor: pass config to build function
1 parent 8d18ca4 commit cc44647

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export async function buildWorker(config: Config): Promise<void> {
3232
// Copy over client-side generated files
3333
await cp(
3434
path.join(config.paths.dotNext, "static"),
35-
path.join(config.paths.builderOutput, "assets", "_next", "static"),
35+
path.join(config.paths.outputDir, "assets", "_next", "static"),
3636
{
3737
recursive: true,
3838
}
3939
);
4040

4141
// Copy over any static files (e.g. images) from the source project
42-
const publicDir = path.join(config.paths.nextApp, "public");
42+
const publicDir = path.join(config.paths.sourceDir, "public");
4343
if (existsSync(publicDir)) {
44-
await cp(publicDir, path.join(config.paths.builderOutput, "assets"), {
44+
await cp(publicDir, path.join(config.paths.outputDir, "assets"), {
4545
recursive: true,
4646
});
4747
}
@@ -52,7 +52,7 @@ export async function buildWorker(config: Config): Promise<void> {
5252
copyPackageCliFiles(packageDistDir, config);
5353

5454
const workerEntrypoint = path.join(config.paths.internalTemplates, "worker.ts");
55-
const workerOutputFile = path.join(config.paths.builderOutput, "index.mjs");
55+
const workerOutputFile = path.join(config.paths.outputDir, "index.mjs");
5656

5757
const nextConfigStr =
5858
readFileSync(path.join(config.paths.standaloneApp, "/server.js"), "utf8")?.match(
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
import { containsDotNextDir, getConfig } from "../config";
2+
import type { ProjectOptions } from "../config";
23
import { buildNextjsApp } from "./build-next-app";
34
import { buildWorker } from "./build-worker";
45
import { cpSync } from "node:fs";
5-
import path from "node:path";
6+
import { join } from "node:path";
67
import { rm } from "node:fs/promises";
78

89
/**
910
* Builds the application in a format that can be passed to workerd
1011
*
1112
* It saves the output in a `.worker-next` directory
1213
*
13-
* @param appDir the directory of the Next.js app to build
14-
* @param opts.outputDir the directory where to save the output (defaults to the app's directory)
15-
* @param opts.skipBuild boolean indicating whether the Next.js build should be skipped (i.e. if the `.next` dir is already built)
14+
* @param opts The options for the project
1615
*/
17-
export async function build(appDir: string, opts: BuildOptions): Promise<void> {
16+
export async function build(opts: ProjectOptions): Promise<void> {
1817
if (!opts.skipBuild) {
1918
// Build the next app
20-
await buildNextjsApp(appDir);
19+
await buildNextjsApp(opts.sourceDir);
2120
}
2221

23-
if (!containsDotNextDir(appDir)) {
24-
throw new Error(`.next folder not found in ${appDir}`);
22+
if (!containsDotNextDir(opts.sourceDir)) {
23+
throw new Error(`.next folder not found in ${opts.sourceDir}`);
2524
}
2625

27-
// Create a clean output directory
28-
const outputDir = path.resolve(opts.outputDir ?? appDir, ".worker-next");
29-
await cleanDirectory(outputDir);
26+
// Clean the output directory
27+
await cleanDirectory(opts.outputDir);
3028

3129
// Copy the .next directory to the output directory so it can be mutated.
32-
cpSync(path.join(appDir, ".next"), path.join(outputDir, ".next"), { recursive: true });
30+
cpSync(join(opts.sourceDir, ".next"), join(opts.outputDir, ".next"), { recursive: true });
3331

34-
const config = getConfig(appDir, outputDir);
32+
const config = getConfig(opts);
3533

3634
await buildWorker(config);
3735
}
3836

39-
type BuildOptions = {
40-
skipBuild: boolean;
41-
outputDir?: string;
42-
};
43-
4437
async function cleanDirectory(path: string): Promise<void> {
4538
return await rm(path, { recursive: true, force: true });
4639
}

packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export async function patchCache(code: string, config: Config): Promise<string>
1010

1111
const cacheHandlerFileName = "cache-handler.mjs";
1212
const cacheHandlerEntrypoint = join(config.paths.internalTemplates, "cache-handler", "index.ts");
13-
const cacheHandlerOutputFile = join(config.paths.builderOutput, cacheHandlerFileName);
13+
const cacheHandlerOutputFile = join(config.paths.outputDir, cacheHandlerFileName);
1414

1515
await build({
1616
entryPoints: [cacheHandlerEntrypoint],

packages/cloudflare/src/cli/build/utils/copy-prerendered-routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function copyPrerenderedRoutes(config: Config) {
1919

2020
const serverAppDirPath = join(config.paths.standaloneAppServer, "app");
2121
const prerenderManifestPath = join(config.paths.standaloneAppDotNext, "prerender-manifest.json");
22-
const outputPath = join(config.paths.builderOutput, "assets", SEED_DATA_DIR);
22+
const outputPath = join(config.paths.outputDir, "assets", SEED_DATA_DIR);
2323

2424
const prerenderManifest: PrerenderManifest = existsSync(prerenderManifestPath)
2525
? JSON.parse(readFileSync(prerenderManifestPath, "utf8"))
@@ -38,7 +38,7 @@ export function copyPrerenderedRoutes(config: Config) {
3838

3939
if (fullPath.endsWith(NEXT_META_SUFFIX)) {
4040
const data = JSON.parse(readFileSync(fullPath, "utf8"));
41-
writeFileSync(destPath, JSON.stringify({ ...data, lastModified: config.buildTimestamp }));
41+
writeFileSync(destPath, JSON.stringify({ ...data, lastModified: config.build.timestamp }));
4242
} else {
4343
copyFileSync(fullPath, destPath);
4444
}

packages/cloudflare/src/cli/config.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ const UserConfig = {
1111
};
1212

1313
export type Config = {
14-
// Timestamp for when the build was started
15-
buildTimestamp: number;
14+
build: {
15+
// Timestamp for when the build was started
16+
timestamp: number;
17+
// Whether to skip building the Next.js app or not
18+
shouldSkip: boolean;
19+
};
1620

1721
paths: {
1822
// Path to the next application
19-
nextApp: string;
23+
sourceDir: string;
2024
// Path to the output folder
21-
builderOutput: string;
25+
outputDir: string;
2226
// Path to the app's `.next` directory (where `next build` saves the build output)
2327
dotNext: string;
2428
// Path to the application standalone root directory
@@ -46,13 +50,14 @@ export type Config = {
4650
/**
4751
* Computes the configuration.
4852
*
49-
* @param appDir Next app root folder
50-
* @param outputDir Output of the cloudflare builder
53+
* @param opts.sourceDir Next app root folder
54+
* @param opts.outputDir The directory where to save the output (defaults to the app's directory)
55+
* @param opts.skipBuild Whether the Next.js build should be skipped (i.e. if the `.next` dir is already built)
5156
*
52-
* @returns the configuration, see `Config`
57+
* @returns The configuration, see `Config`
5358
*/
54-
export function getConfig(appDir: string, outputDir: string): Config {
55-
const dotNext = path.join(outputDir, ".next");
59+
export function getConfig(opts: ProjectOptions): Config {
60+
const dotNext = path.join(opts.outputDir, ".next");
5661
const appPath = getNextjsApplicationPath(dotNext).replace(/\/$/, "");
5762
const standaloneRoot = path.join(dotNext, "standalone");
5863
const standaloneApp = path.join(standaloneRoot, appPath);
@@ -64,11 +69,14 @@ export function getConfig(appDir: string, outputDir: string): Config {
6469
const internalTemplates = path.join(internalPackage, "cli", "templates");
6570

6671
return {
67-
buildTimestamp: Date.now(),
72+
build: {
73+
timestamp: Date.now(),
74+
shouldSkip: !!opts.skipBuild,
75+
},
6876

6977
paths: {
70-
nextApp: appDir,
71-
builderOutput: outputDir,
78+
sourceDir: opts.sourceDir,
79+
outputDir: opts.outputDir,
7280
dotNext,
7381
standaloneRoot,
7482
standaloneApp,
@@ -94,6 +102,12 @@ export function containsDotNextDir(folder: string): boolean {
94102
}
95103
}
96104

105+
export type ProjectOptions = {
106+
sourceDir: string;
107+
outputDir: string;
108+
skipBuild?: boolean;
109+
};
110+
97111
/**
98112
* It basically tries to find the path that the application is under inside the `.next/standalone` directory, using the `.next/server` directory
99113
* presence as the condition that needs to be met.

packages/cloudflare/src/cli/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`)
1515

1616
const { skipBuild, outputDir } = getArgs();
1717

18-
await build(nextAppDir, {
19-
outputDir,
20-
skipBuild: !!skipBuild,
18+
await build({
19+
sourceDir: nextAppDir,
20+
outputDir: resolve(outputDir ?? nextAppDir, ".worker-next"),
21+
skipBuild,
2122
});

0 commit comments

Comments
 (0)