@@ -3,22 +3,27 @@ import url from "node:url";
3
3
import path from "node:path" ;
4
4
import cp from "node:child_process" ;
5
5
import { minifyAll } from "./minimize-js.js" ;
6
- import { buildSync , BuildOptions } from "esbuild" ;
7
- import { exit } from "node:process" ;
6
+ import { buildSync , BuildOptions as ESBuildOptions } from "esbuild" ;
8
7
import { createRequire as topLevelCreateRequire } from "node:module" ;
9
8
9
+ interface BuildOptions {
10
+ minify ?: boolean ;
11
+ debug ?: boolean ;
12
+ appPath ?: string ;
13
+ }
14
+
10
15
const require = topLevelCreateRequire ( import . meta. url ) ;
11
16
const __dirname = url . fileURLToPath ( new URL ( "." , import . meta. url ) ) ;
12
- const appPath = process . cwd ( ) ;
13
- const appPublicPath = path . join ( appPath , "public" ) ;
14
- const outputDir = ".open-next" ;
15
- const tempDir = path . join ( outputDir , ".build" ) ;
17
+ let options : ReturnType < typeof normalizeOptions > ;
16
18
17
19
export type PublicFiles = {
18
20
files : string [ ] ;
19
21
} ;
20
22
21
- export async function build ( ) {
23
+ export async function build ( opts : BuildOptions = { } ) {
24
+ // Initialize options
25
+ options = normalizeOptions ( opts ) ;
26
+
22
27
// Pre-build validation
23
28
printNextjsVersion ( ) ;
24
29
printOpenNextVersion ( ) ;
@@ -37,12 +42,26 @@ export async function build() {
37
42
createServerBundle ( monorepoRoot ) ;
38
43
createImageOptimizationBundle ( ) ;
39
44
createWarmerBundle ( ) ;
40
- if ( process . env . OPEN_NEXT_MINIFY ) {
45
+ if ( options . minify ) {
41
46
await minifyServerBundle ( ) ;
42
47
}
43
48
}
44
49
50
+ function normalizeOptions ( opts : BuildOptions ) {
51
+ const appPath = opts . appPath ?? process . cwd ( ) ;
52
+ const outputDir = ".open-next" ;
53
+ return {
54
+ appPath,
55
+ appPublicPath : path . join ( appPath , "public" ) ,
56
+ outputDir,
57
+ tempDir : path . join ( outputDir , ".build" ) ,
58
+ minify : opts . minify ?? Boolean ( process . env . OPEN_NEXT_MINIFY ) ?? false ,
59
+ debug : opts . debug ?? Boolean ( process . env . OPEN_NEXT_DEBUG ) ?? false ,
60
+ } ;
61
+ }
62
+
45
63
function checkRunningInsideNextjsApp ( ) {
64
+ const { appPath } = options ;
46
65
const extension = [ "js" , "cjs" , "mjs" ] . find ( ( ext ) =>
47
66
fs . existsSync ( path . join ( appPath , `next.config.${ ext } ` ) )
48
67
) ;
@@ -55,6 +74,7 @@ function checkRunningInsideNextjsApp() {
55
74
}
56
75
57
76
function findMonorepoRoot ( ) {
77
+ const { appPath } = options ;
58
78
let currentPath = appPath ;
59
79
while ( currentPath !== "/" ) {
60
80
const found = [
@@ -86,6 +106,7 @@ function setStandaloneBuildMode(monorepoRoot: string) {
86
106
}
87
107
88
108
function buildNextjsApp ( packager : "npm" | "yarn" | "pnpm" ) {
109
+ const { appPath } = options ;
89
110
const result = cp . spawnSync (
90
111
packager ,
91
112
packager === "npm" ? [ "run" , "build" ] : [ "build" ] ,
@@ -114,6 +135,7 @@ function printHeader(header: string) {
114
135
}
115
136
116
137
function printNextjsVersion ( ) {
138
+ const { appPath } = options ;
117
139
cp . spawnSync (
118
140
"node" ,
119
141
[
@@ -167,11 +189,13 @@ function injectMiddlewareGeolocation(outputPath: string, packagePath: string) {
167
189
}
168
190
169
191
function initOutputDir ( ) {
192
+ const { outputDir, tempDir } = options ;
170
193
fs . rmSync ( outputDir , { recursive : true , force : true } ) ;
171
194
fs . mkdirSync ( tempDir , { recursive : true } ) ;
172
195
}
173
196
174
197
function listPublicFiles ( ) {
198
+ const { appPublicPath } = options ;
175
199
const result : PublicFiles = { files : [ ] } ;
176
200
177
201
if ( ! fs . existsSync ( appPublicPath ) ) {
@@ -197,6 +221,8 @@ function listPublicFiles() {
197
221
function createServerBundle ( monorepoRoot : string ) {
198
222
console . info ( `Bundling server function...` ) ;
199
223
224
+ const { appPath, outputDir } = options ;
225
+
200
226
// Create output folder
201
227
const outputPath = path . join ( outputDir , "server-function" ) ;
202
228
fs . mkdirSync ( outputPath , { recursive : true } ) ;
@@ -267,6 +293,8 @@ function createServerBundle(monorepoRoot: string) {
267
293
function createWarmerBundle ( ) {
268
294
console . info ( `Bundling warmer function...` ) ;
269
295
296
+ const { outputDir } = options ;
297
+
270
298
// Create output folder
271
299
const outputPath = path . join ( outputDir , "warmer-function" ) ;
272
300
fs . mkdirSync ( outputPath , { recursive : true } ) ;
@@ -292,6 +320,7 @@ function createWarmerBundle() {
292
320
293
321
async function minifyServerBundle ( ) {
294
322
console . info ( `Minimizing server function...` ) ;
323
+ const { outputDir } = options ;
295
324
await minifyAll ( path . join ( outputDir , "server-function" ) , {
296
325
compress_json : true ,
297
326
mangle : true ,
@@ -301,6 +330,8 @@ async function minifyServerBundle() {
301
330
function createImageOptimizationBundle ( ) {
302
331
console . info ( `Bundling image optimization function...` ) ;
303
332
333
+ const { appPath, outputDir } = options ;
334
+
304
335
// Create output folder
305
336
const outputPath = path . join ( outputDir , "image-optimization-function" ) ;
306
337
fs . mkdirSync ( outputPath , { recursive : true } ) ;
@@ -354,6 +385,8 @@ function createImageOptimizationBundle() {
354
385
function createAssets ( ) {
355
386
console . info ( `Bundling assets...` ) ;
356
387
388
+ const { appPath, appPublicPath, outputDir } = options ;
389
+
357
390
// Create output folder
358
391
const outputPath = path . join ( outputDir , "assets" ) ;
359
392
fs . mkdirSync ( outputPath , { recursive : true } ) ;
@@ -377,18 +410,19 @@ function createAssets() {
377
410
}
378
411
}
379
412
380
- function esbuildSync ( options : BuildOptions ) {
413
+ function esbuildSync ( esbuildOptions : ESBuildOptions ) {
414
+ const { debug } = options ;
381
415
const result = buildSync ( {
382
416
target : "esnext" ,
383
417
format : "esm" ,
384
418
platform : "node" ,
385
419
bundle : true ,
386
- minify : process . env . OPEN_NEXT_DEBUG ? false : true ,
387
- sourcemap : process . env . OPEN_NEXT_DEBUG ? "inline" : false ,
388
- ...options ,
420
+ minify : debug ? false : true ,
421
+ sourcemap : debug ? "inline" : false ,
422
+ ...esbuildOptions ,
389
423
// "process.env.OPEN_NEXT_DEBUG" determines if the logger writes to console.log
390
424
define : {
391
- ...options . define ,
425
+ ...esbuildOptions . define ,
392
426
"process.env.OPEN_NEXT_DEBUG" : process . env . OPEN_NEXT_DEBUG
393
427
? "true"
394
428
: "false" ,
@@ -398,7 +432,9 @@ function esbuildSync(options: BuildOptions) {
398
432
if ( result . errors . length > 0 ) {
399
433
result . errors . forEach ( ( error ) => console . error ( error ) ) ;
400
434
throw new Error (
401
- `There was a problem bundling ${ ( options . entryPoints as string [ ] ) [ 0 ] } .`
435
+ `There was a problem bundling ${
436
+ ( esbuildOptions . entryPoints as string [ ] ) [ 0 ]
437
+ } .`
402
438
) ;
403
439
}
404
440
}
0 commit comments