Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js/web] improve workaround for bundlers #23902

Merged
merged 2 commits into from
Mar 6, 2025

Conversation

fs-eire
Copy link
Contributor

@fs-eire fs-eire commented Mar 5, 2025

Description

This PR improves the workaround for bundlers in onnxruntime-web. Specifically, the following changes have been made:

This change allows to remove unnecessary dependencies of file parsed from new URL("ort.bundle.min.js", import.meta.url) in Vite, and optimize code like if("file://filepath.js".startsWith("file:")) {do_sth1(); } else {do_sth2();} into do_sth1() for webpack/terser usages.

Resolves huggingface/transformers.js#1161

@xenova
Copy link
Contributor

xenova commented Mar 5, 2025

Fantastic! Testing this now! 🔥

Copy link
Contributor

@xenova xenova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with Transformers.js & vite and it works well!

Before:

vite v6.2.0 building for production...
✓ 30 modules transformed.
dist/index.html                                             0.46 kB │ gzip:     0.29 kB
dist/assets/ort.bundle.min-D2-GKZ-g.mjs                   392.96 kB
dist/assets/ort-wasm-simd-threaded.jsep-Y7jqkEt_.wasm  23,013.11 kB │ gzip: 5,533.76 kB
dist/assets/index-BaiKPFAq.css                              1.20 kB │ gzip:     0.62 kB
dist/assets/index-Dg6HUVTj.js                             812.49 kB │ gzip:   216.72 kB

After:

vite v6.2.0 building for production...
✓ 30 modules transformed.
dist/index.html                                             0.46 kB │ gzip:     0.30 kB
dist/assets/ort-wasm-simd-threaded.jsep-eo8oczH3.wasm  21,959.93 kB │ gzip: 5,201.83 kB
dist/assets/index-BaiKPFAq.css                              1.20 kB │ gzip:     0.62 kB
dist/assets/index-DDX6gZv3.js                             816.14 kB │ gzip:   217.45 kB

@fs-eire @guschmue, once merged, could we put out a dev build too? 🤗

@fs-eire fs-eire merged commit aafa8d1 into main Mar 6, 2025
56 of 58 checks passed
@fs-eire fs-eire deleted the fs-eire/better-webpack-workaround branch March 6, 2025 00:03
guschmue pushed a commit that referenced this pull request Mar 6, 2025
### Description
This PR improves the workaround for bundlers in onnxruntime-web.
Specifically, the following changes have been made:

- Use [this
workaround](xenova@9c50aa2)
as suggested by @xenova in
huggingface/transformers.js#1161 (comment)

- Use `url > "file:" && url < "file;"` instead of
`url.startsWith("file:")` to allow minifiers to remove dead code
correctly.

This change allows to remove unnecessary dependencies of file parsed
from `new URL("ort.bundle.min.js", import.meta.url)` in Vite, and
optimize code like `if("file://filepath.js".startsWith("file:"))
{do_sth1(); } else {do_sth2();}` into `do_sth1()` for webpack/terser
usages.

Resolves huggingface/transformers.js#1161
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.

3 participants