Webpack 网页摇树罐’;t提取未使用的函数
当我使用and时,我尝试缩小代码:Webpack 网页摇树罐’;t提取未使用的函数,webpack,tree-shaking,Webpack,Tree Shaking,当我使用and时,我尝试缩小代码: // this is test.js export const ceil = Math.ceil export const random = Math.random // webpack entry import { ceil } from './test' export default ceil(3.3) 我得到: // formated "use strict"; n.d(t,"a",function(){return r}); var r
// this is test.js
export const ceil = Math.ceil
export const random = Math.random
// webpack entry
import { ceil } from './test'
export default ceil(3.3)
我得到:
// formated
"use strict";
n.d(t,"a",function(){return r});
var r=Math.ceil;
Math.random// still exist?
如果我缩小
// this is test.js
export const ceil = Math.ceil
export const random = 1 // or string
我会恢复正常的
那么,为什么Math.random
仍然存在?
摇树不行吗
网页:3.10.0UglifyjsWebpackPlugin:1.1.6
谢谢 你的问题有点不清楚,但是
Math.random
是javascript标准库的一部分,并且总是包含在每个浏览器中。因此,您不能对其进行树摇,但也不必担心其大小:您可以免费“获取”Math.random
换句话说,摇树是一种减少包裹大小的工具。Math.random
的定义一开始就不是包的一部分,它也不会影响包的大小
(注意:Math.ceil的情况也是如此)谢谢你的回答。关于你的答案,我有一个问题:为什么我“得到”
Math.random
,如果我写了很多Math.random
,它不会增加大小?很抱歉,我不会说英语,谢谢您的时间。@JJJMath.random
返回一个值。因此,对于const random=Math.random
,random
等于一个随机值,它不等于Math.random
函数。如果从未使用过const random
,应用程序可能会删除const random=Math.random
,但它不会删除Math.random
(因为浏览器提供了Math.random
)。请注意,在第一个缩小的示例中,const random
是如何被删除的。有意义吗?另外,有点不清楚您作为注释添加的第一个缩略示例的哪一部分,但是如果在第一个示例中缩略后仍然存在单独的Math.random
调用(即文件以Math.random
结尾,并且您正在询问为什么仍然存在对Math.random
的调用),我不能告诉你。这可能是摇树算法中的一个错误。我最初假设您的第一个缩小文件看起来像“use strict”;n、 d(t,“a”,函数({return r});var r=Math.ceil;
以及其他所有内容都是您作为注释添加的。