MVC4 Bundle minification doesn't work with javascript reserved words

前端 未结 2 1545
有刺的猬
有刺的猬 2020-12-31 06:55

Using the latest version of MVC4 I can\'t minify javascript when it contains reserved words as key names!

See the error below with the valid javascript that should h

2条回答
  •  清酒与你
    2020-12-31 07:44

    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 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();
            }
        }
    }
    

提交回复
热议问题