Unable to load ONNX file using vite and onnxruntime-web. #15962
Unanswered
galenoshea
asked this question in
Q&A
Replies: 1 comment
-
@galenoshea I managed to solve the issue without a loader by adding assetsInclude: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug
I am attempting to load an onnx model when using onnxruntime-web and webpack but it seems I can't parse the onnx file when using vite.
Reproduction
https://vite.new/
Steps to reproduce
npm install onnxruntime-web and attempt to parse onnx file
`import { fileURLToPath, URL } from "url";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import environment from "vite-plugin-environment";
import dotenv from "dotenv";
import { viteStaticCopy } from "vite-plugin-static-copy";
import { readFileSync } from "fs";
dotenv.config({ path: "../../.env" });
function onnxLoader() {
return {
name: "onnx-loader",
transform(code, id) {
if (id.endsWith(".onnx")) {
// Read the contents of the ONNX file
const contents = readFileSync(id, "utf-8");
// Return the contents as a JavaScript module
return {
code:
export default ${JSON.stringify(contents)};
,map: null, // You can optionally provide source maps here
};
}
},
};
}
export default defineConfig({
build: {
emptyOutDir: true,
},
optimizeDeps: {
esbuildOptions: {
define: {
global: "globalThis",
},
},
},
server: {
proxy: {
"/api": {
target: "http://127.0.0.1:4943",
changeOrigin: true,
},
},
},
plugins: [
react(),
onnxLoader(),
viteStaticCopy({
targets: [
{
src: "../../node_modules/onnxruntime-web/dist/*.wasm",
dest: ".",
},
],
}),
environment("all", { prefix: "CANISTER_" }),
environment("all", { prefix: "DFX_" }),
],
resolve: {
alias: [
{
find: "declarations",
replacement: fileURLToPath(new URL("../declarations", import.meta.url)),
},
],
},
});
`
System Info
Used Package Manager
npm
Logs
No response
Validations
Beta Was this translation helpful? Give feedback.
All reactions