Skip to content

[Web] Add wasm and mjs files to package.json exports to improve bundler support#26394

Merged
fs-eire merged 3 commits intomicrosoft:mainfrom
KingSora:feat/web-export-wasm-files
Oct 27, 2025
Merged

[Web] Add wasm and mjs files to package.json exports to improve bundler support#26394
fs-eire merged 3 commits intomicrosoft:mainfrom
KingSora:feat/web-export-wasm-files

Conversation

@KingSora
Copy link
Contributor

@KingSora KingSora commented Oct 23, 2025

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. 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:

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:
imports


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.

@KingSora
Copy link
Contributor Author

@fs-eire when I'm trying to run npm run test:e2e in the js/web directory I'm getting the following error: cannot find exactly single package for onnxruntime-web. This error prevents me to write an meaningful test.

@fs-eire
Copy link
Contributor

fs-eire commented Oct 24, 2025

@fs-eire when I'm trying to run npm run test:e2e in the js/web directory I'm getting the following error: cannot find exactly single package for onnxruntime-web. This error prevents me to write an meaningful test.

Looks like the CI passes, so this may indicate that you are not running npm run test:e2e in the expected order. you need run npm pack in /js/common/ and /js/web/ to create the 2 .tgz files and then run the npm run test:e2e

@KingSora
Copy link
Contributor Author

@fs-eire Thanks for the info, npm pack was missing - might be worth adding it to the docs.

I've added some e2e tests which test the said exports with vite and its ?url import feature.

@KingSora
Copy link
Contributor Author

@microsoft-github-policy-service agree

@fs-eire
Copy link
Contributor

fs-eire commented Oct 27, 2025

/azp run Linux QNN CI Pipeline, Win_TRT_Minimal_CUDA_Test_CI, Windows ARM64 QNN CI Pipeline, Windows GPU Doc Gen CI Pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@fs-eire fs-eire merged commit 0600482 into microsoft:main Oct 27, 2025
91 of 92 checks passed
naomiOvad pushed a commit to naomiOvad/onnxruntime that referenced this pull request Nov 2, 2025
…er support (microsoft#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants