question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ES6 & Dead Code Elimination

See original GitHub issue

I recently got pointed rollup, which produces clean builds by assuming the source is using ES6 import/export. It then can mash all the modules into the same closure, instead of the current browser-pack approach that tends to produce a lot of extra boilerplate and closures. It can also tree-shake unused functions.

It would be worth exploring whether something similar is possible in browserify (or, as a plugin/transform independently of browserify).

Big things to figure out:

  • a means of resolving to ES6 source for packages written in ES6 and pre-published to ES5. Right now rollup uses esnext:main which isn’t exactly standard, and maybe could be named better (i.e. what happens with ES7?). It would also be good to have other bundlers like jspm and webpack agree on the same approach.
  • how to take the rollup approach (single closure) rather than bundle-pack approach (closure per module) and what kind of problems this might impose

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
nolanlawsoncommented, Feb 12, 2016

One of the more poorly-understood benefits of Rollup’s approach is that, in addition to tree-shaking, it also hoists modules into a single scope. This is really only possible because of the semantics of ES6 modules, which unlike CommonJS does not enforce a separate function scope for every module.

The traditional CJS solution to tree-shaking is to make your modules as tiny as possible, or use tricks like require('some/deep/func'), but the problem there is that the more you modularize, the more you introduce cruft when you browserify/webpack, due to the required function-scoping boilerplate for every module. You’re punished for doing the right thing.

We noticed this problem in PouchDB, so we recently introduced Rollup as a prebuild step before Browserify, and migrated from CJS to ES6. We had a codebase with lots of small submodules (many files were a single function, 5-20 lines or so). We also had no dead code, as verified by Istanbul coverage tests. However, even without any tree-shaking, Rollup managed to save us 3KB out of 48KB total, which is nothing to sneeze at.

If Browserify or a Browserify plugin could do this same trick, we’d probably be happy to cut Rollup out of our build pipeline and use Browserify only. 😃

18reactions
ghostcommented, Nov 19, 2017

I think we can close this issue now thanks to the great work of @indutny and @goto-bus-stop with common-shake, browser-pack-flat, and common-shakeify.

Using a combination these tools:

you’ll get tree shaking and dead code elimination.

Or you can use tinyify as a plugin which wraps up the aforementioned tools into an easy-to-use zero-configuration package:

browserify -p tinyify main.js

bankai includes these tinyify optimizations by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >
Dead Code Elimination in Javascript - Deepak Pai's Blog
Dead code elimination is a process wherein code that is not used is excluded from the code that is executed. In many compile...
Read more >
Dead-code elimination: Hint uglify-js that your function is pure
uglify-js will do dead code removal, ie: it will remove code that will not affect the program results. Since we defined a and...
Read more >
Tree Shaking in JavaScript - byby.dev
Tree shaking is a form of dead code elimination in JavaScript, relies on es6 modules, and implemented in module bundlers.
Read more >
Tree Shaking - How to Clean up Your JavaScript - KeyCDN
Targeting dead code used to be much more difficult in dynamic languages, but thanks to the static nature of ECMAScript 6 modules, it's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found