14

最新バージョンの 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 = {};

問題は、ノード、カセット、コムレス、サービススタックなどの別のオプションを使用しないことです

MVC4 に予約語を使用してボールをプレーさせるにはどうすればよいですか。

6 か月が経過した後、これに対するサポートがないとは信じがたいです。

4

2 に答える 2

8

これを試してみて、動作します。申し訳ありませんが、醜いコードです。名前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();
        }
    }
}
于 2012-11-16T14:49:05.170 に答える