@@ -2,48 +2,33 @@ import { rm } from "node:fs/promises";
2
2
import { buildNextjsApp } from "./build-next-app" ;
3
3
import { buildWorker } from "./build-worker" ;
4
4
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" ;
10
7
11
8
/**
12
9
* Builds the application in a format that can be passed to workerd
13
10
*
14
11
* It saves the output in a `.worker-next` directory
15
12
*
16
- * @param inputNextAppDir the directory of the Next.js app to build
13
+ * @param appDir the directory of the Next.js app to build
17
14
* @param opts.outputDir the directory where to save the output (defaults to the app's directory)
18
15
* @param opts.skipBuild boolean indicating whether the Next.js build should be skipped (i.e. if the `.next` dir is already built)
19
16
*/
20
- export async function build ( inputNextAppDir : string , opts : BuildOptions ) : Promise < void > {
17
+ export async function build ( appDir : string , opts : BuildOptions ) : Promise < void > {
21
18
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 ) ;
37
21
}
38
22
39
- const outputDir = `${ opts . outputDir ?? inputNextAppDir } /.worker-next` ;
23
+ // Create a clean output directory
24
+ const outputDir = resolve ( opts . outputDir ?? appDir , ".worker-next" ) ;
40
25
await cleanDirectory ( outputDir ) ;
41
26
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 ) ;
45
30
46
- await buildWorker ( inputNextAppDir , outputDir , nextjsAppPaths , templateDir ) ;
31
+ await buildWorker ( appDir , outputDir , nextjsAppPaths ) ;
47
32
}
48
33
49
34
type BuildOptions = {
0 commit comments