The less code you can send to the browser, the better. The concept of tree shaking basically says that if you’re not using some piece of code, then exclude it from the final bundle, even when that piece of code is exported from a module. Because ES6 modules are statically analyzable, Webpack can determine which of your dependencies are used and which are not. In this lesson, see how to take advantage of this awesome feature in Webpack 2.

One of the aspects of ES6 modules is that they're statically analyzable. This means tools like Webpack can predict exactly which exports are used and which are not. 
Bable is transpiling our ES6 module exports into common JS exports. Because this is getting transpiled down to common JS, it's not statically analyzable like ES6 modules, and so, Webpack can't reliably treeshake this function, and our bundle will include it even though it's not in use.
 
We're going to use a different Bable preset, which excludes the transpilation of ES6 modules and leave that to Webpack.
 
 
install:
npm install babel-preset-es2015-webpack --save-dev

.babelrc: change 'es2015 to 'es2015-webpack'

 "presets": ["es2015-webpack", "stage-2"],

So now if you have a function unused anywhere in the project, but still get exported from the file. Then webpack will mark it and when minify the file, this function will be remove from the source.

[Webpack 2] Tree shaking with Webpack 2的更多相关文章

  1. webpack使用tree shaking的问题。及关于UglifyJs不支持ES6的解决方案。

    webpack: plugins:[ new webpack.optimize.UglifyJsPlugin({ compress:{warning:true} }) ] 是的,一些dead code ...

  2. Webpack 的 Tree Shaking

    为什么要使用 Tree Shaking? 当从某文件模块中导出(某一个或几个变量.函数.对象等),然而这个文件模块还有许多其它(我们这次并不需要)的导出,webpack会不管三七二十一简单粗暴的将整个 ...

  3. Webpack 4 Tree Shaking 终极优化指南

    几个月前,我的任务是将我们组的 Vue.js 项目构建配置升级到 Webpack 4.我们的主要目标之一是利用 tree-shaking 的优势,即 Webpack 去掉了实际上并没有使用的代码来减少 ...

  4. 深入研究webpack之Tree Shaking相关属性sideEffects用处

    Tree Shaking我原来也只是了解,这次碰巧深入研究了下,就写个博客记录一下,网上有很多讲Tree Shaking的,我写的这篇跟他们侧重点不一样 Tree Shaking相关的基础知识 1 w ...

  5. webpack和tree shaking和rollup

    http://blog.csdn.net/haodawang/article/details/77199980 tree shaking只对es模块生效,在打包tyscript模块是要使用tsc编译器 ...

  6. Webpack 4教程 - 第七部分 减少打包体积与Tree Shaking

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.原文出处:https://wanago.io/2018/08/13/webpack-4-course-part ...

  7. webpack(6)-模块热替代&tree shaking

    模块热替换(hot module replacement 或 HMR) 模块热替换(hot module replacement 或 HMR)是 webpack 提供的最有用的功能之一.它允许在运行时 ...

  8. 配置Tree Shaking来减少JavaScript的打包体积

    译者按: 用Tree Shaking技术来减少JavaScript的Payload大小 原文: Reduce JavaScript Payloads with Tree Shaking 译者: Fun ...

  9. 深入浅出的webpack构建工具---tree shaking打包性能优化(十二)

    阅读目录 1. 什么是tree-shaking? 2. 在webpack中如何使用 tree-shaking 呢? 3. 使用webpack-deep-scope-plugin 优化 回到顶部 1. ...

随机推荐

  1. c++ 标准委员会网址

    c++ 标准委员会网址: http://www.open-std.org/ C++11 文档网址: http://www.open-std.org/jtc1/sc22/wg21/docs/papers ...

  2. Hbase案例分析(二)

    情景1:

  3. Matlab中Rand()函数用法

    一.理论准备 matlab函数randn:产生均值为0,方差 σ^2 = 1,标准差σ = 1的正态分布的随机数或矩阵的函数. 用法:Y = randn(n),返回一个n*n的随机项的矩阵.如果n不是 ...

  4. discuz 门户栏目URL跳转异常的问题

    “SEO设置-URL静态化”的问题.

  5. OA学习笔记-001-项目介绍

    基本知识 框架工具 解决方案(经典应用) 项目 12天 ========================================== OA项目, 12天 BBS 一.什么是OA? 辅助管理.提 ...

  6. 忽然想到,为什么以前iOS的工资高

    听说做iOS的在上海工资也可以  前几年还行,现在也不太行了除非你水平很高 现在移动应用已经100多万了,基本饱和了以前是新的商业模式要抢时间,会的人也少

  7. 5.android系统裁剪

    我手上的android的SDK,是全志A10方案的android4.0.4.由于公司基于这个平台开发一款设备,需要把android自带的软件以及厂家自带的软件去除掉.就研究如何去裁剪android的a ...

  8. Learning WCF Chapter2 Service Description

    While messaging protocols are responsible for message serialization formats,there must be a way to c ...

  9. m2e使用问题——发布web项目时lib目录下的jar包未发布

    解决过程如下: 在项目上点右键查看properties—>Deployment Assembly,看Deploy Path这项上是否缺少WEB-INF/lib这一项. 相关操作截图如下:

  10. Mac 下配置XAMPP

    1:去官方下载 2:安装dmg 3:安装完成后, 网页上提示, 要设置相应的密码, 设置完成. 4:打开对应的app程序, 把 mysql Database运行起来, 不然, 网页上看到的就是未运行状 ...