Skip to content

Commit 0fd6db4

Browse files
committed
Add init function and update type definitions for BrotliWasm
1 parent f110150 commit 0fd6db4

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

index.browser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ module.exports.default = module.exports;
99

1010
// Without this, ts-loader gets annoyed by imports for the pure type. Clear ts-loader bug,
1111
// but this is a quick & easy fix on our end:
12-
module.exports.BrotliWasmType = undefined;
12+
module.exports.BrotliWasmType = undefined;
13+
14+
module.exports.init = (_) => {
15+
return globalThis.Promise.resolve();
16+
}

index.d.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,21 @@ export * from './pkg.node/brotli_wasm';
66
declare const promisedValue: Promise<typeof BrotliWasm>;
77
export default promisedValue;
88

9-
export type BrotliWasmType = typeof BrotliWasm;
9+
export type BrotliWasmType = typeof BrotliWasm;
10+
11+
import type { InitInput, InitOutput } from './pkg.web/brotli_wasm';
12+
13+
/**
14+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
15+
* for everything else, calls `WebAssembly.instantiate` directly.
16+
*
17+
* If this project's target is `cloudflare-workers`, you must use this.
18+
* If you use web, this function will return a promise that resolves to the
19+
* `BrotliWasm` module, which you can use directly.
20+
* But if you use other targets, this function will return the void type.
21+
*
22+
* @param {InitInput | Promise<InitInput>} module_or_path
23+
*
24+
* @returns {Promise<InitOutput>}
25+
*/
26+
export function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput | void>;

index.node.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ const nodePkg = require('./pkg.node/brotli_wasm');
33
module.exports = nodePkg;
44

55
// In addition though, we provide a default export, to match the pure ESM web bundle:
6-
module.exports.default = Promise.resolve(nodePkg);
6+
module.exports.default = Promise.resolve(nodePkg);
7+
8+
module.exports.init = (_) => {
9+
return globalThis.Promise.resolve();
10+
}

index.web.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
// For usage with an importmap, it's convenient to add the ".js" extension here, because browsers
66
// don't try to guess the file extension.
77
import init, * as brotliWasm from "./pkg.web/brotli_wasm.js";
8-
export default init().then(() => brotliWasm);
8+
export default init().then(() => brotliWasm);
9+
10+
export { init };

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
"browser": "./index.browser.js",
1313
"require": "./index.node.js",
1414
"default": "./index.web.js"
15+
},
16+
"./wasm": {
17+
"import": {
18+
"types": "./pkg.web/brotli_wasm_bg.wasm.d.ts",
19+
"default": "./pkg.web/brotli_wasm_bg.wasm"
20+
},
21+
"browser": {
22+
"types": "./pkg.bundler/brotli_wasm_bg.wasm.d.ts",
23+
"default": "./pkg.bundler/brotli_wasm_bg.wasm"
24+
},
25+
"require": {
26+
"types": "./pkg.node/brotli_wasm_bg.wasm.d.ts",
27+
"default": "./pkg.node/brotli_wasm_bg.wasm"
28+
},
29+
"default": {
30+
"types": "./pkg.web/brotli_wasm_bg.wasm.d.ts",
31+
"default": "./pkg.web/brotli_wasm_bg.wasm"
32+
}
1533
}
1634
},
1735
"sideEffects": false,

0 commit comments

Comments
 (0)