@@ -89,30 +89,46 @@ export async function copyTracedFiles({
8989 const standaloneServerDir = path . join ( standaloneNextDir , "server" ) ;
9090 const outputNextDir = path . join ( outputDir , packagePath , ".next" ) ;
9191
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
10994 const filesToCopy = new Map < string , string > ( ) ;
11095
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+
111123 // Files necessary by the server
112124 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 ) ;
116132 }
117133 // create directory for pages
118134 if ( existsSync ( path . join ( standaloneNextDir , "server/pages" ) ) ) {
@@ -131,17 +147,12 @@ export async function copyTracedFiles({
131147 } ) ;
132148
133149 const computeCopyFilesForPage = ( pagePath : string ) => {
134- const fullFilePath = `server/${ pagePath } .js` ;
135- let requiredFiles : { files : string [ ] } ;
150+ const serverPath = `server/${ pagePath } .js` ;
151+
136152 try {
137- requiredFiles = JSON . parse (
138- readFileSync (
139- path . join ( standaloneNextDir , `${ fullFilePath } .nft.json` ) ,
140- "utf8" ,
141- ) ,
142- ) ;
153+ processNftFile ( `${ serverPath } .nft.json` ) ;
143154 } catch ( e ) {
144- if ( existsSync ( path . join ( standaloneNextDir , fullFilePath ) ) ) {
155+ if ( existsSync ( path . join ( dotNextDir , serverPath ) ) ) {
145156 //TODO: add a link to the docs
146157 throw new Error (
147158 `
@@ -156,27 +167,20 @@ See the docs for more information on how to bundle edge runtime functions.
156167 throw new Error ( `
157168--------------------------------------------------------------------------------
158169We cannot find the route for ${ pagePath } .
159- File ${ fullFilePath } does not exist
170+ File ${ serverPath } does not exist
160171--------------------------------------------------------------------------------` ) ;
161172 }
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- } ) ;
169173
170- if ( ! existsSync ( path . join ( standaloneNextDir , fullFilePath ) ) ) {
174+ if ( ! existsSync ( path . join ( standaloneNextDir , serverPath ) ) ) {
171175 throw new Error (
172176 `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` ,
174178 ) ;
175179 }
176180
177181 filesToCopy . set (
178- path . join ( standaloneNextDir , fullFilePath ) ,
179- path . join ( outputNextDir , fullFilePath ) ,
182+ path . join ( standaloneNextDir , serverPath ) ,
183+ path . join ( outputNextDir , serverPath ) ,
180184 ) ;
181185 } ;
182186
@@ -360,6 +364,7 @@ File ${fullFilePath} does not exist
360364
361365 return {
362366 tracedFiles,
367+ nodePackages,
363368 manifests : getManifests ( standaloneNextDir ) ,
364369 } ;
365370}
0 commit comments