gpt4 book ai didi

javascript - MVC4 Bundle 缩小不适用于 javascript 保留字

转载 作者:可可西里 更新时间:2023-11-01 01:52:46 25 4
gpt4 key购买 nike

使用最新版本的 MVC4,当它包含保留字作为键名时,我无法缩小 javascript!

请参阅下面的错误,其中包含本应缩小的有效 javascript。

有谁知道如何解决这个重写 javascript 以使用 [""] 符号的问题?

PS 有问题的代码有几千行,所以这不是一个选择!

/* Minification failed. Returning unminified contents.
(3,9-15): run-time warning JS1010: Expected identifier: delete
(4,9-13): run-time warning JS1010: Expected identifier: case
(5,9-11): run-time warning JS1010: Expected identifier: if
(3,9-15): run-time error JS1137: 'delete' is a new reserved word and should not be used as an identifier: delete
(4,9-13): run-time error JS1137: 'case' is a new reserved word and should not be used as an identifier: case
(5,9-11): run-time error JS1137: 'if' is a new reserved word and should not be used as an identifier: if
*/
var context = {};

context.delete = {};
context.case = {};
context.if = {};

问题是不使用其他选项,如节点、盒式磁带、combres、服务堆栈等

我们如何让 MVC4 与保留字打球。

我很难相信在 6 个月后还没有对此的支持!

最佳答案

刚试过这个,它有效。对不起,但是丑陋的代码。它将替换名为 delete 的成员及其用法。

public class CustomBundle : ScriptBundle
{
public CustomBundle(string virtualPath) : base(virtualPath)
{
this.Builder = new CustomBuilder();
}
public CustomBundle(string virtualPath, string cdnPath) : base(virtualPath, cdnPath) {}
}

public class CustomBuilder : IBundleBuilder {
public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<FileInfo> files)
{
var content = new StringBuilder();
foreach (var fileInfo in files)
{
var parser = new Microsoft.Ajax.Utilities.JSParser(Read(fileInfo));
parser.Settings.AddRenamePair("delete", "fooDelete");
content.Append(parser.Parse(parser.Settings).ToCode());
content.Append(";");
}

return content.ToString();
}

private string Read(FileInfo file)
{
using(var r = file.OpenText())
{
return r.ReadToEnd();
}
}
}

关于javascript - MVC4 Bundle 缩小不适用于 javascript 保留字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com