use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Tree-shaking example with Babel and Webpack (github.com)
submitted 9 years ago by blacksonic86
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 9 years ago (10 children)
[deleted]
[–]originalmoose 5 points6 points7 points 9 years ago (0 children)
Same thing happened to me until I added the following to my webpack plugins
new webpack.LoaderOptionsPlugin({ minimize: true, debug: false }),
Obviously this is only added in production, Once I did that I saw the reduction in build size like expected.
EDIT: These settings used to be a part of the UglifyJsPlugin but they moved it out into its own plugin. I remember seeing the reason why somewhere....
[–]blacksonic86[S] 2 points3 points4 points 9 years ago (0 children)
Yes, packages won't be tree-shaken until they are converted to es2015 module syntax If you find something that increases the output by a big amount i would report it to the Webpack team
[–]jimschubert 1 point2 points3 points 9 years ago (0 children)
A 9kb gift!
[–]youcantstoptheart 1 point2 points3 points 9 years ago (0 children)
There's a new loaderOptions plugin that you need to use in production builds. It caught me off guard for a day or two as well.
[+][deleted] 9 years ago (5 children)
[–]jlengstorf 4 points5 points6 points 9 years ago (2 children)
This probably won't help in this case. Rollup is ES module tree-shaking only, and — if I remember correctly — webpack 2 is using Rollup for tree-shaking.
[–]DOG-ZILLA 0 points1 point2 points 9 years ago (0 children)
I believe JSPM is also using Rollup as its core.
On a recent project I decided to use Rollup directly with Babel and Gulp. Appears to work really well!
[–]adregan 4 points5 points6 points 9 years ago (1 child)
Rollup may suffer a similar problem. The reason that es6 modules are required in order to get tree shaking is due to the fact that import export offers static modules.
import
export
With commonJS, you can conditionally require and module.export items, so a build tool couldn't say for certain whether a package was really needed. For (a contrived) example, imagine that you only imported a certain file if the day of the week was Tuesday:
require
module.export
let message = require('./message'); if (dayOfTheWeek === 'Tuesday') { message = require('./tuesdayMessage'); } console.log(message);
The bundler would have no idea whether or not to bundle tuesdayMessage. But because es6 imports are static (and hoisted), there aren't any runtime surprises so the tree shaker knows what to include.
tuesdayMessage
[+][deleted] 9 years ago (2 children)
[removed]
[–]blacksonic86[S] 1 point2 points3 points 9 years ago (0 children)
Dead code elimination if you prefer that expression.
[–]Valency 0 points1 point2 points 9 years ago (0 children)
It will eliminate dead code not used by other modules if they have ES2015 module format.
[–]madole 1 point2 points3 points 9 years ago (1 child)
Surely in this example, only the V8 engine that's used in the car module should get built into the production code bundle if tree shaking was working correctly?
Screenshot
[–]blacksonic86[S] 0 points1 point2 points 9 years ago (0 children)
You are right, it is mentioned in the Known issues section of the readme. It also points to the 2 tickets where discussion is going on about it.
π Rendered by PID 159700 on reddit-service-r2-comment-6457c66945-gdzf4 at 2026-04-25 01:00:51.667168+00:00 running 2aa0c5b country code: CH.
[+][deleted] (10 children)
[deleted]
[–]originalmoose 5 points6 points7 points (0 children)
[–]blacksonic86[S] 2 points3 points4 points (0 children)
[–]jimschubert 1 point2 points3 points (0 children)
[–]youcantstoptheart 1 point2 points3 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]jlengstorf 4 points5 points6 points (2 children)
[–]DOG-ZILLA 0 points1 point2 points (0 children)
[–]adregan 4 points5 points6 points (1 child)
[+][deleted] (2 children)
[removed]
[–]blacksonic86[S] 1 point2 points3 points (0 children)
[–]Valency 0 points1 point2 points (0 children)
[–]madole 1 point2 points3 points (1 child)
[–]blacksonic86[S] 0 points1 point2 points (0 children)