This demo tests how various bundlers handle Libauth's WebAssembly crypto implementations (ripemd160, sha1, sha256, sha512, and secp256k1).
As a pure ESM library, Libauth v2 can asynchronously instantiate each WASM implementation internally, exporting simple interfaces that behave like collections of JS-only functions (with better performance).
Many of Libauth's exported functions also use one of these built-in WASM instances as a default parameter. For example, decodeBase58Address has the definition:
export const decodeBase58Address = (
address: string,
sha256: { hash: (input: Uint8Array) => Uint8Array } = internalSha256
) => {
// ...
};Most applications can call decodeBase58Address(address) to automatically use the default, WASM-based sha256 implementation (internalSha256). See src/app-default.js.
Applications that already have another sha256 implementation can provide that implementation as the second parameter: decodeBase58Address(address, mySha256Implementation). In this case, the default parameter (internalSha256) is dead code and should be possible to eliminate from the application's bundle (commonly called tree shaking in the JavaScript ecosystem). See src/app-shakable.js.
This repo tests dead-code elimination of unused default parameters as used in Libauth (with asynchronously instantiated WASM implementations): src/app-shakable.js.
Currently, dead-code elimination of unused default parameters is only supported by Rollup.
| Bundler | Support | Issue | PR |
|---|---|---|---|
esbuild |
❌ (example) | evanw/esbuild#2185 |
- |
parcel |
❌ (example) | parcel-bundler/parcel#7961 |
- |
rollup |
⏳ (example) | rollup/rollup#4466 |
rollup/rollup#4498 |
terser |
❌ (example) | terser/terser#1199 |
- |
webpack |
❌ (example) | webpack/webpack#15671 |
- |
This simpler example may also be useful for quickly testing support in bundlers:
package.json:
{
"type": "module"
}module.js:
// should not appear in bundle:
const internalAdd = (a, b) => (a === '0' ? b : a + b);
export const addAndLog = (a, b, impl = internalAdd) => console.log(impl(a, b));app.js:
import { addAndLog } from './module.js';
const myAdd = (a, b) => a + b;
addAndLog(1, 2, myAdd); // => 3