Tree Shaking
tree shaking指的是将js环境中的无用代码去除,或者叫按需引入。它依赖于es6的import/export模块。webpack2开始支持es6的module特性。
Example
假设main.js导出了2个函数,square和cube
// This function isn't used anywhere
export function square(x) {
return x * x;
}
// This function gets included
export function cube(x) {
return x * x * x;
}
在main.js中,我们选择性的引入来cube函数
import {cube} from './maths.js';
console.log(cube(5)); // 125
执行node_modules/.bin/webpack main.js dist.js ,查看dist.js,会发现square函数没有被引入。
对于生产环境同理。