使用Microsoft网络优化框架时,不要丑化某些文件 [英] Don't uglify certain file when using Microsoft Web Optimization Framework

查看:258
本文介绍了使用Microsoft网络优化框架时,不要丑化某些文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想Concat的大量的.js文件成一个单一使用的Microsoft Web优化框架。一切正常,但这些文件中我有一个已经缩小的&安培数;变丑并没有需要再次处理。

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

  / *缩小失败。返回unminified内容。
(715,29-36):运行时错误JS1019:不能有'破'循环外:打破Ť
(714,293-300):运行时错误JS1019:不能有'破'循环外:休息Ť
(678,210-217):运行时错误JS1019:不能有'破'循环外:休息Ť
(671,1367-1374):运行时错误JS1019:不能有'破'循环外:打破Ť
(665,280-287):运行时错误JS1019:不能有'破'循环外:休息Ť
 * /

我试过把取出来recaptcha_ajax.js捆绑,并直接引用它,但随后其他错误弹出 - 所以,我需要在某些位置的包内文件

我只需要能够说 - 不要再压缩和放大器;丑化recaptcha_ajax.js - 只需将其添加到包

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

 变种B =新ScriptBundle(〜/包/ myjsbundle);b.IncludeDirectory(〜/ ScriptsMine /,*的.js,真);//一些命令,如:
// b.DoNotMinifyOrUglify(〜/ ScriptsMine / recaptcha_ajax.js);bundles.Add(二);


解决方案

捆绑使用 IItemTransform 的集合转换的每个文件并连接的结果。然后,它通过集合变换的结果 IBundleTransform

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

所以要prevent从缩小文件某些文件,您可以创建自己的 IBundleBuilder ,其通过使用minifies按文件包文件中的 IItemTransform

 公共类CustomScriptBundle:捆绑
{
    公共CustomScriptBundle(字符串virtualPath)
        :这个(virtualPath,NULL)
    {
    }    公共CustomScriptBundle(字符串virtualPath,串cdnPath)
        :基地(virtualPath,cdnPath,NULL)
    {
        this.ConcatenationToken =; + Environment.NewLine;
        this.Builder =新CustomBundleBuilder();
    }
}
公共类CustomBundleBuilder:IBundleBuilder
{
    内部静态字符串ConvertToAp prelativePath(字符串APPPATH,串全名)
    {
        回报(string.IsNullOrEmpty(APPPATH)|| fullName.StartsWith(APPPATH,StringComparison.OrdinalIgnoreCase)全名:fullName.Replace(APPPATH,〜/)!?)更换('\\\\','/')。
    }    公共字符串BuildBundleContent(捆绑包,BundleContext的背景下,IEnumerable的< BundleFile>文件)
    {
        如果(文件== NULL)
            返回的String.Empty;
        如果(上下文== NULL)
            抛出新的ArgumentNullException(上下文);
        如果(丛== NULL)
            抛出新的ArgumentNullException(捆绑);        StringBuilder的StringBuilder的=新的StringBuilder();
        的foreach(BundleFile bundleFile在文件中)
        {
            bundleFile.Transforms.Add(新CustomJsMinify());
            stringBuilder.Append(bundleFile.ApplyTransforms());
            stringBuilder.Append(bundle.ConcatenationToken);
        }        返回stringBuilder.ToString();
    }
}公共类CustomJsMinify:IItemTransform
{
    公共字符串处理(字符串includedVirtualPath,字符串输入)
    {
        如果(includedVirtualPath.EndsWith(min.js,StringComparison.OrdinalIgnoreCase))
        {
            返回输入;
        }        Minifier minifier =新Minifier();
        变量codeSettings =新的codeSettings();
        codeSettings.EvalTreatment = EvalTreatment.MakeImmediateSafe;
        codeSettings preserveImportantComments = FALSE。        字符串str = minifier.MinifyJavaScript(输入,codeSettings);        如果(minifier.ErrorList.Count大于0)
            回归/ *+ string.Concat(minifier.Errors)+* /;        返回海峡;
    }
}

然后使用 CustomScriptBundle 而不是 ScriptBundle

 公共静态无效RegisterBundles(BundleCollection包)
{
    bundles.Add(新CustomScriptBundle(〜/包/样本)。包括(
                〜/脚本/ a.js
                〜/脚本/ b.js
                〜/脚本/ c.js));
}

如果你提供了一个 min.js 文件,它会被用来代替污染减量吧。

I am trying to concat lots of .js files into a single one using Microsoft Web Optimization framework. Everything works, but within those files I have several that are already minified & uglified and there is not need to process them again.

For example I have recaptcha_ajax.js file and it causes following errors when it's appended:

/* 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
 */

I've tried to take take recaptcha_ajax.js out of bundle and reference it directly, but then other errors popup - so, I need that file within the bundle at certain position.

I just need to be able to say - do not minify & uglify recaptcha_ajax.js - just add it to the bundle.

Is there a way to do this? Here is how I see it:

var b = new ScriptBundle("~/bundles/myjsbundle");

b.IncludeDirectory("~/ScriptsMine/", "*.js", true);

// some command like:
// b.DoNotMinifyOrUglify("~/ScriptsMine/recaptcha_ajax.js");

bundles.Add(b);

解决方案

Bundles transform each file by using a collection of IItemTransform and concatenate result. Then it transform the result by using a collection of IBundleTransform.

The default script bundle minifies the complete bundle content by using JsMinify (which implements IBundleTransform).

So to prevent some file from minifying, you have to create your own IBundleBuilder which minifies the bundle file by file by using an 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;
    }
}

Then use the CustomScriptBundle instead of ScriptBundle

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

If you provide a min.js file it will be used instead of minifying it.

这篇关于使用Microsoft网络优化框架时,不要丑化某些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