From 322194c4ab4019eae5bb65f661c231b3fdb352fd Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 19 Nov 2024 14:13:51 +0100 Subject: [PATCH] refactor: minor updates --- .../open-next/src/build/copyTracedFiles.ts | 50 +++++++++++-------- packages/open-next/src/plugins/edge.ts | 6 +-- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 46fc44c07..007f8a1e5 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -38,9 +38,8 @@ export async function copyTracedFiles( const standaloneNextDir = path.join(standaloneDir, packagePath, ".next"); const outputNextDir = path.join(outputDir, packagePath, ".next"); - const extractFiles = (files: string[], from = standaloneNextDir) => { - return files.map((f) => path.resolve(from, f)); - }; + 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 @@ -203,25 +202,36 @@ File ${fullFilePath} does not exist } }); - readdirSync(standaloneNextDir).forEach((f) => { - if (statSync(path.join(standaloneNextDir, f)).isDirectory()) return; - copyFileSync(path.join(standaloneNextDir, f), path.join(outputNextDir, f)); - }); + readdirSync(standaloneNextDir) + .filter( + (fileOrDir) => + !statSync(path.join(standaloneNextDir, fileOrDir)).isDirectory(), + ) + .forEach((file) => + copyFileSync( + path.join(standaloneNextDir, file), + path.join(outputNextDir, file), + ), + ); // We then need to copy all the files at the root of server mkdirSync(path.join(outputNextDir, "server"), { recursive: true }); - readdirSync(path.join(standaloneNextDir, "server")).forEach((f) => { - if (statSync(path.join(standaloneNextDir, "server", f)).isDirectory()) - return; - if (f !== "server.js") { + readdirSync(path.join(standaloneNextDir, "server")) + .filter( + (fileOrDir) => + !statSync( + path.join(standaloneNextDir, "server", fileOrDir), + ).isDirectory(), + ) + .filter((file) => file !== "server.js") + .forEach((file) => copyFileSync( - path.join(standaloneNextDir, "server", f), - path.join(path.join(outputNextDir, "server"), f), - ); - } - }); + path.join(standaloneNextDir, "server", file), + path.join(path.join(outputNextDir, "server"), file), + ), + ); // Copy patch file copyPatchFile(path.join(outputDir, packagePath)); @@ -285,11 +295,9 @@ File ${fullFilePath} does not exist } }); - staticFiles.forEach((f: string) => { - if (f.endsWith(".html")) { - copyStaticFile(`server/${f}`); - } - }); + staticFiles + .filter((file) => file.endsWith(".html")) + .forEach((file) => copyStaticFile(`server/${file}`)); } logger.debug("copyTracedFiles:", Date.now() - tsStart, "ms"); diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts index f4dadc6e0..83c71c18c 100644 --- a/packages/open-next/src/plugins/edge.ts +++ b/packages/open-next/src/plugins/edge.ts @@ -169,13 +169,11 @@ ${contents} const MiddlewareManifest = loadMiddlewareManifest(nextDir); const contents = ` - import path from "path"; + import path from "node:path"; import { debug } from "../logger"; - if(!globalThis.__dirname) { - globalThis.__dirname = "" - } + globalThis.__dirname ??= ""; export const NEXT_DIR = path.join(__dirname, ".next"); export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");