Don';使用Microsoft Web优化框架时,请不要损坏某些文件
本文关键字:损坏 文件 框架 使用 Microsoft 优化 Web Don | 更新日期: 2023-09-27 17:57:54
我正在尝试使用Microsoft Web优化框架将许多.js文件连接到一个文件中。一切都可以,但在那些文件中,我有几个已经缩小了&丑化了,没有必要再处理它们。
例如,我有一个repatcha_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
*/
我试图从捆绑包中取出repatcha_ajax.js并直接引用它,但随后会弹出其他错误——所以,我需要捆绑包中某个位置的文件。
我只需要能够说-不要缩小&uglify repatcha_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
的集合来转换每个文件并连接结果。然后使用CCD_ 2的集合对结果进行变换。
默认的脚本捆绑包通过使用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<BundleFile> 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文件重命名为repatcha_ajax.min.js。如果我是正确的,那么它应该跳过uglify/minimy过程。
参考数据:http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification(向下滚动一点可以找到这句话:)
前面的代码创建了一个名为~/bundles/jquery,包括所有适当的(即调试或Scripts文件夹中与通配符字符串"~/Scripts/jquery-{version}.js"。对于ASP.NET MVC 4,这意味着使用调试配置,文件jquery-1.7.1.js将将添加到捆绑包中。在释放配置中,将添加jquery-1.7.1.min.js。捆绑框架如下几种常见的约定,如:
- 当存在"FileX.min.js"answers"FileX.js"时,选择".min"文件进行发布
- 选择非".min"版本进行调试
- 忽略"-vsdoc"文件(如jquery-1.7.1-vsdoc.js),这些文件仅由IntelliSense使用
如果您在ScriptManager中绑定.js文件,您会注意到脚本加载在<form>
中,而不是<head>
中,因此将其从捆绑包中取出会在其他文件之前加载该文件,如果它依赖于捆绑包中的其他内容,这是不好的
下面是一个需要按特定顺序添加到捆绑包中的库的示例
这被添加到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.Path
和ResourceDef.DebugPath
的使用。包含哪个文件将取决于您进行调试或发布
- 调试:无"放大"
- 发布:总"丑化"
这是我的ScriptManager捆绑包,注意ColorBox的位置,位置意义重大:
<asp:ScriptManager runat="server" >
<Scripts>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
<asp:ScriptReference Name="ColorBox" />
<asp:ScriptReference Name="Infragistics" />
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
</Scripts>
</asp:ScriptManager>