如何使用nodejs 的 uglifyjs 压缩js文件

JavaScript016

如何使用nodejs 的 uglifyjs 压缩js文件,第1张

[js压缩]uglifyjs source.js -mc -o target.min.js

以压缩angular.js为例,可以压缩局部变量名,去掉无用的局部变量,空白符等,压缩比例接近80%,效率不错

[user_00@localhost ~]$ uglifyjs angular.js -mc -o angular.min.js

WARN: Dropping unused function isBoolean [angular.js:416,9]

WARN: Dropping unused function makeMap [angular.js:446,9]

WARN: Dropping unused function size [angular.js:488,9]

WARN: Dropping unused function isLeafNode [angular.js:523,9]

WARN: Dropping unused function argument boundTranscludeFn [angular.js:4682,27]

WARN: Dropping unused function argument rootElement [angular.js:4681,16]

WARN: Dropping unused function argument nodeList [angular.js:4680,17]

WARN: Dropping unused function argument scope [angular.js:4679,22]

WARN: Dropping unused function nodesetLinkingFn [angular.js:4678,9]

WARN: Dropping unused function argument boundTranscludeFn [angular.js:4690,27]

WARN: Dropping unused function argument rootElement [angular.js:4689,16]

WARN: Dropping unused function argument node [angular.js:4688,13]

WARN: Dropping unused function argument scope [angular.js:4687,22]

WARN: Dropping unused function argument nodesetLinkingFn [angular.js:4686,25]

WARN: Dropping unused function directiveLinkingFn [angular.js:4685,9]

WARN: Dropping unused function stripHash [angular.js:5009,9]

WARN: Dropping unused variable NG_SWITCH [angular.js:13857,4]

WARN: Dropping unused variable fns [angular.js:1536,8]

WARN: Dropping unused variable events [angular.js:1536,13]

WARN: Dropping unused function wrongMode [angular.js:3879,13]

WARN: Dropping unused function argument cause [angular.js:4799,31]

WARN: Dropping unused function argument exception [angular.js:4799,20]

WARN: Dropping unused function argument args [angular.js:8165,28]

WARN: Dropping unused function argument args [angular.js:8231,33]

WARN: Dropping unused function argument names [angular.js:9018,32]

WARN: Dropping unused function argument name [angular.js:9030,40]

WARN: Dropping unused variable value [angular.js:9230,14]

WARN: Dropping unused function argument value [angular.js:12475,36]

[user_00@localhost ~]$ ls -lh angular.*

-rw-rw-r-- 1 user_00 user_00 479K Apr 5 01:53 angular.js

-rw-rw-r-- 1 user_00 user_00 79K May 4 16:09 angular.min.js

如果您看到上面的错误,这意味着您的 NodeJS 应用程序内存不足,它消耗的内存超过了分配的内存,最终导致它自行终止。

当应用程序批处理大量数据时,数据处理算法的编写方式使其需要保留堆空间中的对象,直到处理完成。随着处理的进行,应用程序逐渐使用了更多内存,V8也将 将花费更多时间进行垃圾收集以释放未使用的内存,直到最终达到分配给进程的限制并导致了OOM。

Node.js 运行时在内存使的用方面非常高效,因此程序通常使用默认限制运行良好。并且,如果没有主动设置最大堆大小,程序则会使用默认内存限制,并且此默认值也是会根据 Node.js 版本和程序运行的系统架构而有所不同。

下面我们具体了解一下:

JavaScript与Java一样,由垃圾回收机制来进行自动的内存管理。对于性能敏感的服务器端程序,内存管理的好坏、垃圾回收状况是否优良,都会对服务构成影响。而在Node中,这一切与V8引擎息息相关。

网上大都说,Node中通过JavaScript只能使用部分内存(64位约1.4G,32位约0.7G)。V8对内存做了限制。因此这种限制下,将会导致Node无法直接操作大内存对象。但是随着版本升级,这个数据好像不是那么绝对。

关于限制官方也没直接说明(主要不确定是否能通过buffer.constants.MAX_LENGTH直接类比),所以写个小程序大概在64位系统上跑一下。

Node.js (64位实测)版本限制

官方文档buffer.constants.MAX_LENGTH

为了解决 OOM 错误,您需要做的就是显式配置内存限制使用 Node.js 命令行选项

Javascript:

