使用Microsoft Web Optimization Framework时,请勿对某些文件进行uglify

我正在尝试使用Microsoft Web Optimization框架将大量.js文件连接到一个文件中。 一切正常,但在这些文件中,我有几个已经缩小和丑化,并且不需要再次处理它们。

例如,我有recaptcha_ajax.js文件,并在附加时导致以下错误:

/* Minification failed. Returning unminified contents. (715,29-36): run-time error JS1019: Can't have 'break' outside of loop: break t (714,293-300): run-time error JS1019: Can't have 'break' outside of loop: break t (678,210-217): run-time error JS1019: Can't have 'break' outside of loop: break t (671,1367-1374): run-time error JS1019: Can't have 'break' outside of loop: break t (665,280-287): run-time error JS1019: Can't have 'break' outside of loop: break t */ 

我试图从包中取出recaptcha_ajax.js并直接引用它,但随后弹出其他错误 – 所以,我需要在特定位置的包内的该文件。

我只需要能够说 – 不要缩小和uglify recaptcha_ajax.js – 只需将其添加到捆绑包中即可。

有没有办法做到这一点? 我是这样看的:

 var b = new ScriptBundle("~/bundles/myjsbundle"); b.IncludeDirectory("~/ScriptsMine/", "*.js", true); // some command like: // b.DoNotMinifyOrUglify("~/ScriptsMine/recaptcha_ajax.js"); bundles.Add(b); 

Bundles使用IItemTransform的集合转换每个文件并连接结果。 然后它使用IBundleTransform的集合转换结果。

默认脚本包使用JsMinify (实现IBundleTransform )缩小完整的包内容。

因此,为了防止某些文件缩小,您必须创建自己的IBundleBuilder ,它使用IItemTransform按文件缩小捆绑文件。

 public class CustomScriptBundle : Bundle { public CustomScriptBundle(string virtualPath) : this(virtualPath, null) { } public CustomScriptBundle(string virtualPath, string cdnPath) : base(virtualPath, cdnPath, null) { this.ConcatenationToken = ";" + Environment.NewLine; this.Builder = new CustomBundleBuilder(); } } public class CustomBundleBuilder : IBundleBuilder { internal static string ConvertToAppRelativePath(string appPath, string fullName) { return (string.IsNullOrEmpty(appPath) || !fullName.StartsWith(appPath, StringComparison.OrdinalIgnoreCase) ? fullName : fullName.Replace(appPath, "~/")).Replace('\\', '/'); } public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable files) { if (files == null) return string.Empty; if (context == null) throw new ArgumentNullException("context"); if (bundle == null) throw new ArgumentNullException("bundle"); StringBuilder stringBuilder = new StringBuilder(); foreach (BundleFile bundleFile in files) { bundleFile.Transforms.Add(new CustomJsMinify()); stringBuilder.Append(bundleFile.ApplyTransforms()); stringBuilder.Append(bundle.ConcatenationToken); } return stringBuilder.ToString(); } } public class CustomJsMinify : IItemTransform { public string Process(string includedVirtualPath, string input) { if (includedVirtualPath.EndsWith("min.js", StringComparison.OrdinalIgnoreCase)) { return input; } Minifier minifier = new Minifier(); var codeSettings = new CodeSettings(); codeSettings.EvalTreatment = EvalTreatment.MakeImmediateSafe; codeSettings.PreserveImportantComments = false; string str = minifier.MinifyJavaScript(input, codeSettings); if (minifier.ErrorList.Count > 0) return "/* " + string.Concat(minifier.Errors) + " */"; return str; } } 

然后使用CustomScriptBundle而不是ScriptBundle

 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new CustomScriptBundle("~/bundles/Sample").Include( "~/Scripts/a.js", "~/Scripts/b.js", "~/Scripts/c.js")); } 

如果您提供min.js文件,将使用它而不是缩小它。

优化框架考虑了文件名。

尝试将* .js文件重命名为recaptcha_ajax.min.js。 如果我是正确的,那么它应该跳过uglify / minify进程。

参考数据: http : //www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification (向下滚动一下,找到这个引用:)

上面的代码创建了一个名为〜/ bundles / jquery的新JavaScript包,其中包含Scripts文件夹中与通配符字符串“〜/ Scripts / jquery- {version”匹配的所有相应的(即调试或缩小但不是.vsdoc)文件} .js文件”。 对于ASP.NET MVC 4,这意味着使用调试配置,文件jquery-1.7.1.js将添加到包中。 在发布配置中,将添加jquery-1.7.1.min.js。 捆绑框架遵循几个常见的约定,例如:

  • 当存在“FileX.min.js”和“FileX.js”时,选择“.min”文件进行发布。
  • 选择非“.min”版本进行调试。
  • 忽略仅由IntelliSense使用的“-vsdoc”文件(例如jquery-1.7.1-vsdoc.js)。

如果您在ScriptManager中捆绑.js文件,您会注意到脚本是在

中加载的,而不是所以将它从捆绑中拉出会将该文件加载到其他文件之前,如果它依赖于它则不好捆绑中的其他东西

以下是需要以特定顺序添加到捆绑包的库的示例。
这将添加到App_Start / BundleConfig.vb中

 '############################################################ ' This is a ScriptManager Resource Definition ' for use in a ScriptManager mapping '############################################################ Dim ResourceDef as ScriptResourceDefinition = New ScriptResourceDefinition() Dim ResourceName as String = "ColorBox" 'add the Resource Definition details ResourceDef.Path = "~/Scripts/colorbox/jquery.colorbox-min.js" ResourceDef.DebugPath = "~/Scripts/colorbox/jquery.colorbox.js" ScriptManager.ScriptResourceMapping.AddDefinition(ResourceName, ResourceDef) '############################################################ 

请注意ResourceDef.PathResourceDef.DebugPath的使用。 包含哪个文件取决于您进行调试或发布

  • 调试:没有“uglification”
  • 发布:总计“丑化”

这是我的ScriptManager包,注意ColorBox的位置,位置意味着很多: