[Webpack 2] Tree shaking with Webpack 2

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.

时间: 2024-08-09 10:39:05

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

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

webpack: plugins:[ new webpack.optimize.UglifyJsPlugin({ compress:{warning:true} }) ] 是的,一些dead code 在打包后会被移除.比如没用的varible ,function. 但是classes会被UglifyJs作为side effect,然后跳过它. 我的TypeScript配置tsconfig.json : compilerOptions:{ target:'es5', module:'es2015

Webpack 的 Tree Shaking

为什么要使用 Tree Shaking? 当从某文件模块中导出(某一个或几个变量.函数.对象等),然而这个文件模块还有许多其它(我们这次并不需要)的导出,webpack会不管三七二十一简单粗暴的将整个模块包含进来,使得我们最终打包的文件里有了许多不需要的垃圾.这就到了tree shaking出手的地方了,因为它能帮助我们干掉那些用不到的代码,大大减少打包的尺寸. 要想让tree shaking能"摇起来",有几个要求: 第一个要求,必须使用ES6模块,不能使用其它类型的模块如Commo

The way of Webpack learning (V.) -- css和js的tree shaking

一:基本概念 1.字面意思是摇树,一句话:项目中没有使用的代码会在打包时候丢掉.分为css的tree shaking和js的tree shaking. 2.使用场景:1)常规优化.2)使用第三方库,但是只使用了部分功能. 二:JS tree shaking 在webpack4中已经移除了UglifyJsPlugin,只需要配置mode为"production",即可显式激活 UglifyjsWebpackPlugin 插件. 下面说的是webpack3.10.0的实现方法: (1)常规

webpack4 系列教程(八): CSS Tree Shaking

教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步原文地址 有空就来看看个人技术小站, 我一直都在 0. 课程介绍和资料 本次课程的代码目录(如下图所示): >>> 本节课源码 >>> 所有课程源码 1. CSS 也有 Tree Shaking? 是滴,随着 webpack 的兴起,css 也可以进行 Tree Shaking: 以去除项目代码中用不到的 CSS 样式,仅保留被使用的样式代码. 为了方便理解 Tree Shaking 概念,并且与 JS

关于webpack的cannot find module 'webpack/lib/node/NodeTemplatePlugin'报错问题

近期公司的项目正在研究使用webpack及vue的使用,遇到了一个问题的报错,上边是报错,说找不到这个文件下的一个模块,但是我去这个文件夹下面找了这个文件是确实存在的,所以各种寻找解决方案,最终发现在全局安装完以后,在你的本地项目上面在安装一个本地的webpack就可以了,下边是我在本地重新安装的.最后成功运行了. 关于webpack的cannot find module 'webpack/lib/node/NodeTemplatePlugin'报错问题

webpack渐入佳境系列一:webpack环境配置与打包基础【附带各种 "坑" 与解决方案!持续更新中...】

首先介绍传统模块化开发的主流方案: 1.基与CMD的sea.js,玉伯提出的解决方案,据说原来京东团队在使用.用时才定义,就近加载.最近在浏览seajs官方文档时发现seajs的域名已经在转卖,惊恐万分之余又想起了那句话,国内团队开发的技术,不是十全九美的最好不要用.国内高强度的生活节奏与压力使得日常维护变得很吃力.(想到这里,不禁为vue捏了一把汗,不过还好vue的前景非常光明!). 2.基于AMD的require.js,之前在用.提前声明与定义.国外团队维护. 3.基于commonJS的we

Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)

概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loading就没法搞了. 因此我使用了webpack2,webpack2自带tree shaking,只要将tsconfig中的module设置成es2015就可以, 虽然效果没rollup好,但支持lazy loading. 另外, angular2目前不支持typescript 2.1.X,所以如果

webpack入门(四)——webpack loader 和plugin

什么是loader loaders是你用在app源码上的转换元件.他们是用node.js运行的,把源文件作为参数,返回新的资源的函数. 例如,你可以用loaders告诉webpack加载 coffeeScript或者JSX. loaders 特点: 1. 可以链式拼接.他们用在通向文件的管道,最后一个loader预期返回一个javascript,其它Loader可以返回任意格式给下一个loader. 2. loaders可以是同步的,也可以是异步的. 3. loaders是用node.js来跑,

webpack安装填坑('webpack' 不是内部或外部命令,也不是可运行的程序或批处理文件)

差不多每次安装软件都是一个波折的过程,这次webpack的安装也不例外,安装成功之后立马来记录解决错误的方法,以备不时之需 前提条件 webpack是依赖于node安装的,所以在安装前要确保自己是否安装了node.js,在命令行运行node -v如果情况如下就是安装了node.js,如果没有安装请参考node.js的安装步骤 注意:记得在安装的node目录下创建两个文件夹然后,配置npm路径 配置全局路径:npm config set prefix "d:\nodejs\node_global&