Skip to content

Add Brotli WASM module imports to package.json #40

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

oligamiq
Copy link

#31

@CLAassistant
Copy link

CLAassistant commented Jan 20, 2025

CLA assistant check
All committers have signed the CLA.

@pimterry
Copy link
Member

Nice work, thanks @oligamiq! I'm away this week I'm afraid but I'll have a proper look at this and test everything next week and get back to you.

@oligamiq oligamiq force-pushed the main branch 2 times, most recently from 084b2fd to 6f7a6fd Compare January 24, 2025 18:03
Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

Still some open questions here I think. It would also be helpful if you could sign the CLA (see the CLA Assistant comment above)

package.json Outdated
"./web/bg.wasm": {
"types": "./pkg/brotli_wasm_bg.wasm.d.ts",
"import": "./pkg/brotli_wasm_bg.wasm",
"default": "./pkg/brotli_wasm_bg.wasm"
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't work as /pkg/ doesn't exist.

package.json Outdated
@@ -12,6 +12,16 @@
"browser": "./index.browser.js",
"require": "./index.node.js",
"default": "./index.web.js"
},
"./web": {
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain more about why we need a separate /web path? The brotli_wasm.js file is already imported in index.web.js - can't we just export the methods directly from there? That would also allow us to also provide a single API to do this that worked for all targets.

I do agree there's improvements to make here, but I'm not sure that just adding more exports is correct. It really seems like we should be able to provide a small set of exports, correctly automatically select the right one for the environment, and export data in the formats required.

If that needs to be "JS export + raw WASM" export for environments that need to manage wasm import & init manually, that makes sense, but I'm not clear why we'd need "JS export + web JS export + web raw WASM export", which seems like what we're doing here.

@TimTheBig
Copy link

Will this be merged soon?

@pimterry
Copy link
Member

pimterry commented Jul 4, 2025

@TimTheBig see my comments above, this isn't mergable at the moment. If @oligamiq has time to resolve those or if you want to open a new PR for this, I'd be happy to review and keep this moving forward.

@oligamiq
Copy link
Author

oligamiq commented Jul 4, 2025

If that needs to be "JS export + raw WASM" export for environments that need to manage wasm import & init manually, that makes sense, but I'm not clear why we'd need "JS export + web JS export + web raw WASM export", which seems like what we're doing here.

The init function is provided solely for the web target, and if one were to tack it-along with its type-onto the JS exports, it would simply become noise for everyone else.
Therefore, I’ve separated the entry points, though if that’s your preference, I can of course make the adjustment for you.

As for importing the WASM asset, we may choose either of these:

import wasm from '../node_modules/brotli-wasm/pkg.web/brotli_wasm_bg.wasm'
import wasm from 'brotli-wasm/web/bg.wasm'

but for the sake of elegance, I’ve enabled the latter.
I can scarcely imagine a scenario in Node where this would be required, but should you wish to unify the entry points, one ought also to unify the import to something like: import wasm from 'brotli-wasm/bg.wasm'.

@TimTheBig
Copy link

For me the wasm does not work with vite as it does not get packed

@oligamiq
Copy link
Author

oligamiq commented Jul 5, 2025

@TimTheBig

For me the wasm does not work with vite as it does not get packed

It should be able to be written like this.
Your configuration may be out of date.

https://github.com/oligamiq/brotli-wasm/tree/raw-wasm/example/test-raw-wasm

@oligamiq
Copy link
Author

oligamiq commented Jul 6, 2025

Hi @pimterry, would you mind taking a look at this PR when you have a moment?

"default": {
"types": "./pkg.web/brotli_wasm_bg.wasm.d.ts",
"default": "./pkg.web/brotli_wasm_bg.wasm"
}
Copy link
Member

Choose a reason for hiding this comment

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

This seems fine now, and makes perfect sense, and /wasm is a good pattern imo 👍 Very happy to ship this.

* @returns {Promise<InitOutput>}
*/
export function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput | void>;
Copy link
Member

Choose a reason for hiding this comment

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

This API is definitely odd though. I don't think having a stub function that doesn't work (or even return something useful) is going to be nice going forwards. The goal is that you can write the same code and it'll work everywhere, and if init() returns different things in different environments then that's not true.

Can we make the below type work?

export function init(module_or_path?: InitInput | Promise<InitInput>): Promise<BrotliWasmType>;

I'm imagining that would be possible by making every module do either:

export async function init(input) {
  await brotliWasmInit(input);
  return brotliWasm;
}

or

export async function init()  {
  return brotliWasm;
}

That way we can expose an init function that works the same way everywhere, but does call the custom init behaviour on the web case where required. Would that cover your use case? It's quite possible I'm missing something (do you need access to the InitOutput data itself? Which part and why?) I'm just trying to explore consistent APIs to solve this for everybody in the same way instead of fragmenting by environment.

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.

4 participants