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

nik*_*3ro 13 c# asp.net asp.net-optimization

我试图使用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
 */
Run Code Online (Sandbox Code Playgroud)

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

我只需要能够说 - 不要缩小和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);
Run Code Online (Sandbox Code Playgroud)

mez*_*tou 26

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用CustomScriptBundle而不是ScriptBundle

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

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