1

我使用JSCompress站点(使用 UglifyJS2)来压缩我的 JavaScript 代码。但看起来它不再在顶级匿名函数中缩短我的变量名称了。这只发生在我的特定代码中,而对于任何其他类型的代码,它会压缩所有局部变量的名称。

这就是我要的:

(function () {
    function aLongNameToBeCompressed () {
    }
    aLongNameToBeCompressed ();
}) ()

从 ^ 到:

!function(){function a(){}a()}();

但是在这个顶级函数中却发生了相反的情况(checkES6,features等...,这些名称没有被压缩,但它们的变量是“本地的”):

;(function (root, name, factory) {
  'use strict';

  if (("function" === typeof define) && define.amd)
    define([exports], factory);

  else if (typeof exports === 'object')
    factory(exports);

  else
    factory(root[name] = {});

})(this, 'esx', function (exports) {
  'use strict';

  var features = {
      "arrayComprehensions": "[for(_ of [0])_]"
    , "arrowFunction": "(_=>_)"
    , "class": "(class{})"
    , "const": "const c=true"
    , "defaultParams": "(function(a=false){})"
    , "destructuring": "let {d}={a:true}"
    , "forOf": "for(var b of [])"
    , "generator": "(function*(){})"
    , "getter": "({get a(){}})"
    , "label": "l:0"
    , "let": "let o"
    , "reservedWords": "({catch:true})"
    , "setter": "({set a(v){}})"
    , "spread": "[...[]]"
    , "stringInterpolation": "`$\{0}`"
    , "stringLineBreak": "'\\\n'"
    , "super": "({b(){super.a}})"
    , "yield": "(function*(){yield true})"
  };

  // exports.features = features;

  function evaluate (code) {
    try {
      eval(code);
      return true;
    } catch(e) {
      return false;
    }
  }

  /**
   * Check if a set of features are supported.
   */
  function supports () {
    var code = "(function(){";
    var i = 0, len = arguments.length;

    for (; i < len; ++i) {
      var feature = arguments[i].toString();

      if (features.hasOwnProperty(feature))
        code += features[feature] + ';';
    }

    code += "})()";
    return evaluate(code);
  }

  exports.supports = supports;

  function checkES7 () {
    return supports("arrayComprehensions");
  }

  function checkES6 () {
    var methods = 'function' === typeof Object.assign &&
                  'function' === typeof Object.freeze;

    var syntax = supports(
                         "arrowFunction"
                       , "class"
                       , "const"
                       , "forOf"
                       , "defaultParams"
                       , "destructuring"
                       , "super"
                       , "yield"
                       );

    return methods && syntax;
  }

  function checkES5 () {
    var methods = 'function' === typeof [].filter &&
                  'function' === typeof Function.prototype.bind &&
                  'function' === typeof Object.defineProperty &&
                  'function' === typeof ''.trim &&
                  'object'   === typeof JSON;

    var syntax = supports("reservedWords");
    return methods && syntax;
  }

  function checkES3 () {
    return "function" === typeof [].hasOwnProperty;
  }

  /**
   * Check for ECMAScript version.
   */
  exports.detectVersion = function () {
    return checkES7() ? 7 :
           checkES6() ? 6 :
           checkES5() ? 5 :
           checkES3() ? 3 :
           null;
  };

});

当我压缩它时,我看到所有局部变量都具有相同的名称(嵌套范围除外),checkES6checkES6supportssupportsfeaturesfeatures,这个顶级匿名函数中的每个局部变量都没有在其名称中进行压缩。

我仍然想压缩这些名称。这是怎么回事?

4

0 回答 0