Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 网页摇树罐’;t提取未使用的函数_Webpack_Tree Shaking - Fatal编程技术网

Webpack 网页摇树罐’;t提取未使用的函数

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

当我使用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=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.0
UglifyjsWebpackPlugin:1.1.6


谢谢

你的问题有点不清楚,但是
Math.random
是javascript标准库的一部分,并且总是包含在每个浏览器中。因此,您不能对其进行树摇,但也不必担心其大小:您可以免费“获取”
Math.random

换句话说,摇树是一种减少包裹大小的工具。
Math.random
的定义一开始就不是包的一部分,它也不会影响包的大小


(注意:Math.ceil的情况也是如此)

谢谢你的回答。关于你的答案,我有一个问题:为什么我“得到”
Math.random
,如果我写了很多
Math.random
,它不会增加大小?很抱歉,我不会说英语,谢谢您的时间。@JJJ
Math.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;
以及其他所有内容都是您作为注释添加的。