【问题标题】:How to uglify JavaScript using UglifyJS 2?如何使用 UglifyJS 2 丑化 JavaScript?
【发布时间】:2013-12-18 08:59:45
【问题描述】:

我尝试使用 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 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

【问题讨论】:

    标签: javascript uglifyjs2


    【解决方案1】:

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

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

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

    uglifyjs [input files] [options]
    

    你应该做的是:

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

    有关从命令行使用 UglifyJS2 的更多信息,see the documentation

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-30
        • 1970-01-01
        相关资源
        最近更新 更多