Skip to content

Commit 63ffc72

Browse files
authored
Merge pull request #36 from heidemn/fix-import-for-importmap
Add .js file extension to import, to make importmap work out of the box
2 parents 0842adc + 3a916c4 commit 63ffc72

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,45 @@ console.log(textDecoder.decode(decompressedData)); // Prints 'some input'
6363

6464
You can also load it from a CDN like so:
6565
```javascript
66-
let brotli = await import("https://unpkg.com/brotli-wasm@1.3.1/index.web.js?module").then(m => m.default);
66+
const brotli = await import("https://unpkg.com/brotli-wasm@3.0.0/index.web.js?module").then(m => m.default);
6767
```
6868

6969
The package itself has no runtime dependencies, although if you prefer using `Buffer` over using `TextEncoder/TextDecoder` you may want a [browser Buffer polyfill](https://www.npmjs.com/package/browserify-zlib).
7070

71+
##### Using an importmap
72+
73+
If you've installed `brotli-wasm` as an NPM package, you can load it from your `node_modules` subfolder:
74+
75+
```html
76+
<!-- index.html -->
77+
<!DOCTYPE html>
78+
<html lang="en">
79+
<head></head>
80+
<body>
81+
<script type="importmap">
82+
{
83+
"imports": {
84+
"brotli-wasm": "/node_modules/brotli-wasm/index.web.js"
85+
}
86+
}
87+
</script>
88+
<script type="module" src="/main.js"></script>
89+
</body>
90+
</html>
91+
```
92+
93+
```javascript
94+
// main.js
95+
import brotliPromise from 'brotli-wasm';
96+
const brotli = await brotliPromise;
97+
98+
const input = 'some input';
99+
const uncompressedData = new TextEncoder().encode(input);
100+
const compressedData = brotli.compress(uncompressedData);
101+
const decompressedData = brotli.decompress(compressedData);
102+
console.log(new TextDecoder().decode(decompressedData)); // Prints 'some input'
103+
```
104+
71105
#### In browser with streams:
72106

73107
```javascript

index.web.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// In pure ESM web bundles, you must call init() and wait for the promised result before you can
22
// call any module methods. To make that as easy as possible, this module directly exposes the
33
// init() promise result, and returns the methods at the end of the promise.
4-
import init, * as brotliWasm from "./pkg.web/brotli_wasm";
4+
// https://github.com/WICG/import-maps?tab=readme-ov-file#extension-less-imports
5+
// For usage with an importmap, it's convenient to add the ".js" extension here, because browsers
6+
// don't try to guess the file extension.
7+
import init, * as brotliWasm from "./pkg.web/brotli_wasm.js";
58
export default init().then(() => brotliWasm);

0 commit comments

Comments
 (0)