From 30c86acd759c3fe26aa5eca7e535f000739497e4 Mon Sep 17 00:00:00 2001 From: magnus Date: Wed, 12 Mar 2025 10:01:55 +0100 Subject: [PATCH 1/4] fix: ignore @img/* in copyTracedFiles --- packages/open-next/src/build/copyTracedFiles.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 9de488629..b78803bf6 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -198,7 +198,10 @@ File ${fullFilePath} does not exist //TODO: we need to figure which packages we could safely remove from.includes(path.join("node_modules", "caniuse-lite")) || // from.includes("jest-worker") || This ones seems necessary for next 12 - from.includes(path.join("node_modules", "sharp")) + from.includes(path.join("node_modules", "sharp")) || + // This seems to be only in Next 15 + // Some of sharp deps are under the @img scope + from.includes(path.join("node_modules", "@img")) ) { return; } From f230586e70cc21373dbcf638fcfc7a019212b8b3 Mon Sep 17 00:00:00 2001 From: magnus Date: Wed, 12 Mar 2025 10:25:38 +0100 Subject: [PATCH 2/4] changeset --- .changeset/tall-pets-hammer.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tall-pets-hammer.md diff --git a/.changeset/tall-pets-hammer.md b/.changeset/tall-pets-hammer.md new file mode 100644 index 000000000..079f1df72 --- /dev/null +++ b/.changeset/tall-pets-hammer.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +Fix: Ignore packages under the @img/\* scope to exclude sharp from the server bundle. \ No newline at end of file From f9c7b0dca2b6d4cf4b104cbb2e347202ffdc6025 Mon Sep 17 00:00:00 2001 From: magnus Date: Wed, 12 Mar 2025 13:13:13 +0100 Subject: [PATCH 3/4] review --- .../open-next/src/build/copyTracedFiles.ts | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index b78803bf6..c85039fe8 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -19,6 +19,23 @@ import { MIDDLEWARE_TRACE_FILE } from "./constant.js"; const __dirname = url.fileURLToPath(new URL(".", import.meta.url)); +//TODO: we need to figure which packages we could safely remove +const EXCLUDED_PACKAGES = [ + "caniuse-lite", + "sharp", + // This seems to be only in Next 15 + // Some of sharp deps are under the @img scope + "@img", +]; + +function isExcluded(srcPath: string) { + return EXCLUDED_PACKAGES.some((excluded) => + new RegExp(`${path.sep}node_modules${path.sep}${excluded}${path.sep}`).test( + srcPath, + ), + ); +} + function copyPatchFile(outputDir: string) { const patchFile = path.join(__dirname, "patch", "patchedAsyncStorage.js"); const outputPatchFile = path.join(outputDir, "patchedAsyncStorage.cjs"); @@ -194,15 +211,8 @@ File ${fullFilePath} does not exist //Actually copy the files filesToCopy.forEach((to, from) => { - if ( - //TODO: we need to figure which packages we could safely remove - from.includes(path.join("node_modules", "caniuse-lite")) || - // from.includes("jest-worker") || This ones seems necessary for next 12 - from.includes(path.join("node_modules", "sharp")) || - // This seems to be only in Next 15 - // Some of sharp deps are under the @img scope - from.includes(path.join("node_modules", "@img")) - ) { + // We don't want to copy excluded packages (i.e sharp) + if (isExcluded(from)) { return; } mkdirSync(path.dirname(to), { recursive: true }); From d0cf023f3fdf3a13316c413c3e26138eca84e3e0 Mon Sep 17 00:00:00 2001 From: magnus Date: Wed, 12 Mar 2025 14:12:16 +0100 Subject: [PATCH 4/4] getCrossPlatformPathRegex and str.match --- packages/open-next/src/build/copyTracedFiles.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index c85039fe8..cbacc6938 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -14,6 +14,7 @@ import { import path from "node:path"; import { loadConfig, loadPrerenderManifest } from "config/util.js"; +import { getCrossPlatformPathRegex } from "utils/regex.js"; import logger from "../logger.js"; import { MIDDLEWARE_TRACE_FILE } from "./constant.js"; @@ -30,9 +31,7 @@ const EXCLUDED_PACKAGES = [ function isExcluded(srcPath: string) { return EXCLUDED_PACKAGES.some((excluded) => - new RegExp(`${path.sep}node_modules${path.sep}${excluded}${path.sep}`).test( - srcPath, - ), + srcPath.match(getCrossPlatformPathRegex(`/node_modules/${excluded}/`)), ); }