You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constinternalAdd=(a,b)=>(a==='0' ? b : a+b);// should not appear in bundleconstaddAndLog=(a,b,impl=internalAdd)=>console.log(impl(a,b));constmyAdd=(a,b)=>a+b;addAndLog(1,2,myAdd);// => 3
馃敠 Context
I maintain Libauth, a library that offers WebAssembly crypto implementations (ripemd160, sha1, sha256, sha512, and secp256k1). As a pure ESM library, Libauth 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:
Most applications can call decodeBase58Address(address) to automatically use the default, WASM-based sha256 implementation (internalSha256).
However, 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, saving hundreds of KBs from those bundles.
I agree with evanw/esbuild#2185 (comment). The only DCE done by Parcel itself is excluding unused files (if they have sideEffects: false). Apart from that it just tries to transform as many imports into regular top-level functions calls as possible (and not with a namespace object as i.e. Babel does _module.addAndLog()). The actual dead code is then removed by Terser/whatever minified you want to use afterwards.
馃檵 Feature Request
During dead-code elimination/tree shaking, default parameters that aren't used by the bundle can be removed.
馃 Expected Behavior
For this simple example:
module.js
:app.js
:The produced bundle should include only:
馃槸 Current Behavior
Currently, parcel bundles the dead code:
馃敠 Context
I maintain Libauth, a library that offers WebAssembly crypto implementations (
ripemd160
,sha1
,sha256
,sha512
, andsecp256k1
). As a pure ESM library, Libauth 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:Most applications can call
decodeBase58Address(address)
to automatically use the default, WASM-basedsha256
implementation (internalSha256
).However, 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, saving hundreds of KBs from those bundles.馃捇 Examples
A detailed example (testing with all known bundlers) is available at this repo: https://github.com/bitjson/shake-default-params
The text was updated successfully, but these errors were encountered: