-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathbundle-server.ts
More file actions
234 lines (211 loc) · 9.07 KB
/
bundle-server.ts
File metadata and controls
234 lines (211 loc) · 9.07 KB
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
import fs from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { Lang, parse } from "@ast-grep/napi";
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
import { build, Plugin } from "esbuild";
import { patchOptionalDependencies } from "./patches/ast/optional-deps.js";
import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js";
import * as patches from "./patches/index.js";
import fixRequire from "./patches/plugins/require.js";
import inlineRequirePagePlugin from "./patches/plugins/require-page.js";
import setWranglerExternal from "./patches/plugins/wrangler-external.js";
import { normalizePath, patchCodeWithValidations } from "./utils/index.js";
/** The dist directory of the Cloudflare adapter package */
const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../..");
/**
* List of optional Next.js dependencies.
* They are not required for Next.js to run but only needed to enabled specific features.
* When one of those dependency is required, it should be installed by the application.
*/
const optionalDependencies = [
"caniuse-lite",
"critters",
"jimp",
"probe-image-size",
// `server.edge` is not available in react-dom@18
"react-dom/server.edge",
];
/**
* Bundle the Open Next server.
*/
export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
patches.copyPackageCliFiles(packageDistDir, buildOpts);
const { appPath, outputDir, monorepoRoot } = buildOpts;
const serverFiles = path.join(
outputDir,
"server-functions/default",
getPackagePath(buildOpts),
".next/required-server-files.json"
);
const nextConfig = JSON.parse(fs.readFileSync(serverFiles, "utf-8")).config;
console.log(`\x1b[35m⚙️ Bundling the OpenNext server...\n\x1b[0m`);
await patches.updateWebpackChunksFile(buildOpts);
patchVercelOgLibrary(buildOpts);
const outputPath = path.join(outputDir, "server-functions", "default");
const packagePath = getPackagePath(buildOpts);
const openNextServer = path.join(outputPath, packagePath, `index.mjs`);
const openNextServerBundle = path.join(outputPath, packagePath, `handler.mjs`);
const result = await build({
entryPoints: [openNextServer],
bundle: true,
outfile: openNextServerBundle,
format: "esm",
target: "esnext",
minify: false,
metafile: true,
plugins: [
createFixRequiresESBuildPlugin(buildOpts),
inlineRequirePagePlugin(buildOpts),
setWranglerExternal(),
fixRequire(),
],
external: ["./middleware/handler.mjs", ...optionalDependencies],
alias: {
// Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:
// eval("require")("bufferutil");
// eval("require")("utf-8-validate");
"next/dist/compiled/ws": path.join(buildOpts.outputDir, "cloudflare-templates/shims/empty.js"),
// Note: we apply an empty shim to next/dist/compiled/edge-runtime since (amongst others) it generated the following `eval`:
// eval(getModuleCode)(module, module.exports, throwingRequire, params.context, ...Object.values(params.scopedContext));
// which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63
// QUESTION: Why did I encountered this but mhart didn't?
"next/dist/compiled/edge-runtime": path.join(
buildOpts.outputDir,
"cloudflare-templates/shims/empty.js"
),
// `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here
// source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env
"@next/env": path.join(buildOpts.outputDir, "cloudflare-templates/shims/env.js"),
},
define: {
// config file used by Next.js, see: https://github.com/vercel/next.js/blob/68a7128/packages/next/src/build/utils.ts#L2137-L2139
"process.env.__NEXT_PRIVATE_STANDALONE_CONFIG": `${JSON.stringify(nextConfig)}`,
// Next.js tried to access __dirname so we need to define it
__dirname: '""',
// Note: we need the __non_webpack_require__ variable declared as it is used by next-server:
// https://github.com/vercel/next.js/blob/be0c3283/packages/next/src/server/next-server.ts#L116-L119
__non_webpack_require__: "require",
// Ask mhart if he can explain why the `define`s below are necessary
"process.env.NEXT_RUNTIME": '"nodejs"',
"process.env.NODE_ENV": '"production"',
"process.env.NEXT_MINIMAL": "true",
},
platform: "node",
banner: {
js: `
// __dirname is used by unbundled js files (which don't inherit the __dirname present in the define field)
// so we also need to set it on the global scope
// Note: this was hit in the next/dist/compiled/@opentelemetry/api module
globalThis.__dirname ??= "";
// Do not crash on cache not supported
// https://github.com/cloudflare/workerd/pull/2434
// compatibility flag "cache_option_enabled" -> does not support "force-cache"
const curFetch = globalThis.fetch;
globalThis.fetch = (input, init) => {
if (init) {
delete init.cache;
}
return curFetch(input, init);
};
import __cf_stream from 'node:stream';
fetch = globalThis.fetch;
const CustomRequest = class extends globalThis.Request {
constructor(input, init) {
if (init) {
delete init.cache;
// https://github.com/cloudflare/workerd/issues/2746
// https://github.com/cloudflare/workerd/issues/3245
Object.defineProperty(init, "body", {
value: init.body instanceof __cf_stream.Readable ? ReadableStream.from(init.body) : init.body
});
}
super(input, init);
}
};
globalThis.Request = CustomRequest;
Request = globalThis.Request;
// Makes the edge converter returns either a Response or a Request.
globalThis.__dangerous_ON_edge_converter_returns_request = true;
globalThis.__BUILD_TIMESTAMP_MS__ = ${Date.now()};
`,
},
});
if (result.errors.length > 0) {
result.errors.forEach((error) => console.error(error));
throw new Error(`There was a problem bundling the server.`);
}
fs.writeFileSync(openNextServerBundle + ".meta.json", JSON.stringify(result.metafile, null, 2));
await updateWorkerBundledCode(openNextServerBundle, buildOpts);
const isMonorepo = monorepoRoot !== appPath;
if (isMonorepo) {
fs.writeFileSync(
path.join(outputPath, "handler.mjs"),
`export * from "./${normalizePath(packagePath)}/handler.mjs";`
);
}
console.log(`\x1b[35mWorker saved in \`${getOutputWorkerPath(buildOpts)}\` 🚀\n\x1b[0m`);
}
/**
* This function applies patches required for the code to run on workers.
*/
export async function updateWorkerBundledCode(
workerOutputFile: string,
buildOpts: BuildOptions
): Promise<void> {
const code = await readFile(workerOutputFile, "utf8");
const patchedCode = await patchCodeWithValidations(code, [
["require", patches.patchRequire],
["`buildId` function", (code) => patches.patchBuildId(code, buildOpts)],
["`loadManifest` function", (code) => patches.patchLoadManifest(code, buildOpts)],
["`findDir` function", (code) => patches.patchFindDir(code, buildOpts)],
["`evalManifest` function", (code) => patches.inlineEvalManifest(code, buildOpts)],
["cacheHandler", (code) => patches.patchCache(code, buildOpts)],
[
"'require(this.middlewareManifestPath)'",
(code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
],
["exception bubbling", patches.patchExceptionBubbling],
["`loadInstrumentationModule` function", patches.patchLoadInstrumentationModule],
[
"`patchAsyncStorage` call",
(code) =>
code
// TODO: implement for cf (possibly in @opennextjs/aws)
.replace("patchAsyncStorage();", "//patchAsyncStorage();"),
],
[
"`require.resolve` call",
// workers do not support dynamic require nor require.resolve
(code) => code.replace('require.resolve("./cache.cjs")', '"unused"'),
],
]);
const bundle = parse(Lang.TypeScript, patchedCode).root();
const { edits } = patchOptionalDependencies(bundle, optionalDependencies);
await writeFile(workerOutputFile, bundle.commitEdits(edits));
}
function createFixRequiresESBuildPlugin(options: BuildOptions): Plugin {
return {
name: "replaceRelative",
setup(build) {
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
build.onResolve(
{ filter: getCrossPlatformPathRegex(String.raw`^\./require-hook$`, { escape: false }) },
() => ({
path: path.join(options.outputDir, "cloudflare-templates/shims/empty.js"),
})
);
},
};
}
/**
* Gets the path of the worker.js file generated by the build process
*
* @param buildOpts the open-next build options
* @returns the path of the worker.js file that the build process generates
*/
export function getOutputWorkerPath(buildOpts: BuildOptions): string {
return path.join(buildOpts.outputDir, "worker.js");
}