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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ module.exports.default = module.exports;

// Without this, ts-loader gets annoyed by imports for the pure type. Clear ts-loader bug,
// but this is a quick & easy fix on our end:
module.exports.BrotliWasmType = undefined;
module.exports.BrotliWasmType = undefined;

module.exports.init = (_) => {
return globalThis.Promise.resolve();
}
19 changes: 18 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ export * from './pkg.node/brotli_wasm';
declare const promisedValue: Promise<typeof BrotliWasm>;
export default promisedValue;

export type BrotliWasmType = typeof BrotliWasm;
export type BrotliWasmType = typeof BrotliWasm;

import type { InitInput, InitOutput } from './pkg.web/brotli_wasm';

/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* If this project's target is `cloudflare-workers`, you must use this.
* If you use web, this function will return a promise that resolves to the
* `BrotliWasm` module, which you can use directly.
* But if you use other targets, this function will return the void type.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
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.

export function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput | void>;
6 changes: 5 additions & 1 deletion index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ const nodePkg = require('./pkg.node/brotli_wasm');
module.exports = nodePkg;

// In addition though, we provide a default export, to match the pure ESM web bundle:
module.exports.default = Promise.resolve(nodePkg);
module.exports.default = Promise.resolve(nodePkg);

module.exports.init = (_) => {
return globalThis.Promise.resolve();
}
4 changes: 3 additions & 1 deletion index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
// For usage with an importmap, it's convenient to add the ".js" extension here, because browsers
// don't try to guess the file extension.
import init, * as brotliWasm from "./pkg.web/brotli_wasm.js";
export default init().then(() => brotliWasm);
export default init().then(() => brotliWasm);

export { init };
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
"browser": "./index.browser.js",
"require": "./index.node.js",
"default": "./index.web.js"
},
"./wasm": {
"import": {
"types": "./pkg.web/brotli_wasm_bg.wasm.d.ts",
"default": "./pkg.web/brotli_wasm_bg.wasm"
},
"browser": {
"types": "./pkg.bundler/brotli_wasm_bg.wasm.d.ts",
"default": "./pkg.bundler/brotli_wasm_bg.wasm"
},
"require": {
"types": "./pkg.node/brotli_wasm_bg.wasm.d.ts",
"default": "./pkg.node/brotli_wasm_bg.wasm"
},
"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.

}
},
"sideEffects": false,
Expand Down
Loading