Skip to content

Commit 0600482

Browse files
authored
[Web] Add wasm and mjs files to package.json exports to improve bundler support (#26394)
### Description Currently the files which are needed for wasm aren't exported from the package at all. The files in question are: ``` ort-wasm-simd-threaded.wasm ort-wasm-simd-threaded.jsep.wasm ort-wasm-simd-threaded.asyncify.wasm ort-wasm-simd-threaded.mjs ort-wasm-simd-threaded.jsep.mjs ort-wasm-simd-threaded.asyncify.mjs ``` This PR changes that and adds them to `exports` field in the `package.json`. ### Motivation and Context Bundlers like `webpack` use the `copyPlugin` to move those files into the `public` directory, so the files can be accessed by a stable url. However more advanced and "state of the art" bundlers like `vite` are able to [import asset urls directly](https://vite.dev/guide/assets.html#explicit-url-imports). Vite takes the asset, moves it to to public assets folder (possibily renames the asset and adds a hash etc.). The imported value then is the bundled assets final url. Those urls can then be used in the `env.wasm.wasmPaths` directly. In vites case the full code example is: ```js import wasmUrl from 'onnxruntime-web/ort-wasm-simd-threaded.wasm?url'; import mjsUrl from 'onnxruntime-web/ort-wasm-simd-threaded.mjs?url'; env.wasm.wasmPaths = { wasm: wasmUrl, mjs: mjsUrl, }; ``` With those added exports we can leverage more of the bundlers capabilities and in vites case there isn't any need to add any additional configs. It would just work. When importing we also get proper suggestions: <img width="1604" height="498" alt="imports" src="https://github.com/user-attachments/assets/2678ccc2-ae46-4289-aa6e-607ecbc5388b" /> ---- I would like additional tests to ensure that the exports are available, but I couldn't make the `e2e` tests work on my system. I would appreciate some guidance on that topic.
1 parent 771a4d4 commit 0600482

File tree

7 files changed

+92
-2
lines changed

7 files changed

+92
-2
lines changed

js/web/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@
115115
"default": "./dist/ort.jspi.bundle.min.mjs"
116116
},
117117
"require": "./dist/ort.jspi.min.js"
118-
}
118+
},
119+
"./ort-wasm-simd-threaded.wasm": "./dist/ort-wasm-simd-threaded.wasm",
120+
"./ort-wasm-simd-threaded.jsep.wasm": "./dist/ort-wasm-simd-threaded.jsep.wasm",
121+
"./ort-wasm-simd-threaded.jspi.wasm": "./dist/ort-wasm-simd-threaded.jspi.wasm",
122+
"./ort-wasm-simd-threaded.asyncify.wasm": "./dist/ort-wasm-simd-threaded.asyncify.wasm",
123+
"./ort-wasm-simd-threaded.mjs": "./dist/ort-wasm-simd-threaded.mjs",
124+
"./ort-wasm-simd-threaded.jsep.mjs": "./dist/ort-wasm-simd-threaded.jsep.mjs",
125+
"./ort-wasm-simd-threaded.jspi.mjs": "./dist/ort-wasm-simd-threaded.jspi.mjs",
126+
"./ort-wasm-simd-threaded.asyncify.mjs": "./dist/ort-wasm-simd-threaded.asyncify.mjs"
119127
},
120128
"types": "./types.d.ts",
121129
"description": "A Javascript library for running ONNX models on browsers"

