From 1ad25451a69e10183200dfaf4c7a8d6c8f800543 Mon Sep 17 00:00:00 2001
From: Thuc Pham <51660321+thucpn@users.noreply.github.com>
Date: Wed, 24 Apr 2024 15:54:50 +0700
Subject: [PATCH] fix: allow onnxruntime in nextjs server side (#59)

---
 templates/types/streaming/nextjs/next.config.json  |  3 ++-
 .../types/streaming/nextjs/webpack.config.mjs      | 14 ++++++++------
 .../types/streaming/nextjs/webpack.config.o11y.mjs |  6 ------
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/templates/types/streaming/nextjs/next.config.json b/templates/types/streaming/nextjs/next.config.json
index 264e20ef..856fb100 100644
--- a/templates/types/streaming/nextjs/next.config.json
+++ b/templates/types/streaming/nextjs/next.config.json
@@ -2,6 +2,7 @@
   "experimental": {
     "outputFileTracingIncludes": {
       "/*": ["./cache/**/*"]
-    }
+    },
+    "serverComponentsExternalPackages": ["sharp", "onnxruntime-node"]
   }
 }
diff --git a/templates/types/streaming/nextjs/webpack.config.mjs b/templates/types/streaming/nextjs/webpack.config.mjs
index 8c9edeb1..156284b3 100644
--- a/templates/types/streaming/nextjs/webpack.config.mjs
+++ b/templates/types/streaming/nextjs/webpack.config.mjs
@@ -1,13 +1,15 @@
 // webpack config must be a function in NextJS that is used to patch the default webpack config provided by NextJS, see https://nextjs.org/docs/pages/api-reference/next-config-js/webpack
 export default function webpack(config) {
-  // See https://webpack.js.org/configuration/resolve/#resolvealias
-  config.resolve.alias = {
-    ...config.resolve.alias,
-    sharp$: false,
-    "onnxruntime-node$": false,
-  };
   config.resolve.fallback = {
     aws4: false,
   };
+
+  // Following lines will fix issues with onnxruntime-node when using pnpm
+  // See: https://github.com/vercel/next.js/issues/43433
+  config.externals.push({
+    "onnxruntime-node": "commonjs onnxruntime-node",
+    sharp: "commonjs sharp",
+  });
+
   return config;
 }
diff --git a/templates/types/streaming/nextjs/webpack.config.o11y.mjs b/templates/types/streaming/nextjs/webpack.config.o11y.mjs
index c7c7ad3f..03e5be23 100644
--- a/templates/types/streaming/nextjs/webpack.config.o11y.mjs
+++ b/templates/types/streaming/nextjs/webpack.config.o11y.mjs
@@ -1,10 +1,4 @@
 export default function webpack(config, isServer) {
-  // See https://webpack.js.org/configuration/resolve/#resolvealias
-  config.resolve.alias = {
-    ...config.resolve.alias,
-    sharp$: false,
-    "onnxruntime-node$": false,
-  };
   config.resolve.fallback = {
     aws4: false,
   };
-- 
GitLab