gpt4 book ai didi

c# - 使用 Microsoft Web Optimization Framework 时不要丑化某些文件

转载 作者:可可西里 更新时间:2023-11-01 08:03:09 24 4
gpt4 key购买 nike

我正在尝试使用 Microsoft Web Optimization framework 将大量 .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 并直接引用它,但随后弹出其他错误 - 因此,我需要包中特定位置的文件。

我只需要能够说 - 不要缩小和丑化 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<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 文件,它将被使用而不是缩小它。

关于c# - 使用 Microsoft Web Optimization Framework 时不要丑化某些文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23022586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com