-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathload-manifest.ts
More file actions
345 lines (308 loc) · 11.3 KB
/
load-manifest.ts
File metadata and controls
345 lines (308 loc) · 11.3 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
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
/**
* Inline `loadManifest` and `evalManifest` from `load-manifest.js`
*
* They rely on `readFileSync` that is not supported by workerd.
*/
import crypto from "node:crypto";
import { glob, readFile } from "node:fs/promises";
import { join, posix, relative, sep } from "node:path";
import { Lang, parse, type SgNode } from "@ast-grep/napi";
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
import { applyRule, patchCode, type RuleConfig } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
import { normalizePath } from "../../../utils/normalize-path.js";
export function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOptions): Plugin {
return updater.updateContent("inline-load-manifest", [
{
filter: getCrossPlatformPathRegex(String.raw`/next/dist/server/load-manifest(\.external)?\.js$`, {
escape: false,
}),
contentFilter: /function loadManifest\(/,
callback: async ({ contents }) => {
contents = await patchCode(contents, await getLoadManifestRule(buildOpts));
contents = await patchCode(contents, await getEvalManifestRule(buildOpts));
return contents;
},
},
]);
}
async function getLoadManifestRule(buildOpts: BuildOptions) {
const { outputDir } = buildOpts;
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
const dotNextDir = join(baseDir, ".next");
const manifests = await Array.fromAsync(
glob("**/{*-manifest,required-server-files,prefetch-hints}.json", { cwd: dotNextDir })
);
const returnManifests = (
await Promise.all(
manifests.map(async (manifestPath) => {
const fullManifestPath = join(dotNextDir, manifestPath);
return `
if ($PATH.endsWith("${normalizePath("/" + manifestPath)}")) {
return ${await readFile(fullManifestPath, "utf-8")};
}`;
})
)
).join("\n");
return {
rule: {
pattern: `
function loadManifest($PATH, $$$ARGS) {
$$$_
}`,
},
fix: `
function loadManifest($PATH, $$$ARGS) {
$PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
if ($PATH.endsWith(".next/BUILD_ID")) {
return process.env.NEXT_BUILD_ID;
}
${returnManifests}
// Known optional manifests \u2014 Next.js loads these with handleMissing: true
// (see vercel/next.js packages/next/src/server/route-modules/route-module.ts).
// Return {} to match Next.js behaviour instead of crashing the worker.
// Note: Some manifest constants in Next.js omit the .json extension
// (e.g. SUBRESOURCE_INTEGRITY_MANIFEST, DYNAMIC_CSS_MANIFEST), so we
// strip .json before matching to handle both forms.
{
const p = $PATH.replace(/\\.json$/, "");
if (p.endsWith("react-loadable-manifest") ||
p.endsWith("subresource-integrity-manifest") ||
p.endsWith("server-reference-manifest") ||
p.endsWith("dynamic-css-manifest") ||
p.endsWith("fallback-build-manifest") ||
p.endsWith("prefetch-hints")) {
return {};
}
}
throw new Error(\`Unexpected loadManifest(\${$PATH}) call!\`);
}`,
} satisfies RuleConfig;
}
async function getEvalManifestRule(buildOpts: BuildOptions) {
const { outputDir } = buildOpts;
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next");
const appDir = join(baseDir, "server/app");
const manifestPaths = await Array.fromAsync(glob("**/*_client-reference-manifest.js", { cwd: baseDir }));
// Map of factored large objects (variable name -> {...})
const factoredObjects = new Map<string, string>();
// Map of manifest path -> factored manifest content
const factoredManifest = new Map<string, string>();
// Shared map of short hash prefix -> full SHA1 hash, used for collision resolution.
const prefixMap = new Map<string, string>();
for (const manifestPath of manifestPaths) {
const fullManifestPath = join(baseDir, manifestPath);
if (manifestPath.endsWith("page_client-reference-manifest.js")) {
// `page_client-reference-manifest.js` files could contain large repeated values.
// Factor out large values into separate variables to reduce the overall size of the generated code.
let manifest = await readFile(fullManifestPath, "utf-8");
for (const key of [
"clientModules",
"ssrModuleMapping",
"edgeSSRModuleMapping",
"rscModuleMapping",
"entryCSSFiles",
"entryJSFiles",
]) {
manifest = factorManifestValue(manifest, key, factoredObjects, prefixMap);
}
factoredManifest.set(manifestPath, manifest);
}
}
// Map of factored values in an object
const factoredValues = new Map<string, string>();
for (const [varName, value] of factoredObjects) {
factoredObjects.set(varName, factorObjectValues(value, factoredValues, prefixMap));
}
// Prepend chunks variable declarations before the factored values
const factoredValueCode = [...factoredValues.entries()]
.map(([name, val]) => `const ${name} = ${val};`)
.join("\n");
const factoredObjectCode = [...factoredObjects.entries()]
.map(([varName, value]) => `const ${varName} = ${value};`)
.join("\n");
const returnManifests = manifestPaths
// Sort by path length descending so longer (more specific) paths match first,
// preventing suffix collisions in the `.endsWith()` chain (see #1156).
.toSorted((a, b) => b.length - a.length)
.map((manifestPath) => {
const fullManifestPath = join(baseDir, manifestPath);
let manifest: string;
if (factoredManifest.has(manifestPath)) {
manifest = factoredManifest.get(manifestPath)!;
} else {
manifest = `require(${JSON.stringify(fullManifestPath)});`;
}
const endsWith = normalizePath(manifestPath);
const key = normalizePath("/" + relative(appDir, fullManifestPath)).replace(
"_client-reference-manifest.js",
""
);
return `
if ($PATH.endsWith("${endsWith}")) {
${manifest}
return {
__RSC_MANIFEST: {
"${key}": globalThis.__RSC_MANIFEST["${key}"],
},
};
}`;
})
.join("\n");
return {
rule: {
pattern: `
function evalManifest($PATH, $$$ARGS) {
$$$_
}`,
},
fix: `
${factoredValueCode}
${factoredObjectCode}
function evalManifest($PATH, $$$ARGS) {
$PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
${returnManifests}
// client-reference-manifest is optional for static metadata routes
// (see vercel/next.js route-module.ts, loaded with handleMissing: true)
if ($PATH.endsWith("_client-reference-manifest.js")) {
return { __RSC_MANIFEST: {} };
}
throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
}`,
} satisfies RuleConfig;
}
/**
* Factor out large manifest values into separate variables.
*
* @param manifest The manifest code.
* @param key The key to factor out.
* @param values A map to store the factored values (indexed by variable name).
* @param prefixMap Map of short hash prefix → full hash, updated in place for
* collision resolution across calls.
* @returns The manifest code with large values factored out.
*/
export function factorManifestValue(
manifest: string,
key: string,
values: Map<string, string>,
prefixMap: Map<string, string>
): string {
const valueName = "VALUE";
// ASTGrep rule to extract the value of a specific key from the manifest object in the evalManifest function.
//
// globalThis.__RSC_MANIFEST["/path/to/page"] = {
// // ...
// key: $VALUE
// // ...
// }
const extractValueRule = `
rule:
kind: pair
all:
- has:
field: key
pattern: '"${key}"'
- has:
field: value
pattern: $${valueName}
inside:
pattern: globalThis.__RSC_MANIFEST[$$$_] = { $$$ };
stopBy: end
`;
const rootNode = parse(Lang.JavaScript, manifest).root();
const { matches } = applyRule(extractValueRule, rootNode, { once: true });
if (matches.length === 1 && matches[0]?.getMatch(valueName)) {
const match = matches[0];
const value = match.getMatch(valueName)!.text();
if (value.length > 30) {
// Factor out large values into separate variables.
const varName = getOrCreateVarName(value, prefixMap);
values.set(varName, value);
// Replace the value in the manifest with the variable reference.
return rootNode.commitEdits([match.replace(`"${key}": ${varName}`)]);
}
}
// Return the original manifest if the value is not found or is small enough to not warrant factoring out.
return manifest;
}
/**
* Factor out large object values into separate variables.
*
* @param valueText The JS source text of the module mapping object.
* @param sharedVars Map to accumulate shared variable declarations.
* @param prefixMap Map of short hash prefix → full hash, updated in place for
* collision resolution across calls.
* @returns The rewritten value text with chunks arrays replaced by variable refs.
*/
export function factorObjectValues(
valueText: string,
sharedVars: Map<string, string>,
prefixMap: Map<string, string>
): string {
const rootNode = parse(Lang.JavaScript, valueText).root();
// Find all "chunks": [...] pairs
const chunksRule = `
rule:
kind: pair
all:
- has:
field: key
pattern: '"chunks"'
- has:
field: value
kind: array
pattern: $CHUNKS
`;
const { matches } = applyRule(chunksRule, rootNode, { once: false });
const edits: Array<{ match: SgNode; replacement: string }> = [];
for (const match of matches) {
const chunksNode = match.getMatch("CHUNKS");
if (!chunksNode) continue;
const chunksText = chunksNode.text();
if (chunksText.length <= 30) continue; // Skip small arrays
const varName = getOrCreateVarName(chunksText, prefixMap);
sharedVars.set(varName, chunksText);
edits.push({ match, replacement: `"chunks": ${varName}` });
}
return edits.length === 0
? valueText
: rootNode.commitEdits(edits.map((e) => e.match.replace(e.replacement)));
}
/** Minimum number of hex characters used for short hash prefixes. */
const MIN_PREFIX_LENGTH = 3;
/**
* Get or create a short variable name for a value, resolving collisions.
*
* Computes a SHA1 hash of the value, then finds the shortest unique prefix
* (minimum {@link MIN_PREFIX_LENGTH} hex chars). When a new hash collides with
* an existing prefix, the new entry is given a longer prefix — existing entries
* are never renamed.
*
* This allows saving space in the generated code (A full SHA1 is 40 hex chars) because
* identifiers are not minimized by the Open Next build process.
*
* @param value The value to hash.
* @param prefixMap Map of short prefix → full hash, updated in place.
* @returns The variable name (`v<shortPrefix>`).
*/
export function getOrCreateVarName(value: string, prefixMap: Map<string, string>): string {
const sha1 = crypto.createHash("sha1").update(value).digest("hex");
// Find the shortest prefix (>= MIN_PREFIX_LENGTH) that doesn't collide
// with any existing prefix. Only the new entry is lengthened.
for (let len = MIN_PREFIX_LENGTH; len <= sha1.length; len++) {
const candidate = sha1.slice(0, len);
const existing = prefixMap.get(candidate);
if (existing === undefined) {
prefixMap.set(candidate, sha1);
return `v${candidate}`;
}
if (existing === sha1) {
// Same content seen again — reuse the existing variable.
return `v${candidate}`;
}
// A different hash occupies this exact prefix — lengthen and retry.
}
// Unreachable: two different SHA1 hashes always diverge before 40 chars.
throw new Error("Failed to find a unique prefix");
}