Typescript的 ts-node :

这就能快速解决 Node.js 内存不足的问题!

建议始终明确设置, --max-old-space-size 而不是依赖 Node.js 的默认值,因为在较新的 Node.js 版本中默认值可能会更改。

在具有 2 GB 内存的机器上,考虑将其设置为 1536 (1.5 GB) 以留出一些内存用于其他用途并避免内存交换。

如果您在小型机器(例如 Raspberry Pi 板)上使用 Node.js 运行简单的 Web 服务器,您可以将 设置 --max-old-space-size 为适当的小值,例如 128 MB,以避免 Node.js 占用过多宝贵的内存。

关于pm2的具体使用请查看我的文章 Node服务与pm2实战

通过我们除了前端项目编译(各种cli等等)可能出现内存不足,node服务端也可能导致此问题。前端编译我们很简单的借助增加默认内存可以解决,但是服务端部署是一个持续过程,我们很少使用node直接启动的方式启动服务。我们通常借助 pm2 工具来进行,它可以在服务因异常或其他原因被杀掉后进行自动重启。 由于Node的单线程特征,自动重启能很大程度上的提高它的健壮性。

因为我们服务端使用pm2的目的之一,是服务出问题自动重启,而万一我们设置的内存不足或者服务考虑不足有些问题,导致服务内存不足崩溃对于生产环境来说很不友好。而 pm2 针对内存不足也有一个重启命令,一旦内存不足,会自动重启服务,防止整个服务卡死。

当内存超过1024M时自动重启。 如果工程中有比较棘手的内存泄露问题,这个算是一个折中方案。

pm2其实也是支持配置文件来启动的,我们也可以借助配置文件来配置命令与参数:

[js压缩]uglifyjs source.js -mc -o target.min.js

压缩angular.js例压缩局部变量名掉用局部变量空白符等压缩比例接近80%效率错

[user_00@localhost ~]$ uglifyjs angular.js -mc -o angular.min.js

WARN: Dropping unused function isBoolean [angular.js:416,9]

WARN: Dropping unused function makeMap [angular.js:446,9]

WARN: Dropping unused function size [angular.js:488,9]

WARN: Dropping unused function isLeafNode [angular.js:523,9]

WARN: Dropping unused function argument boundTranscludeFn [angular.js:4682,27]

WARN: Dropping unused function argument rootElement [angular.js:4681,16]

WARN: Dropping unused function argument nodeList [angular.js:4680,17]

WARN: Dropping unused function argument scope [angular.js:4679,22]

WARN: Dropping unused function nodesetLinkingFn [angular.js:4678,9]

WARN: Dropping unused function argument boundTranscludeFn [angular.js:4690,27]

WARN: Dropping unused function argument rootElement [angular.js:4689,16]

WARN: Dropping unused function argument node [angular.js:4688,13]

WARN: Dropping unused function argument scope [angular.js:4687,22]

WARN: Dropping unused function argument nodesetLinkingFn [angular.js:4686,25]

WARN: Dropping unused function directiveLinkingFn [angular.js:4685,9]

WARN: Dropping unused function stripHash [angular.js:5009,9]

WARN: Dropping unused variable NG_SWITCH [angular.js:13857,4]

WARN: Dropping unused variable fns [angular.js:1536,8]

WARN: Dropping unused variable events [angular.js:1536,13]

WARN: Dropping unused function wrongMode [angular.js:3879,13]

WARN: Dropping unused function argument cause [angular.js:4799,31]

WARN: Dropping unused function argument exception [angular.js:4799,20]

WARN: Dropping unused function argument args [angular.js:8165,28]

WARN: Dropping unused function argument args [angular.js:8231,33]

WARN: Dropping unused function argument names [angular.js:9018,32]

WARN: Dropping unused function argument name [angular.js:9030,40]

WARN: Dropping unused variable value [angular.js:9230,14]

WARN: Dropping unused function argument value [angular.js:12475,36]

[user_00@localhost ~]$ ls -lh angular.*

-rw-rw-r-- 1 user_00 user_00 479K Apr 5 01:53 angular.js

-rw-rw-r-- 1 user_00 user_00 79K May 4 16:09 angular.min.js

现我test.tar文件通tar -xvf test.tar 解压缩文件夹test现我想解压缩文件夹名称直接改123,命令哪位帮忙答