忽略捆绑包中的单个文件

时间:2013-05-07 13:59:44

标签: asp.net-mvc-4 asp.net-optimization

按如下方式设置我的包:

bundles.UseCdn = true;

var scriptJquery = new ScriptBundle(Bundles.Scripts.Jquery, "//code.jquery.com/jquery-1.9.1.js")
    {
        CdnFallbackExpression = "window.jQuery",
    }
    .Include("~/scripts/jquery-{version}.js"); // {version} not work!

bundles.Add(scriptJquery);

var scriptBundle = new ScriptBundle(Bundles.Scripts.Common)
    {
        Orderer = new FileBundleOrderer(server.MapPath("~/Scripts/bundle.txt"))
    }
    .IncludeDirectory("~/Scripts", "*.js", false);
bundles.Add(scriptBundle); 

在我的Scripts文件夹中,具有以下结构 请注意,jQuery与其他jQuery处于同一级别。

Folder JS Structure

_Layout.cshtml

@Scripts.Render(CreditoImobiliarioBB.Web.App_Start.Bundles.Scripts.Jquery)
@Scripts.Render(CreditoImobiliarioBB.Web.App_Start.Bundles.Scripts.Common)

运行应用程序时,会生成以下脚本。 请注意, jQuery出现了两次!

<script src="/scripts/jquery-1.9.1.js"></script>    <!--  <------ -->

<script src="/Scripts/moment.js"></script>
<script src="/Scripts/moment.pt-br.js"></script>
<script src="/Scripts/handlebars.runtime.js"></script>
<script src="/Scripts/knockout-2.2.1.debug.js"></script>
<script src="/Scripts/knockout.mapping-latest.js"></script>
<script src="/Scripts/underscore.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/bootstrap-datepicker.js"></script>
<script src="/Scripts/bootstrap-datepicker.pt-BR.js"></script>
<script src="/Scripts/bootstrap-multiselect.js"></script>
<script src="/Scripts/bootstrap-typeahead.js"></script>
<script src="/Scripts/accounting.js"></script>
<script src="/Scripts/jquery.pnotify.js"></script>
<script src="/Scripts/fileuploader.js"></script>
<script src="/Scripts/jquery.inputmask.js"></script>
<script src="/Scripts/jquery.inputmask.extensions.js"></script>
<script src="/Scripts/jquery.inputmask.numeric.extensions.js"></script>
<script src="/Scripts/jquery.inputmask.date.extensions.js"></script>
<script src="/Scripts/jquery.maskMoney.js"></script>
<script src="/Scripts/jquery.bootpag.js"></script>
<script src="/Scripts/Email.js"></script>
<script src="/Scripts/Endereco.js"></script>
<script src="/Scripts/Telefone.js"></script>
<script src="/Scripts/prototypes.js"></script>
<script src="/Scripts/antiForgetyTokenHelper.js"></script>
<script src="/Scripts/ajaxPost.js"></script>
<script src="/Scripts/ajaxUploader.js"></script>
<script src="/Scripts/jquery-1.9.1.js"></script>   <!--  <------ -->
<script src="/Scripts/jquery.cycle.all.js"></script>
<script src="/Scripts/scripts.js"></script>

如果我试图忽略jQuery文件,它不会以任何方式出现!

bundles.IgnoreList.Ignore("jquery-{version}.js", OptimizationMode.Always);

如何只在一个包中忽略jQuery?如果是Bundles.Scripts.Common

1 个答案:

答案 0 :(得分:3)

我已经延长了@Jon Malcolm的答案来解决我的问题。

public static class BundleExtentions
{
    public static Bundle IncludeDirectoryWithExclusion(this ScriptBundle bundle, string directoryVirtualPath, string searchPattern, bool includeSubDirectories, params string[] toExclude)
    {
        var folderPath = HttpContext.Current.Server.MapPath(directoryVirtualPath);
        SearchOption searchOption = includeSubDirectories
                                        ? SearchOption.AllDirectories
                                        : SearchOption.TopDirectoryOnly;

        foreach (var file in from file in Directory.GetFiles(folderPath, searchPattern, searchOption) let fileName = Path.GetFileName(file) where String.IsNullOrEmpty(Array.Find(toExclude, s => fileName != null && s.ToLower() == fileName.ToLower())) select file)
        {
            bundle.Include(directoryVirtualPath + file.Replace(folderPath, string.Empty).Replace("\\", "/"));
        }

        return bundle;
    }
}

用法

 mybundle.IncludeDirectoryWithExclusion("~/Directory", "*.js", true, "excluejsfile.js");

希望它可以帮到你。