6

我尝试使用UglifyJS2对一个简单的 javascript 文件进行 uglify 。

以下是文件的内容:

//this is simply a sample var
var sampleVar = "xyz";

//lots of comments
//this is just another comment
//such things should not be present in javascript
//waiting to see result after uglifying

//this is simply a sample function
function sampleFunction()
{
  var sampleLocalVar = "xzx";
  if(true)
  {
    //inserting some sample comments
    alert("in if block");
  }
  else
  {
    //inserting some sample comments
    alert("in else block");
  }
}

这是我用来丑化的命令:

uglifyjs -c -m sample.js sample.min.js

我收到的错误:

Dot
Error parsing arguments in : sample.js
4

2 回答 2

12

您需要指定输出参数(-o--output),如文档所述:

指定--output( -o) 以声明输出文件。否则输出到 STDOUT。

此外,必须首先指定要缩小的文件(或要连接和缩小的文件),如用法所示:

uglifyjs [input files] [options]

你应该做的是以下几点:

uglifyjs sample.js -c -m -o sample.min.js

有关从命令行使用 UglifyJS2 的更多信息,请参阅文档

于 2013-12-18T09:02:26.633 回答
3

2个问题:

首先,命令行uglifyjs的参数解析有一个bug,所以你要么把选项放在最后,要么用--把它们和命令分开。例如:

uglifyjs -c -m foo.js     # Will fail Error parsing arguments in : foo.js 
uglifyjs foo.js -c -m     # Will work, printing the compressed
uglifyjs -c -m -- foo.js  # Will also work

其次,默认情况下输出为标准输出。传递更多 js 文件作为参数将在缩小之前将它们连接起来。您可以使用-o指定输出文件,或使用普通的 shell 重定向运算符(>、、>>|

uglifyjs -c -m -- foo.js                # Will output the file to stdout
uglifyjs -c -m -- foo.js > foo.min.js   # Will save the file to foo.min.js
uglifyjs -c -m  -o foo.min.js -- foo.js # Will save the file to foo.min.js
uglifyjs -c -m -- foo.js bar.js         # Will concatenate 2 js files
于 2015-02-28T19:10:47.617 回答