jay*_*rjo 17 javascript compression performance minify uglifyjs
试图为我的js库准备好的构建环境.根据网络上的评论,UglifyJS似乎是最好的压缩模块之一,在NodeJS下工作.所以这里是建议缩小代码的最佳方法:
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var orig_code = "... JS code here";
var ast = jsp.parse(orig_code); // parse code and get the initial AST
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
var final_code = pro.gen_code(ast); // compressed code here
Run Code Online (Sandbox Code Playgroud)
如此处所示,pro.ast_mangle(ast)应该修改变量名称,但事实并非如此.我从这个管道中得到的只是javascript代码,没有空格.起初我认为我的代码没有针对压缩进行优化,但后来我尝试使用Google Closure并进行了相当大的压缩(包含变量名称和所有内容).
UglifyJS专家,任何暗示我做错了什么?
更新:
实际代码太大,无法在此处引用,但即使是这样的代码段也不会被破坏:
;(function(window, document, undefined) {
function o(id) {
if (typeof id !== 'string') {
return id;
}
return document.getElementById(id);
}
// ...
/** @namespace */
window.mOxie = o;
}(window, document));
Run Code Online (Sandbox Code Playgroud)
这就是我得到的(我想只有空格被剥离):
(function(window,document,undefined){function o(id){return typeof id!="string"?id:document.getElementById(id)}window.mOxie=window.o=o})(window,document)
Run Code Online (Sandbox Code Playgroud)
jay*_*rjo 16
好吧,似乎最新版本的Uglify JS要求将mangle选项显式传递为true,否则它不会破坏任何东西.像这样:
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var orig_code = "... JS code here";
var options = {
mangle: true
};
var ast = jsp.parse(orig_code); // parse code and get the initial AST
ast = pro.ast_mangle(ast, options); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
var final_code = pro.gen_code(ast); // compressed code here
Run Code Online (Sandbox Code Playgroud)
axk*_*ibe 10
默认情况下,uglify不会破坏顶级名称,也许这就是你所看到的?
尝试:-mt或--mangle-toplevel - 顶层范围内的mangle名称(默认情况下我们不这样做).
| 归档时间: |
|
| 查看次数: |
33758 次 |
| 最近记录: |