简体   繁体   English

如何缩小knockoutjs代码文件

[英]How to minify knockoutjs code files

I have added knockout js files in Javascript Bundles & at release mode these are minified.我在 Javascript Bundles 中添加了淘汰 js 文件,并且在发布模式下这些文件被缩小了。 Everything is working fine in debug mode.在调试模式下一切正常。 But when optimizations are on & JsBundles are minified automatically by Visual Studio it changes parameters name which starts causing issues.但是,当优化开启且 JsBundles 由 Visual Studio 自动缩小时,它会更改开始引起问题的参数名称。 Example:例子:

minified file after Prettify美化后的缩小文件

function ProgramViewModel() {
  var n = this;
  n.Programs = ko.observableArray([]);
  n.SelectProgramForDetails = ko.observable('');
  n.SelectedProgramList = ko.observableArray([]);
  n.RequestedPrograms = ko.observableArray([]);
  n.SelectedPrograms = ko.observableArray([]);
  n.SearchSelected = ko.observable(!1);
  n.ProgramName = ko.observable('');
  n.CurrentPage = ko.observable(0);
  n.LastHeight = ko.observable(0);
  n.ProgramCatalog = ko.observableArray([{
    CatalogName: 'Global Catalog',
    CatalogId: 1
  },
  {
    CatalogName: 'Area Catalog',
    CatalogId: 2
  }
  ]);
  n.ColumnClicked = function (n, t) {
    paginationViewModel.ColumnClicked(t);
    programViewModel.GetPrograms()
  };
  n.ShowData = function (n) {
    $('html, body').animate({
      scrollTop: paginationViewModel.offset()
    }, 500);
    paginationViewModel.ShowData(n);
    programViewModel.GetPrograms()
  };
}

File before之前的文件

function ProgramViewModel() {
    var self = this;
    self.Programs = ko.observableArray([]);
    self.SelectProgramForDetails = ko.observable("");
    self.SelectedProgramList = ko.observableArray([]);
    self.RequestedPrograms = ko.observableArray([]);
    self.SelectedPrograms = ko.observableArray([]);
    self.SearchSelected = ko.observable(false);
    self.ProgramName = ko.observable("");

    self.CurrentPage = ko.observable(0);
    self.LastHeight = ko.observable(0);

    // Default Catalog for now
    self.ProgramCatalog = ko.observableArray([
        { CatalogName: "Global Catalog", CatalogId: 1 },
        { CatalogName: "Area Catalog", CatalogId: 2 }
    ]);

    self.ColumnClicked = function (data, e) {
        paginationViewModel.ColumnClicked(e);
        programViewModel.GetPrograms();
    };

    self.ShowData = function (mode) {
        $('html, body').animate({
            scrollTop: paginationViewModel.offset()
        }, 500);
        paginationViewModel.ShowData(mode);

        programViewModel.GetPrograms();
    };
}

The problem is that minifier is not aware of keywords in scope thus changing name of variable in same scope to existing names.问题是 minifier 不知道范围内的关键字,因此将同一范围内的变量名称更改为现有名称。 When there is a n named variable in that scope it is renaming other function variable into n again(might can issue or might not too, not so sure).当有n命名变量在范围内,可以重命名等功能的可调成n再次(也许可以发出或可能不会太大,不太确定)。

Any minify tool that can help minify KnockoutJs files more effectively?任何可以帮助更有效地缩小 KnockoutJs 文件的缩小工具?

Uglifyjs2 有可以关闭的 mangler 选项,单独保留变量名称: https : //github.com/mishoo/UglifyJS2#mangler-options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM