मैं जॉन मैल्कम से अच्छा सुझाव सुधार हुआ है (और सतपाल here द्वारा कुछ अपडेट) कुछ कमियों है कि यह था ठीक करने के लिए:
1) यह ".min साथ बंडलों के डिफ़ॉल्ट व्यवहार टूट जाता है । " फ़ाइलों
2) यह खोज पैटर्न की अनुमति नहीं है, लेकिन केवल फाइलों
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Optimization;
public static class BundleExtentions
{
public static Bundle IncludeDirectoryWithExclusion(this Bundle bundle, string directoryVirtualPath, string searchPattern, bool includeSubDirectories, params string[] excludePatterns)
{
string folderPath = HttpContext.Current.Server.MapPath(directoryVirtualPath);
SearchOption searchOption = includeSubDirectories
? SearchOption.AllDirectories
: SearchOption.TopDirectoryOnly;
HashSet<string> excludedFiles = GetFilesToExclude(folderPath, searchOption, excludePatterns);
IEnumerable<string> resultFiles = Directory.GetFiles(folderPath, searchPattern, searchOption)
.Where(file => !excludedFiles.Contains(file) && !file.Contains(".min."));
foreach (string resultFile in resultFiles)
{
bundle.Include(directoryVirtualPath + resultFile.Replace(folderPath, "")
.Replace("\\", "/"));
}
return bundle;
}
private static HashSet<string> GetFilesToExclude(string path, SearchOption searchOptions, params string[] excludePatterns)
{
var result = new HashSet<string>();
foreach (string pattern in excludePatterns)
{
result.UnionWith(Directory.GetFiles(path, pattern, searchOptions));
}
return result;
}
}
एक उदाहरण उपयोग कि मैं कोणीय के साथ शुरू lib फ़ोल्डर से सभी पुस्तकालयों शामिल करने के लिए है बाहर रखा जाना है, लेकिन सभी को छोड़कर
bundles.Add(new Bundle("~/bundles/scripts")
.Include("~/wwwroot/lib/angular/angular.js") // Has to be first
.IncludeDirectoryWithExclusion("~/wwwroot/lib", "*.js", true, "*.locale.*.js"));
यह ठीक से यदि आप दोनों angular.min.js और Angular.js ही व्यवहार और में unminified संस्करण जुड़ जाएगा: भाषा लिपियों (एक अलग बंडल में भाषा बाद के आधार पर क्योंकि केवल एक ही जोड़ दिया जाएगा) डीबग और आर में मौजूदा .min.js का उपयोग कर elease।
स्रोत
2016-03-18 09:26:46
आप उस निर्देशिका से सीएसएस फ़ाइल को क्यों नहीं ले जाते हैं? –
बेशक मैं ऐसा कर सकता हूं और इस फ़ाइल के लिए एक और बंडल निर्दिष्ट कर सकता हूं, लेकिन सभी फ़ाइलों को एक निर्देशिका में रखना बेहतर होगा और यदि मुझे इसकी आवश्यकता नहीं है, तो मैं इसे केवल बंडल से बाहर कर दूंगा – xurca
क्या आपके पास बहुत कुछ है फाइलों का? यदि आपके पास छोटी संख्या में फाइलें हैं तो आप अलग-अलग प्रत्येक सीएसएस को पंजीकृत कर सकते हैं। –