From 01a692ce7af9164b4fc881dd3cdccdfd9faaa073 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 18 Apr 2025 11:14:08 +0200 Subject: [PATCH 1/2] trace node packages --- .changeset/smooth-tomatoes-compete.md | 5 ++ .../open-next/src/build/copyTracedFiles.ts | 61 +++++++++++++------ 2 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 .changeset/smooth-tomatoes-compete.md diff --git a/.changeset/smooth-tomatoes-compete.md b/.changeset/smooth-tomatoes-compete.md new file mode 100644 index 00000000..5854a1b1 --- /dev/null +++ b/.changeset/smooth-tomatoes-compete.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +trace node packages diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index e01e49a8..8c2c76be 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -89,9 +89,6 @@ export async function copyTracedFiles({ const standaloneServerDir = path.join(standaloneNextDir, "server"); const outputNextDir = path.join(outputDir, packagePath, ".next"); - const extractFiles = (files: string[], from = standaloneNextDir) => - files.map((f) => path.resolve(from, f)); - // On next 14+, we might not have to include those files // For next 13, we need to include them otherwise we get runtime error const requiredServerFiles = JSON.parse( @@ -106,12 +103,25 @@ export async function copyTracedFiles({ ), ); + // Files to copy + // Map from files in the `.next/standalone` to files in the `.open-next` folder const filesToCopy = new Map(); + // Node packages + // Map from folders in the project to folders in the `.open-next` folder + const nodePackages = new Map(); + // Files necessary by the server if (!skipServerFiles) { - extractFiles(requiredServerFiles.files).forEach((f) => { - filesToCopy.set(f, f.replace(standaloneDir, outputDir)); + requiredServerFiles.files.forEach((tracedPath: string) => { + const src = path.join(standaloneNextDir, tracedPath); + const dst = path.join(outputDir, packagePath, ".next", tracedPath); + filesToCopy.set(src, dst); + + const module = path.join(dotNextDir, tracedPath); + if (module.endsWith("package.json")) { + nodePackages.set(path.dirname(module), path.dirname(dst)); + } }); } // create directory for pages @@ -131,17 +141,19 @@ export async function copyTracedFiles({ }); const computeCopyFilesForPage = (pagePath: string) => { - const fullFilePath = `server/${pagePath}.js`; + const serverPath = `server/${pagePath}.js`; + const serverDir = path.dirname(serverPath); + let requiredFiles: { files: string[] }; try { requiredFiles = JSON.parse( readFileSync( - path.join(standaloneNextDir, `${fullFilePath}.nft.json`), + path.join(standaloneNextDir, `${serverPath}.nft.json`), "utf8", ), ); } catch (e) { - if (existsSync(path.join(standaloneNextDir, fullFilePath))) { + if (existsSync(path.join(standaloneNextDir, serverPath))) { //TODO: add a link to the docs throw new Error( ` @@ -156,27 +168,37 @@ See the docs for more information on how to bundle edge runtime functions. throw new Error(` -------------------------------------------------------------------------------- We cannot find the route for ${pagePath}. -File ${fullFilePath} does not exist +File ${serverPath} does not exist --------------------------------------------------------------------------------`); } - const dir = path.dirname(fullFilePath); - extractFiles( - requiredFiles.files, - path.join(standaloneNextDir, dir), - ).forEach((f) => { - filesToCopy.set(f, f.replace(standaloneDir, outputDir)); + + requiredFiles.files.forEach((tracedPath: string) => { + const src = path.join(standaloneNextDir, serverDir, tracedPath); + const dst = path.join( + outputDir, + packagePath, + ".next", + serverDir, + tracedPath, + ); + filesToCopy.set(src, dst); + + const module = path.join(dotNextDir, serverDir, tracedPath); + if (module.endsWith("package.json")) { + nodePackages.set(path.dirname(module), path.dirname(dst)); + } }); - if (!existsSync(path.join(standaloneNextDir, fullFilePath))) { + if (!existsSync(path.join(standaloneNextDir, serverPath))) { throw new Error( `This error should only happen for static 404 and 500 page from page router. Report this if that's not the case., - File ${fullFilePath} does not exist`, + File ${serverPath} does not exist`, ); } filesToCopy.set( - path.join(standaloneNextDir, fullFilePath), - path.join(outputNextDir, fullFilePath), + path.join(standaloneNextDir, serverPath), + path.join(outputNextDir, serverPath), ); }; @@ -360,6 +382,7 @@ File ${fullFilePath} does not exist return { tracedFiles, + nodePackages, manifests: getManifests(standaloneNextDir), }; } From b21840005dfd1bb6536dcf7c3e099414650add4d Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 18 Apr 2025 12:32:37 +0200 Subject: [PATCH 2/2] fixup! review --- .../open-next/src/build/copyTracedFiles.ts | 74 +++++++------------ 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 8c2c76be..0dd2f15a 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -89,40 +89,46 @@ export async function copyTracedFiles({ const standaloneServerDir = path.join(standaloneNextDir, "server"); const outputNextDir = path.join(outputDir, packagePath, ".next"); - // On next 14+, we might not have to include those files - // For next 13, we need to include them otherwise we get runtime error - const requiredServerFiles = JSON.parse( - readFileSync( - path.join( - dotNextDir, - bundledNextServer - ? "next-minimal-server.js.nft.json" - : "next-server.js.nft.json", - ), - "utf8", - ), - ); - // Files to copy // Map from files in the `.next/standalone` to files in the `.open-next` folder const filesToCopy = new Map(); // Node packages // Map from folders in the project to folders in the `.open-next` folder + // The map might also include the mono-repo path. const nodePackages = new Map(); - // Files necessary by the server - if (!skipServerFiles) { - requiredServerFiles.files.forEach((tracedPath: string) => { - const src = path.join(standaloneNextDir, tracedPath); - const dst = path.join(outputDir, packagePath, ".next", tracedPath); + /** + * Extracts files and node packages from a .nft.json file + * @param nftFile path to the .nft.json file relative to `.next/` + */ + const processNftFile = (nftFile: string) => { + const subDir = path.dirname(nftFile); + const files: string[] = JSON.parse( + readFileSync(path.join(dotNextDir, nftFile), "utf8"), + ).files; + + files.forEach((tracedPath: string) => { + const src = path.join(standaloneNextDir, subDir, tracedPath); + const dst = path.join(outputNextDir, subDir, tracedPath); filesToCopy.set(src, dst); - const module = path.join(dotNextDir, tracedPath); + const module = path.join(dotNextDir, subDir, tracedPath); if (module.endsWith("package.json")) { nodePackages.set(path.dirname(module), path.dirname(dst)); } }); + }; + + // Files necessary by the server + if (!skipServerFiles) { + // On next 14+, we might not have to include those files + // For next 13, we need to include them otherwise we get runtime error + const nftFile = bundledNextServer + ? "next-minimal-server.js.nft.json" + : "next-server.js.nft.json"; + + processNftFile(nftFile); } // create directory for pages if (existsSync(path.join(standaloneNextDir, "server/pages"))) { @@ -142,18 +148,11 @@ export async function copyTracedFiles({ const computeCopyFilesForPage = (pagePath: string) => { const serverPath = `server/${pagePath}.js`; - const serverDir = path.dirname(serverPath); - let requiredFiles: { files: string[] }; try { - requiredFiles = JSON.parse( - readFileSync( - path.join(standaloneNextDir, `${serverPath}.nft.json`), - "utf8", - ), - ); + processNftFile(`${serverPath}.nft.json`); } catch (e) { - if (existsSync(path.join(standaloneNextDir, serverPath))) { + if (existsSync(path.join(dotNextDir, serverPath))) { //TODO: add a link to the docs throw new Error( ` @@ -172,23 +171,6 @@ File ${serverPath} does not exist --------------------------------------------------------------------------------`); } - requiredFiles.files.forEach((tracedPath: string) => { - const src = path.join(standaloneNextDir, serverDir, tracedPath); - const dst = path.join( - outputDir, - packagePath, - ".next", - serverDir, - tracedPath, - ); - filesToCopy.set(src, dst); - - const module = path.join(dotNextDir, serverDir, tracedPath); - if (module.endsWith("package.json")) { - nodePackages.set(path.dirname(module), path.dirname(dst)); - } - }); - if (!existsSync(path.join(standaloneNextDir, serverPath))) { throw new Error( `This error should only happen for static 404 and 500 page from page router. Report this if that's not the case.,