From 386930699de2e58c26b0bdc201dfeaa0519b57c8 Mon Sep 17 00:00:00 2001 From: ae9is <125031666+ae9is@users.noreply.github.com> Date: Tue, 10 Dec 2024 03:54:52 +0000 Subject: [PATCH] Fix ghost dependency onnxruntime-common with explicit versions for node and web --- package-lock.json | 16 ++++++++++++++++ package.json | 13 ++++++++++++- src/backends/onnx.js | 5 ++++- webpack.config.js | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b8e050b97..ed604dcc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,8 @@ "license": "Apache-2.0", "dependencies": { "@huggingface/jinja": "^0.3.2", + "onnxruntime-common-for-node": "npm:onnxruntime-common@1.20.1", + "onnxruntime-common-for-web": "npm:onnxruntime-common@1.21.0-dev.20241205-6ed77cc374", "onnxruntime-node": "1.20.1", "onnxruntime-web": "1.21.0-dev.20241205-d27fecd3d3", "sharp": "^0.33.5" @@ -8765,6 +8767,20 @@ "integrity": "sha512-YiU0s0IzYYC+gWvqD1HzLc46Du1sXpSiwzKb63PACIJr6LfL27VsXSXQvt68EzD3V0D5Bc0vyJTjmMxp0ylQiw==", "license": "MIT" }, + "node_modules/onnxruntime-common-for-node": { + "name": "onnxruntime-common", + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.20.1.tgz", + "integrity": "sha512-YiU0s0IzYYC+gWvqD1HzLc46Du1sXpSiwzKb63PACIJr6LfL27VsXSXQvt68EzD3V0D5Bc0vyJTjmMxp0ylQiw==", + "license": "MIT" + }, + "node_modules/onnxruntime-common-for-web": { + "name": "onnxruntime-common", + "version": "1.21.0-dev.20241205-6ed77cc374", + "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.21.0-dev.20241205-6ed77cc374.tgz", + "integrity": "sha512-U4DGq/dZiboIEK0Zv1KUuWJesJ/txUALpWSXwI8kqOCSxe8GrI65xfRFeMbqYFhPVGAWZPsBpT1zo1s4ksrlrg==", + "license": "MIT" + }, "node_modules/onnxruntime-node": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.20.1.tgz", diff --git a/package.json b/package.json index 361158609..c4b24b378 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,8 @@ "homepage": "https://github.com/huggingface/transformers.js#readme", "dependencies": { "@huggingface/jinja": "^0.3.2", + "onnxruntime-common-for-node": "npm:onnxruntime-common@1.20.0", + "onnxruntime-common-for-web": "npm:onnxruntime-common@1.21.0-dev.20241205-6ed77cc374", "onnxruntime-node": "1.20.1", "onnxruntime-web": "1.21.0-dev.20241205-d27fecd3d3", "sharp": "^0.33.5" @@ -75,6 +77,14 @@ "webpack-cli": "^5.1.4", "webpack-dev-server": "^5.1.0" }, + "overrides": { + "onnxruntime-node": { + "onnxruntime-common": "$onnxruntime-common-for-node" + }, + "onnxruntime-web": { + "onnxruntime-common": "$onnxruntime-common-for-web" + } + }, "files": [ "src", "dist", @@ -87,7 +97,8 @@ "path": false, "url": false, "sharp": false, - "onnxruntime-node": false + "onnxruntime-node": false, + "onnxruntime-common-for-node": false }, "publishConfig": { "access": "public" diff --git a/src/backends/onnx.js b/src/backends/onnx.js index 38cd71337..9541efa26 100644 --- a/src/backends/onnx.js +++ b/src/backends/onnx.js @@ -23,7 +23,10 @@ import { env, apis } from '../env.js'; import * as ONNX_NODE from 'onnxruntime-node'; import * as ONNX_WEB from 'onnxruntime-web'; -export { Tensor } from 'onnxruntime-common'; +import { Tensor as Tensor_NODE } from "onnxruntime-common-for-node"; +import { Tensor as Tensor_WEB } from "onnxruntime-common-for-web"; +let Tensor = apis.IS_NODE_ENV ? Tensor_NODE : Tensor_WEB; +export { Tensor }; /** * @typedef {import('onnxruntime-common').InferenceSession.ExecutionProviderConfig} ONNXExecutionProviders diff --git a/webpack.config.js b/webpack.config.js index 8aff63d0d..0cff8eb92 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -94,13 +94,14 @@ function buildConfig({ // Do not bundle onnxruntime-web when packaging for Node.js. // Instead, we use the native library (onnxruntime-node). -const NODE_IGNORE_MODULES = ["onnxruntime-web"]; +const NODE_IGNORE_MODULES = ["onnxruntime-web", "onnxruntime-common-for-web"]; // Do not bundle the following modules with webpack (mark as external) // NOTE: This is necessary for both type="module" and type="commonjs", // and will be ignored when building for web (only used for node/deno) const NODE_EXTERNAL_MODULES = [ "onnxruntime-node", + "onnxruntime-common-for-node", "sharp", "fs", "path",