js/web/test/e2e/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"rollup": "^4.13.2",
2121
"rollup-plugin-copy": "^3.5.0",
2222
"tree-kill": "^1.2.2",
23+
"vite": "^7.1.12",
2324
"webpack-cli": "^5.1.4"
2425
},
2526
"scripts": {
@@ -32,7 +33,10 @@
3233
"build:p:esmjs": "parcel build --no-autoinstall --target esm && node ./bundler.esm.postprocess.js ./dist/parcel_esm_js/main.js",
3334
"build:p:umdjs": "parcel build --no-autoinstall --target umd",
3435
"build:p": "npm run build:p:esmjs && npm run build:p:umdjs",
35-
"build": "npm run build:w && npm run build:r && npm run build:p"
36+
"build:v:esmjs": "vite build -c vite.config.esm-js.js",
37+
"build:v:umdjs": "vite build -c vite.config.umd-js.js",
38+
"build:v": "npm run build:v:esmjs && npm run build:v:umdjs",
39+
"build": "npm run build:v && npm run build:w && npm run build:r && npm run build:p"
3640
},
3741
"@parcel/resolver-default": {
3842
"packageExports": true

js/web/test/e2e/run-data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const BUNDLER_TEST_CASES = [
7777
['./dist/rollup_umd_js/ort-test-e2e.bundle.js', 'iife'],
7878
['./dist/parcel_esm_js/main.js', 'esm'],
7979
['./dist/parcel_umd_js/main.js', 'iife'],
80+
['./dist/vite_esm_js/ort-test-e2e.bundle.mjs', 'esm'],
81+
['./dist/vite_umd_js/ort-test-e2e.bundle.js', 'iife'],
8082
];
8183

8284
module.exports = {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require('onnxruntime-web/ort-wasm-simd-threaded.wasm?url&no-inline');
2+
require('onnxruntime-web/ort-wasm-simd-threaded.mjs?url&no-inline');
3+
4+
require('onnxruntime-web/ort-wasm-simd-threaded.jsep.wasm?url&no-inline');
5+
require('onnxruntime-web/ort-wasm-simd-threaded.jsep.mjs?url&no-inline');
6+
7+
require('onnxruntime-web/ort-wasm-simd-threaded.jspi.wasm?url&no-inline');
8+
require('onnxruntime-web/ort-wasm-simd-threaded.jspi.mjs?url&no-inline');
9+
10+
require('onnxruntime-web/ort-wasm-simd-threaded.asyncify.wasm?url&no-inline');
11+
require('onnxruntime-web/ort-wasm-simd-threaded.asyncify.mjs?url&no-inline');
12+
13+
require('./main');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'onnxruntime-web/ort-wasm-simd-threaded.wasm?url&no-inline';
2+
import 'onnxruntime-web/ort-wasm-simd-threaded.mjs?url&no-inline';
3+
4+
import 'onnxruntime-web/ort-wasm-simd-threaded.jsep.wasm?url&no-inline';
5+
import 'onnxruntime-web/ort-wasm-simd-threaded.jsep.mjs?url&no-inline';
6+
7+
import 'onnxruntime-web/ort-wasm-simd-threaded.jspi.wasm?url&no-inline';
8+
import 'onnxruntime-web/ort-wasm-simd-threaded.jspi.mjs?url&no-inline';
9+
10+
import 'onnxruntime-web/ort-wasm-simd-threaded.asyncify.wasm?url&no-inline';
11+
import 'onnxruntime-web/ort-wasm-simd-threaded.asyncify.mjs?url&no-inline';
12+
13+
import './main.js';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
const path = require('node:path');
5+
const { defineConfig } = require('vite');
6+
7+
module.exports = defineConfig({
8+
build: {
9+
outDir: path.resolve(__dirname, 'dist/vite_esm_js'),
10+
emptyOutDir: true,
11+
sourcemap: false,
12+
lib: {
13+
name: 'testPackageConsuming',
14+
entry: path.resolve(__dirname, 'src/esm-js/vite-main.js'),
15+
fileName: () => 'ort-test-e2e.bundle.mjs',
16+
formats: ['es'],
17+
},
18+
minify: false,
19+
assetsDir: './',
20+
assetsInlineLimit: 0,
21+
},
22+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
const path = require('node:path');
5+
const { defineConfig } = require('vite');
6+
7+
module.exports = defineConfig({
8+
build: {
9+
outDir: path.resolve(__dirname, 'dist/vite_umd_js'),
10+
emptyOutDir: true,
11+
sourcemap: false,
12+
lib: {
13+
name: 'testPackageConsuming',
14+
entry: path.resolve(__dirname, 'src/esm-js/vite-main.js'),
15+
fileName: () => 'ort-test-e2e.bundle.js',
16+
formats: ['umd'],
17+
},
18+
minify: false,
19+
assetsDir: './',
20+
assetsInlineLimit: 0,
21+
commonjsOptions: {
22+
include: ['**/*.js'],
23+
exclude: [],
24+
transformMixedEsModules: true,
25+
ignoreDynamicRequires: true,
26+
},
27+
},
28+
});

0 commit comments

Comments
 (0)