如果我使用 uglify,以模块化方式使用 lodash 有什么好处吗?

Is there any advantage to consuming lodash in a modular way if I am using uglify?

this performance audit of a React site, the lodash author suggests "cherry-picking" methods in lodash (i.e., requireing individual methods instead of the whole lodash package) or using the individual packages 的评论中。但是,如果使用死代码消除器(例如 uglify 或闭包编译器)作为捆绑过程的一部分(使用 browserify 或 webpack),这难道不是不必要的吗?

如果您使用的是 CommonJS 风格 require('lodash'),Uglify 不会消除死代码(我认为 Closure Compiler 也不能)。

但是,如果您使用的是 ES6 风格 import { merge, reduce } from 'lodash'(例如),您将能够使用 Webpack2(其他一些捆绑器也支持)利用 tree-shaking。由于 ES6 模块系统是静态结构,bundler 可以进行静态分析并确定库的某些部分未被使用并消除它们。

有关使用 Webpack 2 进行 tree-shaking 的更多信息,您可以查看 http://www.2ality.com/2015/12/webpack-tree-shaking.html

截至撰写此答案(2016 年 3 月 3 日),Webpack 2 仍处于测试阶段,因此可能存在一些问题,但如果您想尝试一下,请尝试一下!