@@ -89,30 +89,46 @@ export async function copyTracedFiles({
89
89
const standaloneServerDir = path . join ( standaloneNextDir , "server" ) ;
90
90
const outputNextDir = path . join ( outputDir , packagePath , ".next" ) ;
91
91
92
- const extractFiles = ( files : string [ ] , from = standaloneNextDir ) =>
93
- files . map ( ( f ) => path . resolve ( from , f ) ) ;
94
-
95
- // On next 14+, we might not have to include those files
96
- // For next 13, we need to include them otherwise we get runtime error
97
- const requiredServerFiles = JSON . parse (
98
- readFileSync (
99
- path . join (
100
- dotNextDir ,
101
- bundledNextServer
102
- ? "next-minimal-server.js.nft.json"
103
- : "next-server.js.nft.json" ,
104
- ) ,
105
- "utf8" ,
106
- ) ,
107
- ) ;
108
-
92
+ // Files to copy
93
+ // Map from files in the `.next/standalone` to files in the `.open-next` folder
109
94
const filesToCopy = new Map < string , string > ( ) ;
110
95
96
+ // Node packages
97
+ // Map from folders in the project to folders in the `.open-next` folder
98
+ // The map might also include the mono-repo path.
99
+ const nodePackages = new Map < string , string > ( ) ;
100
+
101
+ /**
102
+ * Extracts files and node packages from a .nft.json file
103
+ * @param nftFile path to the .nft.json file relative to `.next/`
104
+ */
105
+ const processNftFile = ( nftFile : string ) => {
106
+ const subDir = path . dirname ( nftFile ) ;
107
+ const files : string [ ] = JSON . parse (
108
+ readFileSync ( path . join ( dotNextDir , nftFile ) , "utf8" ) ,
109
+ ) . files ;
110
+
111
+ files . forEach ( ( tracedPath : string ) => {
112
+ const src = path . join ( standaloneNextDir , subDir , tracedPath ) ;
113
+ const dst = path . join ( outputNextDir , subDir , tracedPath ) ;
114
+ filesToCopy . set ( src , dst ) ;
115
+
116
+ const module = path . join ( dotNextDir , subDir , tracedPath ) ;
117
+ if ( module . endsWith ( "package.json" ) ) {
118
+ nodePackages . set ( path . dirname ( module ) , path . dirname ( dst ) ) ;
119
+ }
120
+ } ) ;
121
+ } ;
122
+
111
123
// Files necessary by the server
112
124
if ( ! skipServerFiles ) {
113
- extractFiles ( requiredServerFiles . files ) . forEach ( ( f ) => {
114
- filesToCopy . set ( f , f . replace ( standaloneDir , outputDir ) ) ;
115
- } ) ;
125
+ // On next 14+, we might not have to include those files
126
+ // For next 13, we need to include them otherwise we get runtime error
127
+ const nftFile = bundledNextServer
128
+ ? "next-minimal-server.js.nft.json"
129
+ : "next-server.js.nft.json" ;
130
+
131
+ processNftFile ( nftFile ) ;
116
132
}
117
133
// create directory for pages
118
134
if ( existsSync ( path . join ( standaloneNextDir , "server/pages" ) ) ) {
@@ -131,17 +147,12 @@ export async function copyTracedFiles({
131
147
} ) ;
132
148
133
149
const computeCopyFilesForPage = ( pagePath : string ) => {
134
- const fullFilePath = `server/${ pagePath } .js` ;
135
- let requiredFiles : { files : string [ ] } ;
150
+ const serverPath = `server/${ pagePath } .js` ;
151
+
136
152
try {
137
- requiredFiles = JSON . parse (
138
- readFileSync (
139
- path . join ( standaloneNextDir , `${ fullFilePath } .nft.json` ) ,
140
- "utf8" ,
141
- ) ,
142
- ) ;
153
+ processNftFile ( `${ serverPath } .nft.json` ) ;
143
154
} catch ( e ) {
144
- if ( existsSync ( path . join ( standaloneNextDir , fullFilePath ) ) ) {
155
+ if ( existsSync ( path . join ( dotNextDir , serverPath ) ) ) {
145
156
//TODO: add a link to the docs
146
157
throw new Error (
147
158
`
@@ -156,27 +167,20 @@ See the docs for more information on how to bundle edge runtime functions.
156
167
throw new Error ( `
157
168
--------------------------------------------------------------------------------
158
169
We cannot find the route for ${ pagePath } .
159
- File ${ fullFilePath } does not exist
170
+ File ${ serverPath } does not exist
160
171
--------------------------------------------------------------------------------` ) ;
161
172
}
162
- const dir = path . dirname ( fullFilePath ) ;
163
- extractFiles (
164
- requiredFiles . files ,
165
- path . join ( standaloneNextDir , dir ) ,
166
- ) . forEach ( ( f ) => {
167
- filesToCopy . set ( f , f . replace ( standaloneDir , outputDir ) ) ;
168
- } ) ;
169
173
170
- if ( ! existsSync ( path . join ( standaloneNextDir , fullFilePath ) ) ) {
174
+ if ( ! existsSync ( path . join ( standaloneNextDir , serverPath ) ) ) {
171
175
throw new Error (
172
176
`This error should only happen for static 404 and 500 page from page router. Report this if that's not the case.,
173
- File ${ fullFilePath } does not exist` ,
177
+ File ${ serverPath } does not exist` ,
174
178
) ;
175
179
}
176
180
177
181
filesToCopy . set (
178
- path . join ( standaloneNextDir , fullFilePath ) ,
179
- path . join ( outputNextDir , fullFilePath ) ,
182
+ path . join ( standaloneNextDir , serverPath ) ,
183
+ path . join ( outputNextDir , serverPath ) ,
180
184
) ;
181
185
} ;
182
186
@@ -360,6 +364,7 @@ File ${fullFilePath} does not exist
360
364
361
365
return {
362
366
tracedFiles,
367
+ nodePackages,
363
368
manifests : getManifests ( standaloneNextDir ) ,
364
369
} ;
365
370
}
0 commit comments