跳过单个文件的最小化处理?

9
我将尝试使用ASP.Net的BundleTable来优化一些Javascript文件,但是遇到了一个问题,当代码被压缩时,特定的插件(jQuery-Timepicker)无法正常工作。请参见这里。目前bundle代码类似于:
// Add our commonBundle
var commonBundle= new Bundle("~/CommonJS" + culture.ToString());

// JQuery and related entries.
commonBundle.Include("~/Scripts/jquery-1.7.2.js");
commonBundle.Include("~/Scripts/jquery-ui-1.8.22.js");
commonBundle.Include("~/Scripts/jquery.cookie.js");
commonBundle.Include("~/Scripts/jquery-ui/jquery-ui-timepicker-addon.js"); // This is the one that does not work when bundled

// JS Transformer
commonBundle.Transforms.Add(new JsMinify());

BundleTable.Bundles.Add(commonBundle);

如果我移除jquery-ui-timepicker-addon.js文件,然后在我的网页中单独包含它,那么它就可以正常工作。(否则我会收到Uncaught TypeError: undefined is not a function错误)。
我想知道是否有办法设置我的打包代码来跳过压缩这个文件(但仍将其包含在捆绑文件中)?我一直在寻找解决方案,但没有找到任何解决方案。

你能不能把 commonBundle.Include 行移动到转换之后的时间选择器后面? - Toby
4个回答

3

问题在于所有文件都被捆绑在一起,然后整个捆绑包被最小化。因此,您不容易只跳过一个文件的最小化。最好的方法可能是创建一个新的转换器,将您想要还原最小化的文件内容附加到其中。然后,您可以将此转换器附加到已注册的ScriptBundle中:

commonBundle.Transforms.Add(new AppendFileTransform(""~/Scripts/jquery-ui/jquery-ui-timepicker-addon.js""));

AppendFileTransform会将文件内容简单地附加到捆绑响应中。您不再需要明确地将timepicker包含在捆绑包中,而是这个转换会将其包含在内,这将有效地给您所需的行为,因为JsMinify转换将首先运行并对捆绑包进行缩小,然后您可以在末尾添加未缩小的文件。


是的,你说得对,但如果文件被修改,它不会重新创建捆绑包。在这种情况下可能可以接受,但作为一般解决方案不行。 - Nenad
我在下面发布了所请求的“提示” ;) - Jiří Herník

3

可以从另一个方向更好地解决这个问题 - 而不是试图不压缩单个文件,可以添加针对单个项的转换。

首先 - 创建一个实现IItemTransform接口的类,并使用相同的代码来压缩给定的输入:

public class JsItemMinify : System.Web.Optimization.IItemTransform
{
    public string Process(string includedVirtualPath, string input)
    {
        var min = new Microsoft.Ajax.Utilities.Minifier();
        var result = min.MinifyJavaScript(input);
        if (min.ErrorList.Count > 0)
            return "/*minification failed*/" + input;

        return result;
    }
}

第二步 - 将此项转换添加到各个文件中,并删除捆绑转换:

var commonBundle= new Bundle("~/CommonJS");
// the first two includes will be minified
commonBundle.Include("~/Scripts/jquery-1.7.2.js", new JsItemMinify());
commonBundle.Include("~/Scripts/jquery-ui-1.8.22.js", new JsItemMinify());

// this one will not
commonBundle.Include("~/Scripts/jquery.cookie.js");

// Remove the default JsMinify bundle transform
commonBundle.Transforms.Clear();

BundleTable.Bundles.Add(commonBundle);

1

您无法设置Bundle跳过某些文件的缩小和缩小其余文件。

您可以通过覆盖Bundle.ApplyTransform或JsMinify.Process方法来实现自己的Bundle或Transform,但是您需要注意不要破坏文件的更改跟踪、键生成、缓存失效等(或进行一些丑陋的黑客攻击)。这不值得努力。

我会保留单独的js文件,正如您已经提到的那样。


0

这只是一个基于Hao Kung答案的完整示例

var myBundle = new ScriptBundle("~/bundles/myBundle").Include(
    "~/Scripts/script1.js",
    "~/Scripts/script2.js",
);
myBundle.Transforms.Add(new AppendFileTransform("~/Scripts/excludedFile.min.js"));
bundles.Add(myBundle);

这里是AppendFileTransform的示例实现:

public class AppendFileTransform : IBundleTransform
{
    private readonly string _filePath;

    public AppendFileTransform(string filePath)
    {
        _filePath = filePath;
    }

    public void Process(BundleContext context, BundleResponse response)
    {
        response.Content += File.ReadAllText(context.HttpContext.Server.MapPath(_filePath));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接