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
Hope it's not too late. You can implement own minification transform and ignore these errors.
var bundle = new ScriptBundle("~/bundles/xxxbundle", null, new JsMinifyIgnoreReservedWordError()).
Include("~/Scripts/xxx.js");
private class JsMinifyIgnoreReservedWordError : IBundleTransform
{
private const string JsContentType = "text/javascript";
public void Process(BundleContext context, BundleResponse response)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (response == null)
{
throw new ArgumentNullException("response");
}
if (!context.EnableInstrumentation)
{
Minifier minifier = new Minifier();
string result = minifier.MinifyJavaScript(response.Content, new CodeSettings
{
EvalTreatment = EvalTreatment.MakeImmediateSafe,
PreserveImportantComments = false,
IgnoreErrorList = "JS1137" // ignore 'is a new reserved word and should not be used as an identifier' error
});
if (minifier.ErrorList.Count > 0)
{
GenerateErrorResponse(response, minifier.ErrorList);
}
else
{
response.Content = result;
}
}
response.ContentType = JsContentType;
}
private static void GenerateErrorResponse(BundleResponse bundle, IEnumerable<object> errors)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("/* ");
stringBuilder.Append("Minification failed. Returning unminified contents.").AppendLine();
foreach (object error in errors)
{
stringBuilder.Append(error).AppendLine();
}
stringBuilder.Append(" */").AppendLine();
stringBuilder.Append(bundle.Content);
bundle.Content = stringBuilder.ToString();
}
}
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();
}
}
}