MVC4捆绑缩小不与JavaScript的保留字工作 [英] MVC4 Bundle minification doesn't work with javascript reserved words
问题描述
使用最新版本的MVC4时,它包含保留字作为键名称我不能缩小JavaScript!
Using the latest version of MVC4 I can't minify javascript when it contains reserved words as key names!
请参阅下面应该已经缩小的有效的JavaScript错误。
See the error below with the valid javascript that should have been minified.
有谁知道如何解决这个短重写JavaScript才能使用()符号的?
Does anyone know how to fix this short of rewriting the javascript to use [""] notation?
PS中的code是几千行代码,所以它不是一种选择!
PS The code in question is a few thousand lines long, so it's not an option!
/* 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,servicestack等
The question is without going with another option like node, cassette, combres, servicestack etc
我们如何获得MVC4打球与保留字。
How do we get MVC4 to play ball with reserved words.
我觉得很难相信,6个月后加有这种不支持!
I find it hard to believe that after 6 months plus that there is no support for this!
推荐答案
只是尝试这样做,它的作品。很抱歉,但丑陋的code。它将取代你的成员名为删除
和他们的用法。
Just tried this and it works. Sorry but the ugly code. It will replace your members named delete
and the usages of them.
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();
}
}
}
这篇关于MVC4捆绑缩小不与JavaScript的保留字工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!