如何使用UglifyJS 2丑化JavaScript?

如何使用UglifyJS 2丑化JavaScript?,javascript,uglifyjs2,Javascript,Uglifyjs2,我试着用一个简单的javascript文件 以下是该文件的内容: //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 sam

我试着用一个简单的javascript文件

以下是该文件的内容:

//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

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

指定
--output
-o
)以声明输出文件。否则,输出将转到标准输出

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

uglifyjs [input files] [options]
您应该做的是以下几点:

uglifyjs sample.js -c -m -o sample.min.js
有关从命令行使用UglifyJS2的更多信息,请参见。

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