-
Notifications
You must be signed in to change notification settings - Fork 19
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
Does not work with Vite/Browser #8
Comments
Basically, I think the |
Hmmmmm, I don't know anything about Vite I'm afraid! This project definitely does work in browsers in some configurations (I'm using it with Webpack 4 in multiple projects, and the test suite in this repo builds and tests it using Webpack 5). I'm not sure what the difference is in the Vite case though! What should happen is that:
Any idea where that's going wrong? When you say the For reference the working webpack 5 config is here (there's a |
Basically, Vite uses Esbuild + some extra magic for dependencies during development. Webpack's performance is atrocious after all. With the magic stuff enabled, I can't quite figure out what exactly Vite does. With it disabled, it pretty much uses normal ES module imports.
at which point the web browser goes "Uncaught ReferenceError: module is not defined" and fails. |
So my next step is trying out import * as brotliPromise from "brotli-wasm/pkg.bundler/brotli_wasm.js";
console.log(brotliPromise); This actually correctly imports the module. However, it then fails, since Vite on the other hand can't handle it as nicely and ends up doing the following: I can see a few different options:
|
Everything inside the That same code is used by more or less 100% of Rust+WASM packages that exist for JS. If that part isn't working, then it's a Vite and/or wasm-pack bug, there's not much to do here. The only custom code in this repo is the promise wrapper in |
I see, thank you very much. As for the future, apparently there is an active proposal to properly fix this at a language level |
I guess this can then be closed as a combination of "wontfix" and "upstream issue 🐟". |
Ok! Hope that points you towards a solution eventually. If you do find a fix and there are small changes that can be made here to support Vite without breaking the existing setup then PRs are very welcome 😄. In the meantime I'm actually going to leave this open - it's still a real issue regardless, and maybe this will eventually point towards some useful info for any other Vite users hitting the same problem. Even if it is an upstream issue that wasm-pack can fix, we'll need to pull through an update to actually sort it here afterwards too. |
@stefnotch Have you found a solution? I just bumped in the same issue(s) in the same order ;) |
@kyr0 Sadly not really. My workaround ended up being copying a lot of brotli-wasm's code into my project (don't forget about the licensing part), and only importing the |
I don't have a good answer for you either @kyr0 but I think this is a general problem with wasm-pack, and you should ping them about it, e.g. over here: rustwasm/wasm-pack#1106. I expect this probably affects all Vite users for all Wasm-Pack projects (i.e. basically every Rust-via-WASM library anywhere) so if you're keen on Vite it's well worth getting this fixed more generally. Might also be worth opening an issue with Vite too to see if there's solutions on that side. If you do get any information on ways to work around this, or if there's any improvements on the Wasm-Pack side then do share that here, I'd be happy to fix this in brotli-wasm if possible. |
Update from the future: It now almost works out of the box. https://github.com/stefnotch/url-catpressor/blob/main/src/useCompression.ts |
We came to the same issue. Looks like there is a way to workaround it without changing vite config: import init, * as brotli from "../../node_modules/brotli-wasm/pkg.web/brotli_wasm";
import wasmUrl from "../../node_modules/brotli-wasm/pkg.web/brotli_wasm_bg.wasm?url";
const brotliPromise = init(wasmUrl).then(() => brotli);
The only problem here is that I need to specify relative path to module. When I try as @stefnotch @pimterry I'm not bundle expert, but I think some additional items should be added to "exports": {
".": {
"import": "./index.web.js",
"browser": "./index.browser.js",
"require": "./index.node.js",
"default": "./index.web.js"
}
} After that it would be nice to use it like, or something better: import init, * as brotli from "brotli-wasm/brotli_wasm";
import wasmUrl from "brotli-wasm/brotli_wasm_bg.wasm?url";
const brotliPromise = init(wasmUrl).then(() => brotli); |
I tried out the example in the Readme, and after fixing it #7 and tweaking it to use TextEncoder/TextDecoder, it refused to work. At this point, I decided to inspect the imported object
To my surprise, it's basically empty.
![image](https://user-images.githubusercontent.com/10220080/164022895-dc9db569-e9d7-47dd-aea3-0ba187d8de0f.png)
Then I decided to cut to the chase and directly imported the relevant bits and pieces.
However, this fails since the WASM object doesn't seem to have been loaded yet.
![image](https://user-images.githubusercontent.com/10220080/164022649-58225b68-f5cb-41c6-916b-cba3c5a94cd3.png)
For what it's worth, a different project of mine used Brotli-Wasm. Back then, I couldn't figure it out either and just copy-pasted the code + license. Then, I fixed it up a bit... https://github.com/stefnotch/starboard-editor/blob/d51bc17673385ad192417592add0092881379bfb/src/useCompression.ts#L1
The text was updated successfully, but these errors were encountered: