Reputation: 11059
Set up my bundles as follows:
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);
In my Scripts folder have the following structure.
Note that the jQuery is on the same level as the others.
_Layout.cshtml
@Scripts.Render(CreditoImobiliarioBB.Web.App_Start.Bundles.Scripts.Jquery)
@Scripts.Render(CreditoImobiliarioBB.Web.App_Start.Bundles.Scripts.Common)
When running the application the following scripts are generated. Note that the jQuery appeared twice!
<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>
If I try to ignore the jQuery file, it does not appear in any way!
bundles.IgnoreList.Ignore("jquery-{version}.js", OptimizationMode.Always);
How to ignore the jQuery in a single bundle only? In case the Bundles.Scripts.Common
Upvotes: 2
Views: 4883
Reputation: 133423
I have extended @Jon Malcolm answer to solve my problem.
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;
}
}
Usage
mybundle.IncludeDirectoryWithExclusion("~/Directory", "*.js", true, "excluejsfile.js");
Hope it can help you.
Upvotes: 3