-
Notifications
You must be signed in to change notification settings - Fork 25
Add an asset entry point #31
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
Comments
Just pulling this out as a separate topic @myl7, to avoid confusing that thread too much. Can you explain more about how this works, and exactly why it's useful? Is there a good reason why we'd need to do this here, instead of fixing the target (or adding a new target) in the wasm-pack project itself? I'd really like to avoid maintaining more and more complicated entrypoints if we can - it would be much better if wasm-pack could handle this properly. That's less maintenance for each of us on these packages, but would also make it much easier for everybody else building wasm packages elsewhere to do the same thing automatically. |
The asset entry should be like this, as in brotli-dec-wasm.
By doing the above, you can write code like this, which will greatly improve the development experience by allowing the vite side to manage the publication of the asset, etc. Also, to use it with cloudflare workers, you must be able to specify the path, as per this documentation. In other words, you must be able to create a WebassemblyModule based on the path. The way to do so is to expose the init function. The code generated by wasm-pack is thought out in this regard, and if you do not want to wrap it on this side of the library, we recommend that you expose it as an import init, {
BrotliDecStream,
BrotliStreamResultCode,
} from "brotli-dec-wasm/web"; // Import the default export
// @ts-ignore
import brotli_dec_wasm_bg from "brotli-dec-wasm/web/bg.wasm?wasm"; // Import the wasm file
const promise = init(brotli_dec_wasm_bg); // Import is async in browsers due to wasm requirements! |
With the changes you see in this pull request, I am able to write code like this. // @ts-ignore
import init, { decompress, compress } from "brotli-wasm/web"; // Import the default export
// @ts-ignore
import brotli_dec_wasm_bg from "brotli-wasm/web/bg.wasm?wasm&url"; // Import the wasm file
console.log("brotli_dec_wasm_bg", brotli_dec_wasm_bg);
const promise = init(brotli_dec_wasm_bg); // Import is async in browsers due to wasm requirements!
export const brotli_compress = async (data: Uint8Array) => {
await promise;
return compress(data, {
quality: 11,
});
};
// export const brotli_compress = () => {};
// export const brotli_decompress = () => {};
export const brotli_decompress = async (data: Uint8Array) => {
await promise;
return decompress(data);
}; cloudflare docs: Also, to use it with cloudflare workers, you must be able to specify the path, as per this documentation. In other words, you must be able to create a WebassemblyModule based on the path. The way to do so is to expose the init function. |
Originally posted by @myl7 in #18 (comment)
The text was updated successfully, but these errors were encountered: