|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import type { |
| 5 | + IncludedOriginResolver, |
| 6 | + LazyLoadedOverride, |
| 7 | + OverrideOptions, |
| 8 | +} from "types/open-next.js"; |
| 9 | +import type { OriginResolver } from "types/overrides.js"; |
| 10 | +import { getCrossPlatformPathRegex } from "utils/regex.js"; |
| 11 | +import { openNextExternalMiddlewarePlugin } from "../../plugins/externalMiddleware.js"; |
| 12 | +import { openNextReplacementPlugin } from "../../plugins/replacement.js"; |
| 13 | +import { openNextResolvePlugin } from "../../plugins/resolve.js"; |
| 14 | +import { copyTracedFiles } from "../copyTracedFiles.js"; |
| 15 | +import * as buildHelper from "../helper.js"; |
| 16 | +import { installDependencies } from "../installDeps.js"; |
| 17 | + |
| 18 | +type Override = OverrideOptions & { |
| 19 | + originResolver?: LazyLoadedOverride<OriginResolver> | IncludedOriginResolver; |
| 20 | +}; |
| 21 | + |
| 22 | +export async function buildExternalNodeMiddleware( |
| 23 | + options: buildHelper.BuildOptions, |
| 24 | +) { |
| 25 | + const { appBuildOutputPath, config, outputDir } = options; |
| 26 | + if (!config.middleware?.external) { |
| 27 | + throw new Error( |
| 28 | + "This function should only be called for external middleware", |
| 29 | + ); |
| 30 | + } |
| 31 | + const outputPath = path.join(outputDir, "middleware"); |
| 32 | + fs.mkdirSync(outputPath, { recursive: true }); |
| 33 | + |
| 34 | + // Copy open-next.config.mjs |
| 35 | + buildHelper.copyOpenNextConfig( |
| 36 | + options.buildDir, |
| 37 | + outputPath, |
| 38 | + await buildHelper.isEdgeRuntime(config.middleware.override), |
| 39 | + ); |
| 40 | + const overrides = { |
| 41 | + ...config.middleware.override, |
| 42 | + originResolver: config.middleware.originResolver, |
| 43 | + }; |
| 44 | + const includeCache = config.dangerous?.enableCacheInterception; |
| 45 | + const packagePath = buildHelper.getPackagePath(options); |
| 46 | + |
| 47 | + // TODO: change this so that we don't copy unnecessary files |
| 48 | + await copyTracedFiles({ |
| 49 | + buildOutputPath: appBuildOutputPath, |
| 50 | + packagePath, |
| 51 | + outputDir: outputPath, |
| 52 | + routes: [], |
| 53 | + bundledNextServer: false, |
| 54 | + skipServerFiles: true, |
| 55 | + }); |
| 56 | + |
| 57 | + function override<T extends keyof Override>(target: T) { |
| 58 | + return typeof overrides?.[target] === "string" |
| 59 | + ? overrides[target] |
| 60 | + : undefined; |
| 61 | + } |
| 62 | + |
| 63 | + // Bundle middleware |
| 64 | + await buildHelper.esbuildAsync( |
| 65 | + { |
| 66 | + entryPoints: [ |
| 67 | + path.join(options.openNextDistDir, "adapters", "middleware.js"), |
| 68 | + ], |
| 69 | + outfile: path.join(outputPath, "handler.mjs"), |
| 70 | + external: ["./.next/*"], |
| 71 | + platform: "node", |
| 72 | + plugins: [ |
| 73 | + openNextResolvePlugin({ |
| 74 | + overrides: { |
| 75 | + wrapper: override("wrapper") ?? "aws-lambda", |
| 76 | + converter: override("converter") ?? "aws-cloudfront", |
| 77 | + ...(includeCache |
| 78 | + ? { |
| 79 | + tagCache: override("tagCache") ?? "dynamodb-lite", |
| 80 | + incrementalCache: override("incrementalCache") ?? "s3-lite", |
| 81 | + queue: override("queue") ?? "sqs-lite", |
| 82 | + } |
| 83 | + : {}), |
| 84 | + originResolver: override("originResolver") ?? "pattern-env", |
| 85 | + proxyExternalRequest: override("proxyExternalRequest") ?? "node", |
| 86 | + }, |
| 87 | + fnName: "middleware", |
| 88 | + }), |
| 89 | + openNextReplacementPlugin({ |
| 90 | + name: "externalMiddlewareOverrides", |
| 91 | + target: getCrossPlatformPathRegex("adapters/middleware.js"), |
| 92 | + deletes: includeCache ? [] : ["includeCacheInMiddleware"], |
| 93 | + }), |
| 94 | + openNextExternalMiddlewarePlugin( |
| 95 | + path.join( |
| 96 | + options.openNextDistDir, |
| 97 | + "core", |
| 98 | + "nodeMiddlewareHandler.js", |
| 99 | + ), |
| 100 | + ), |
| 101 | + ], |
| 102 | + banner: { |
| 103 | + js: [ |
| 104 | + `globalThis.monorepoPackagePath = '${packagePath}';`, |
| 105 | + "import process from 'node:process';", |
| 106 | + "import { Buffer } from 'node:buffer';", |
| 107 | + "import { AsyncLocalStorage } from 'node:async_hooks';", |
| 108 | + "import { createRequire as topLevelCreateRequire } from 'module';", |
| 109 | + "const require = topLevelCreateRequire(import.meta.url);", |
| 110 | + "import bannerUrl from 'url';", |
| 111 | + "const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));", |
| 112 | + ].join(""), |
| 113 | + }, |
| 114 | + }, |
| 115 | + options, |
| 116 | + ); |
| 117 | + |
| 118 | + // Do we need to copy or do something with env file here? |
| 119 | + |
| 120 | + installDependencies(outputPath, config.middleware?.install); |
| 121 | +} |
| 122 | + |
| 123 | +export async function buildBundledNodeMiddleware( |
| 124 | + options: buildHelper.BuildOptions, |
| 125 | +) { |
| 126 | + await buildHelper.esbuildAsync( |
| 127 | + { |
| 128 | + entryPoints: [ |
| 129 | + path.join(options.openNextDistDir, "core/nodeMiddlewareHandler.js"), |
| 130 | + ], |
| 131 | + external: ["./.next/*"], |
| 132 | + outfile: path.join(options.buildDir, "middleware.mjs"), |
| 133 | + bundle: true, |
| 134 | + platform: "node", |
| 135 | + }, |
| 136 | + options, |
| 137 | + ); |
| 138 | +} |
0 commit comments