-
Notifications
You must be signed in to change notification settings - Fork 113
/
build.ts
executable file
·763 lines (675 loc) · 23.3 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import cp from "node:child_process";
import fs, { readFileSync } from "node:fs";
import { createRequire as topLevelCreateRequire } from "node:module";
import os from "node:os";
import path from "node:path";
import url from "node:url";
import { MiddlewareManifest } from "types/next-types.js";
import { isBinaryContentType } from "./adapters/binary.js";
import {
compileOpenNextConfigEdge,
compileOpenNextConfigNode,
} from "./build/compileConfig.js";
import { createServerBundle } from "./build/createServerBundle.js";
import { buildEdgeBundle } from "./build/edge/createEdgeBundle.js";
import { generateOutput } from "./build/generateOutput.js";
import {
BuildOptions,
compareSemver,
copyOpenNextConfig,
esbuildAsync,
esbuildSync,
getBuildId,
getHtmlPages,
normalizeOptions,
removeFiles,
traverseFiles,
} from "./build/helper.js";
import { validateConfig } from "./build/validateConfig.js";
import logger from "./logger.js";
import { openNextReplacementPlugin } from "./plugins/replacement.js";
import { openNextResolvePlugin } from "./plugins/resolve.js";
import { OpenNextConfig } from "./types/open-next.js";
const require = topLevelCreateRequire(import.meta.url);
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
let options: BuildOptions;
let config: OpenNextConfig;
export type PublicFiles = {
files: string[];
};
export async function build(
openNextConfigPath?: string,
nodeExternals?: string,
) {
showWindowsWarning();
// Load open-next.config.ts
const tempDir = initTempDir();
let configPath = compileOpenNextConfigNode(
tempDir,
openNextConfigPath,
nodeExternals,
);
// On Windows, we need to use file:// protocol to load the config file using import()
if (process.platform === "win32") configPath = `file://${configPath}`;
config = (await import(configPath)).default as OpenNextConfig;
if (!config || !config.default) {
logger.error(
`config.default cannot be empty, it should be at least {}, see more info here: https://open-next.js.org/config#configuration-file`,
);
process.exit(1);
}
validateConfig(config);
compileOpenNextConfigEdge(tempDir, config, openNextConfigPath);
const { root: monorepoRoot, packager } = findMonorepoRoot(
path.join(process.cwd(), config.appPath || "."),
);
// Initialize options
options = normalizeOptions(config, monorepoRoot);
logger.setLevel(options.debug ? "debug" : "info");
// Pre-build validation
checkRunningInsideNextjsApp();
printNextjsVersion();
printOpenNextVersion();
// Build Next.js app
printHeader("Building Next.js app");
setStandaloneBuildMode(monorepoRoot);
buildNextjsApp(packager);
// Generate deployable bundle
printHeader("Generating bundle");
initOutputDir(tempDir);
// Compile cache.ts
compileCache();
// Compile middleware
await createMiddleware();
createStaticAssets();
await createCacheAssets(monorepoRoot);
await createServerBundle(config, options);
await createRevalidationBundle(config);
await createImageOptimizationBundle(config);
await createWarmerBundle(config);
await generateOutput(options.appBuildOutputPath, config);
logger.info("OpenNext build complete.");
}
function showWindowsWarning() {
if (os.platform() !== "win32") return;
logger.warn("OpenNext is not fully compatible with Windows.");
logger.warn(
"For optimal performance, it is recommended to use Windows Subsystem for Linux (WSL).",
);
logger.warn(
"While OpenNext may function on Windows, it could encounter unpredictable failures during runtime.",
);
}
function initTempDir() {
const dir = path.join(process.cwd(), ".open-next");
const tempDir = path.join(dir, ".build");
fs.rmSync(dir, { recursive: true, force: true });
fs.mkdirSync(tempDir, { recursive: true });
return tempDir;
}
function checkRunningInsideNextjsApp() {
const { appPath } = options;
const extension = ["js", "cjs", "mjs", "ts"].find((ext) =>
fs.existsSync(path.join(appPath, `next.config.${ext}`)),
);
if (!extension) {
logger.error(
"Error: next.config.js not found. Please make sure you are running this command inside a Next.js app.",
);
process.exit(1);
}
}
function findMonorepoRoot(appPath: string) {
let currentPath = appPath;
while (currentPath !== "/") {
const found = [
{ file: "package-lock.json", packager: "npm" as const },
{ file: "yarn.lock", packager: "yarn" as const },
{ file: "pnpm-lock.yaml", packager: "pnpm" as const },
{ file: "bun.lockb", packager: "bun" as const },
].find((f) => fs.existsSync(path.join(currentPath, f.file)));
if (found) {
if (currentPath !== appPath) {
logger.info("Monorepo detected at", currentPath);
}
return { root: currentPath, packager: found.packager };
}
currentPath = path.dirname(currentPath);
}
// note: a lock file (package-lock.json, yarn.lock, or pnpm-lock.yaml) is
// not found in the app's directory or any of its parent directories.
// We are going to assume that the app is not part of a monorepo.
return { root: appPath, packager: "npm" as const };
}
function setStandaloneBuildMode(monorepoRoot: string) {
// Equivalent to setting `target: "standalone"` in next.config.js
process.env.NEXT_PRIVATE_STANDALONE = "true";
// Equivalent to setting `experimental.outputFileTracingRoot` in next.config.js
process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT = monorepoRoot;
}
function buildNextjsApp(packager: "npm" | "yarn" | "pnpm" | "bun") {
const { appPackageJsonPath } = options;
const command =
config.buildCommand ??
(["bun", "npm"].includes(packager)
? `${packager} run build`
: `${packager} build`);
cp.execSync(command, {
stdio: "inherit",
cwd: path.dirname(appPackageJsonPath),
});
}
function printHeader(header: string) {
header = `OpenNext — ${header}`;
logger.info(
[
"",
"┌" + "─".repeat(header.length + 2) + "┐",
`│ ${header} │`,
"└" + "─".repeat(header.length + 2) + "┘",
"",
].join("\n"),
);
}
function printNextjsVersion() {
const { nextVersion } = options;
logger.info(`Next.js version : ${nextVersion}`);
}
function printOpenNextVersion() {
const { openNextVersion } = options;
logger.info(`OpenNext v${openNextVersion}`);
}
function initOutputDir(tempDir: string) {
// We need to get the build relative to the cwd to find the compiled config
// This is needed for the case where the app is a single-version monorepo and the package.json is in the root of the monorepo
// where the build is in the app directory, but the compiled config is in the root of the monorepo.
const { outputDir, tempDir: lTempDir } = options;
const openNextConfig = readFileSync(
path.join(tempDir, "open-next.config.mjs"),
"utf8",
);
let openNextConfigEdge: string | null = null;
if (fs.existsSync(path.join(tempDir, "open-next.config.edge.mjs"))) {
openNextConfigEdge = readFileSync(
path.join(tempDir, "open-next.config.edge.mjs"),
"utf8",
);
}
fs.rmSync(outputDir, { recursive: true, force: true });
fs.mkdirSync(lTempDir, { recursive: true });
fs.writeFileSync(path.join(lTempDir, "open-next.config.mjs"), openNextConfig);
if (openNextConfigEdge) {
fs.writeFileSync(
path.join(lTempDir, "open-next.config.edge.mjs"),
openNextConfigEdge,
);
}
}
async function createWarmerBundle(config: OpenNextConfig) {
logger.info(`Bundling warmer function...`);
const { outputDir } = options;
// Create output folder
const outputPath = path.join(outputDir, "warmer-function");
fs.mkdirSync(outputPath, { recursive: true });
// Copy open-next.config.mjs into the bundle
copyOpenNextConfig(options.tempDir, outputPath);
// Build Lambda code
// note: bundle in OpenNext package b/c the adatper relys on the
// "serverless-http" package which is not a dependency in user's
// Next.js app.
await esbuildAsync(
{
entryPoints: [path.join(__dirname, "adapters", "warmer-function.js")],
external: ["next"],
outfile: path.join(outputPath, "index.mjs"),
plugins: [
openNextResolvePlugin({
overrides: {
converter: config.warmer?.override?.converter ?? "dummy",
wrapper: config.warmer?.override?.wrapper,
},
fnName: "warmer",
}),
],
banner: {
js: [
"import { createRequire as topLevelCreateRequire } from 'module';",
"const require = topLevelCreateRequire(import.meta.url);",
"import bannerUrl from 'url';",
"const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));",
].join(""),
},
},
options,
);
}
async function createRevalidationBundle(config: OpenNextConfig) {
logger.info(`Bundling revalidation function...`);
const { appBuildOutputPath, outputDir } = options;
// Create output folder
const outputPath = path.join(outputDir, "revalidation-function");
fs.mkdirSync(outputPath, { recursive: true });
//Copy open-next.config.mjs into the bundle
copyOpenNextConfig(options.tempDir, outputPath);
// Build Lambda code
await esbuildAsync(
{
external: ["next", "styled-jsx", "react"],
entryPoints: [path.join(__dirname, "adapters", "revalidate.js")],
outfile: path.join(outputPath, "index.mjs"),
plugins: [
openNextResolvePlugin({
fnName: "revalidate",
overrides: {
converter:
config.revalidate?.override?.converter ?? "sqs-revalidate",
wrapper: config.revalidate?.override?.wrapper,
},
}),
],
},
options,
);
// Copy over .next/prerender-manifest.json file
fs.copyFileSync(
path.join(appBuildOutputPath, ".next", "prerender-manifest.json"),
path.join(outputPath, "prerender-manifest.json"),
);
}
async function createImageOptimizationBundle(config: OpenNextConfig) {
logger.info(`Bundling image optimization function...`);
const { appPath, appBuildOutputPath, outputDir } = options;
// Create output folder
const outputPath = path.join(outputDir, "image-optimization-function");
fs.mkdirSync(outputPath, { recursive: true });
// Copy open-next.config.mjs into the bundle
copyOpenNextConfig(options.tempDir, outputPath);
const plugins = [
openNextResolvePlugin({
fnName: "imageOptimization",
overrides: {
converter: config.imageOptimization?.override?.converter,
wrapper: config.imageOptimization?.override?.wrapper,
imageLoader: config.imageOptimization?.loader,
},
}),
];
if (compareSemver(options.nextVersion, "14.1.1") >= 0) {
plugins.push(
openNextReplacementPlugin({
name: "opennext-14.1.1-image-optimization",
target:
/plugins(\/|\\)image-optimization(\/|\\)image-optimization\.js/g,
replacements: [
require.resolve(
"./adapters/plugins/image-optimization/image-optimization.replacement.js",
),
],
}),
);
}
// Build Lambda code (1st pass)
// note: bundle in OpenNext package b/c the adapter relies on the
// "@aws-sdk/client-s3" package which is not a dependency in user's
// Next.js app.
await esbuildAsync(
{
entryPoints: [
path.join(__dirname, "adapters", "image-optimization-adapter.js"),
],
external: ["sharp", "next"],
outfile: path.join(outputPath, "index.mjs"),
plugins,
},
options,
);
// Build Lambda code (2nd pass)
// note: bundle in user's Next.js app again b/c the adapter relies on the
// "next" package. And the "next" package from user's app should
// be used.
esbuildSync(
{
entryPoints: [path.join(outputPath, "index.mjs")],
external: ["sharp"],
allowOverwrite: true,
outfile: path.join(outputPath, "index.mjs"),
banner: {
js: [
"import { createRequire as topLevelCreateRequire } from 'module';",
"const require = topLevelCreateRequire(import.meta.url);",
"import bannerUrl from 'url';",
"const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));",
].join("\n"),
},
},
options,
);
// Copy over .next/required-server-files.json file and BUILD_ID
fs.mkdirSync(path.join(outputPath, ".next"));
fs.copyFileSync(
path.join(appBuildOutputPath, ".next/required-server-files.json"),
path.join(outputPath, ".next/required-server-files.json"),
);
fs.copyFileSync(
path.join(appBuildOutputPath, ".next/BUILD_ID"),
path.join(outputPath, ".next/BUILD_ID"),
);
// Sharp provides pre-build binaries for all platforms. https://github.com/lovell/sharp/blob/main/docs/install.md#cross-platform
// Target should be same as used by Lambda, see https://github.com/sst/sst/blob/ca6f763fdfddd099ce2260202d0ce48c72e211ea/packages/sst/src/constructs/NextjsSite.ts#L114
// For SHARP_IGNORE_GLOBAL_LIBVIPS see: https://github.com/lovell/sharp/blob/main/docs/install.md#aws-lambda
const nodeOutputPath = path.resolve(outputPath);
const sharpVersion = process.env.SHARP_VERSION ?? "0.32.6";
const arch = config.imageOptimization?.arch ?? "arm64";
const nodeVersion = config.imageOptimization?.nodeVersion ?? "18";
//check if we are running in Windows environment then set env variables accordingly.
try {
cp.execSync(
// We might want to change the arch args to cpu args, it seems to be the documented way
`npm install --arch=${arch} --platform=linux --target=${nodeVersion} --libc=glibc --prefix="${nodeOutputPath}" sharp@${sharpVersion}`,
{
stdio: "pipe",
cwd: appPath,
env: {
...process.env,
SHARP_IGNORE_GLOBAL_LIBVIPS: "1",
},
},
);
} catch (e: any) {
logger.error(e.stdout.toString());
logger.error(e.stderr.toString());
logger.error("Failed to install sharp.");
}
}
function createStaticAssets() {
logger.info(`Bundling static assets...`);
const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options;
// Create output folder
const outputPath = path.join(outputDir, "assets");
fs.mkdirSync(outputPath, { recursive: true });
// Next.js outputs assets into multiple files. Copy into the same directory.
// Copy over:
// - .next/BUILD_ID => _next/BUILD_ID
// - .next/static => _next/static
// - public/* => *
// - app/favicon.ico or src/app/favicon.ico => favicon.ico
fs.copyFileSync(
path.join(appBuildOutputPath, ".next/BUILD_ID"),
path.join(outputPath, "BUILD_ID"),
);
fs.cpSync(
path.join(appBuildOutputPath, ".next/static"),
path.join(outputPath, "_next", "static"),
{ recursive: true },
);
if (fs.existsSync(appPublicPath)) {
fs.cpSync(appPublicPath, outputPath, { recursive: true });
}
const appSrcPath = fs.existsSync(path.join(appPath, "src"))
? "src/app"
: "app";
const faviconPath = path.join(appPath, appSrcPath, "favicon.ico");
if (fs.existsSync(faviconPath)) {
fs.copyFileSync(faviconPath, path.join(outputPath, "favicon.ico"));
}
}
async function createCacheAssets(monorepoRoot: string) {
if (config.dangerous?.disableIncrementalCache) return;
logger.info(`Bundling cache assets...`);
const { appBuildOutputPath, outputDir } = options;
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
const buildId = getBuildId(appBuildOutputPath);
// Copy pages to cache folder
const dotNextPath = path.join(
appBuildOutputPath,
".next/standalone",
packagePath,
);
const outputPath = path.join(outputDir, "cache", buildId);
[".next/server/pages", ".next/server/app"]
.map((dir) => path.join(dotNextPath, dir))
.filter(fs.existsSync)
.forEach((dir) => fs.cpSync(dir, outputPath, { recursive: true }));
// Remove non-cache files
const htmlPages = getHtmlPages(dotNextPath);
removeFiles(
outputPath,
(file) =>
file.endsWith(".js") ||
file.endsWith(".js.nft.json") ||
(file.endsWith(".html") && htmlPages.has(file)),
);
//merge cache files into a single file
const cacheFilesPath: Record<
string,
{
meta?: string;
html?: string;
json?: string;
rsc?: string;
body?: string;
}
> = {};
traverseFiles(
outputPath,
() => true,
(filepath) => {
const ext = path.extname(filepath);
const newFilePath =
ext !== "" ? filepath.replace(ext, ".cache") : `${filepath}.cache`;
switch (ext) {
case ".meta":
case ".html":
case ".json":
case ".body":
case ".rsc":
cacheFilesPath[newFilePath] = {
[ext.slice(1)]: filepath,
...cacheFilesPath[newFilePath],
};
break;
case ".map":
break;
default:
logger.warn(`Unknown file extension: ${ext}`);
break;
}
},
);
// Generate cache file
Object.entries(cacheFilesPath).forEach(([cacheFilePath, files]) => {
const cacheFileMeta = files.meta
? JSON.parse(fs.readFileSync(files.meta, "utf8"))
: undefined;
const cacheFileContent = {
type: files.body ? "route" : files.json ? "page" : "app",
meta: cacheFileMeta,
html: files.html ? fs.readFileSync(files.html, "utf8") : undefined,
json: files.json
? JSON.parse(fs.readFileSync(files.json, "utf8"))
: undefined,
rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined,
body: files.body
? fs
.readFileSync(files.body)
.toString(
isBinaryContentType(cacheFileMeta.headers["content-type"])
? "base64"
: "utf8",
)
: undefined,
};
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
});
if (!config.dangerous?.disableTagCache) {
// Generate dynamodb data
// We need to traverse the cache to find every .meta file
const metaFiles: {
tag: { S: string };
path: { S: string };
revalidatedAt: { N: string };
}[] = [];
// Compute dynamodb cache data
// Traverse files inside cache to find all meta files and cache tags associated with them
traverseFiles(
outputPath,
(file) => file.endsWith(".meta"),
(filePath) => {
const fileContent = fs.readFileSync(filePath, "utf8");
const fileData = JSON.parse(fileContent);
if (fileData.headers?.["x-next-cache-tags"]) {
fileData.headers["x-next-cache-tags"]
.split(",")
.forEach((tag: string) => {
// TODO: We should split the tag using getDerivedTags from next.js or maybe use an in house implementation
metaFiles.push({
tag: { S: path.posix.join(buildId, tag.trim()) },
path: {
S: path.posix.join(
buildId,
path.relative(outputPath, filePath).replace(".meta", ""),
),
},
revalidatedAt: { N: `${Date.now()}` },
});
});
}
},
);
// Copy fetch-cache to cache folder
const fetchCachePath = path.join(
appBuildOutputPath,
".next/cache/fetch-cache",
);
if (fs.existsSync(fetchCachePath)) {
const fetchOutputPath = path.join(outputDir, "cache", "__fetch", buildId);
fs.mkdirSync(fetchOutputPath, { recursive: true });
fs.cpSync(fetchCachePath, fetchOutputPath, { recursive: true });
traverseFiles(
fetchCachePath,
() => true,
(filepath) => {
const fileContent = fs.readFileSync(filepath, "utf8");
const fileData = JSON.parse(fileContent);
fileData?.tags?.forEach((tag: string) => {
metaFiles.push({
tag: { S: path.posix.join(buildId, tag) },
path: {
S: path.posix.join(
buildId,
path.relative(fetchCachePath, filepath),
),
},
revalidatedAt: { N: `${Date.now()}` },
});
});
},
);
}
if (metaFiles.length > 0) {
const providerPath = path.join(outputDir, "dynamodb-provider");
await esbuildAsync(
{
external: ["@aws-sdk/client-dynamodb"],
entryPoints: [path.join(__dirname, "adapters", "dynamo-provider.js")],
outfile: path.join(providerPath, "index.mjs"),
target: ["node18"],
plugins: [
openNextResolvePlugin({
fnName: "initializationFunction",
overrides: {
converter:
config.initializationFunction?.override?.converter ?? "dummy",
wrapper: config.initializationFunction?.override?.wrapper,
},
}),
],
},
options,
);
//Copy open-next.config.mjs into the bundle
copyOpenNextConfig(options.tempDir, providerPath);
// TODO: check if metafiles doesn't contain duplicates
fs.writeFileSync(
path.join(providerPath, "dynamodb-cache.json"),
JSON.stringify(metaFiles),
);
}
}
// We need to remove files later because we need the metafiles for dynamodb tags cache
removeFiles(outputPath, (file) => !file.endsWith(".cache"));
}
/***************************/
/* Server Helper Functions */
/***************************/
export function compileCache(format: "cjs" | "esm" = "cjs") {
const ext = format === "cjs" ? "cjs" : "mjs";
const outfile = path.join(options.outputDir, ".build", `cache.${ext}`);
esbuildSync(
{
external: ["next", "styled-jsx", "react", "@aws-sdk/*"],
entryPoints: [path.join(__dirname, "adapters", "cache.js")],
outfile,
target: ["node18"],
format,
banner: {
js: [
`globalThis.disableIncrementalCache = ${
config.dangerous?.disableIncrementalCache ?? false
};`,
`globalThis.disableDynamoDBCache = ${
config.dangerous?.disableTagCache ?? false
};`,
].join(""),
},
},
options,
);
return outfile;
}
async function createMiddleware() {
console.info(`Bundling middleware function...`);
const { appBuildOutputPath, outputDir } = options;
// Get middleware manifest
const middlewareManifest = JSON.parse(
readFileSync(
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
"utf8",
),
) as MiddlewareManifest;
const entry = middlewareManifest.middleware["/"];
if (!entry) {
return;
}
// Create output folder
let outputPath = path.join(outputDir, "server-function");
const commonMiddlewareOptions = {
middlewareInfo: entry,
options,
appBuildOutputPath,
};
if (config.middleware?.external) {
outputPath = path.join(outputDir, "middleware");
fs.mkdirSync(outputPath, { recursive: true });
// Copy open-next.config.mjs
copyOpenNextConfig(
options.tempDir,
outputPath,
config.middleware.override?.wrapper === "cloudflare",
);
// Bundle middleware
await buildEdgeBundle({
entrypoint: path.join(__dirname, "adapters", "middleware.js"),
outfile: path.join(outputPath, "handler.mjs"),
...commonMiddlewareOptions,
overrides: config.middleware?.override,
defaultConverter: "aws-cloudfront",
});
} else {
await buildEdgeBundle({
entrypoint: path.join(__dirname, "core", "edgeFunctionHandler.js"),
outfile: path.join(outputDir, ".build", "middleware.mjs"),
...commonMiddlewareOptions,
});
}
}