1
1
/**
2
- * Inline `loadManifest` as it relies on `readFileSync` that is not supported by workerd.
2
+ * Inline `loadManifest` and `evalManifest` from `load-manifest.js`
3
+ *
4
+ * They rely on `readFileSync` that is not supported by workerd.
3
5
*/
4
6
5
7
import { readFile } from "node:fs/promises" ;
@@ -21,13 +23,17 @@ export function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOpti
21
23
escape : false ,
22
24
} ) ,
23
25
contentFilter : / f u n c t i o n l o a d M a n i f e s t \( / ,
24
- callback : async ( { contents } ) => patchCode ( contents , await getRule ( buildOpts ) ) ,
26
+ callback : async ( { contents } ) => {
27
+ contents = await patchCode ( contents , await getLoadManifestRule ( buildOpts ) ) ;
28
+ contents = await patchCode ( contents , await getEvalManifestRule ( buildOpts ) ) ;
29
+ return contents ;
30
+ } ,
25
31
} ,
26
32
} ,
27
33
] ) ;
28
34
}
29
35
30
- async function getRule ( buildOpts : BuildOptions ) {
36
+ async function getLoadManifestRule ( buildOpts : BuildOptions ) {
31
37
const { outputDir } = buildOpts ;
32
38
33
39
const baseDir = join ( outputDir , "server-functions/default" , getPackagePath ( buildOpts ) ) ;
@@ -39,10 +45,9 @@ async function getRule(buildOpts: BuildOptions) {
39
45
await Promise . all (
40
46
manifests . map (
41
47
async ( manifest ) => `
42
- if ($PATH.endsWith("${ normalizePath ( "/" + relative ( dotNextDir , manifest ) ) } ")) {
43
- return ${ await readFile ( manifest , "utf-8" ) } ;
44
- }
45
- `
48
+ if ($PATH.endsWith("${ normalizePath ( "/" + relative ( dotNextDir , manifest ) ) } ")) {
49
+ return ${ await readFile ( manifest , "utf-8" ) } ;
50
+ }`
46
51
)
47
52
)
48
53
) . join ( "\n" ) ;
@@ -62,3 +67,47 @@ function loadManifest($PATH, $$$ARGS) {
62
67
}` ,
63
68
} satisfies RuleConfig ;
64
69
}
70
+
71
+ async function getEvalManifestRule ( buildOpts : BuildOptions ) {
72
+ const { outputDir } = buildOpts ;
73
+
74
+ const baseDir = join ( outputDir , "server-functions/default" , getPackagePath ( buildOpts ) , ".next" ) ;
75
+ const appDir = join ( baseDir , "server/app" ) ;
76
+ const manifests = await glob ( join ( baseDir , "**/*_client-reference-manifest.js" ) , {
77
+ windowsPathsNoEscape : true ,
78
+ } ) ;
79
+
80
+ const returnManifests = manifests
81
+ . map ( ( manifest ) => {
82
+ const endsWith = normalizePath ( relative ( baseDir , manifest ) ) ;
83
+ const key = normalizePath ( "/" + relative ( appDir , manifest ) ) . replace (
84
+ "_client-reference-manifest.js" ,
85
+ ""
86
+ ) ;
87
+ return `
88
+ if ($PATH.endsWith("${ endsWith } ")) {
89
+ require(${ JSON . stringify ( manifest ) } );
90
+ return {
91
+ __RSC_MANIFEST: {
92
+ "${ key } ": globalThis.__RSC_MANIFEST["${ key } "],
93
+ },
94
+ };
95
+ }` ;
96
+ } )
97
+ . join ( "\n" ) ;
98
+
99
+ return {
100
+ rule : {
101
+ pattern : `
102
+ function evalManifest($PATH, $$$ARGS) {
103
+ $$$_
104
+ }` ,
105
+ } ,
106
+ fix : `
107
+ function evalManifest($PATH, $$$ARGS) {
108
+ $PATH = $PATH.replaceAll(${ JSON . stringify ( sep ) } , ${ JSON . stringify ( posix . sep ) } );
109
+ ${ returnManifests }
110
+ throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
111
+ }` ,
112
+ } satisfies RuleConfig ;
113
+ }
0 commit comments