/// <summary> /// This returns the appropriate html string to include in the web page such that the requested bundle will be loaded. /// </summary> /// <param name="bundleName"></param> /// <param name="cssOrJs"></param> /// <param name="inDevelopment">This controls whether we supply individual files or development mode /// or single minified files/CDNs in non-development mode</param> /// <param name="getContentUrl">method to get url of content</param> /// <returns></returns> public string CalculateHtmlIncludes(string bundleName, CssOrJs cssOrJs, bool inDevelopment, Func<string, string> getContentUrl) { var settingFilePath = Path.Combine(_jsonDataDir, config.BundlesFileName); var reader = new ReadBundleFile(settingFilePath); var fileTypeInfo = config.GetFileTypeData(cssOrJs); if (inDevelopment) { var sb = new StringBuilder(); //we send the individual files as found in the bundle json file foreach (var relFilePath in _searcher.UnpackBundle(reader.GetBundleDebugFiles(bundleName))) { sb.AppendLine(fileTypeInfo.DebugHtmlFormatString .Replace(FileTypeConfigInfo.FileUrlParam, getContentUrl(relFilePath))); } return sb.ToString(); } //We are in nonDebug, i.e. production mode var cdnLinks = reader.GetBundleCdnInfo(bundleName); return cdnLinks.Any() ? FormCdnIncludes(cdnLinks, bundleName, cssOrJs, fileTypeInfo, getContentUrl) : FormSingleMinifiedFileInclude(bundleName, cssOrJs, fileTypeInfo, getContentUrl); }
//------------------------------------------------------------------ //private methods private string FormSingleMinifiedFileInclude(string bundleName, CssOrJs cssOrJs, FileTypeConfigInfo fileTypeInfo, Func<string, string> getContentUrl) { var relFilePath = $"~/{fileTypeInfo.Directory}{bundleName}.min.{cssOrJs.ToString().ToLowerInvariant()}"; var fileUrl = getContentUrl(relFilePath); var htmlLink = fileTypeInfo.NonDebugHtmlFormatString.Replace(FileTypeConfigInfo.FileUrlParam, fileUrl); if (fileTypeInfo.NonDebugHtmlFormatString.Contains(FileTypeConfigInfo.CachebusterParam)) { var cacheBusterValue = _getChecksumFromRelPath(relFilePath); htmlLink = htmlLink.Replace(FileTypeConfigInfo.CachebusterParam, cacheBusterValue); } return htmlLink; }
private string FormCdnIncludes(IEnumerable<CdnInfo> cdnLinks, string bundleName, CssOrJs cssOrJs, FileTypeConfigInfo fileTypeInfo, Func<string, string> getContentUrl) { if (string.IsNullOrEmpty(fileTypeInfo.CdnHtmlFormatString)) throw new InvalidOperationException( $"The bundle {bundleName} contains a cdn definition, but the current config does not support CDN for {cssOrJs}"); var sb = new StringBuilder(); //we send the individual files as found in the bundle json file foreach (var cdnLink in cdnLinks) { var relFilePath = $"~/{fileTypeInfo.Directory}{cdnLink.Production}"; var httpFileUrl = getContentUrl(relFilePath); sb.AppendLine(cdnLink.BuildCdnIncludeString(fileTypeInfo.CdnHtmlFormatString, httpFileUrl, () => _getChecksumFromRelPath(relFilePath))); } return sb.ToString(); }
//--------------------------------------- //private methods /// <summary> /// This returns the html to include either CSS or JavaScript files /// </summary> /// <param name="helper"></param> /// <param name="bundleName">The name of the bundle property and the name of the minified file</param> /// <param name="cssOrJs">This says if its css or javascript. NOTE: the enum string is used as the dir and the file type</param> /// <param name="forceState">if not null then true forces into debug state and false forces production state</param> /// <returns></returns> private static MvcHtmlString CreateHtmlIncludes(this HtmlHelper helper, string bundleName, CssOrJs cssOrJs, bool?forceState = null) { var isDebug = false; #if DEBUG isDebug = true; #endif if (forceState != null) { isDebug = ( bool )forceState; } var bundler = GetBundlerForBowerCached(helper); var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); return(new MvcHtmlString(bundler.CalculateHtmlIncludes(bundleName, cssOrJs, isDebug, urlHelper.Content))); }
//--------------------------------------- //private methods /// <summary> /// This returns the html to include either CSS or JavaScript files /// </summary> /// <param name="urlHelper"></param> /// <param name="bundleName">The name of the bundle property and the name of the minified file</param> /// <param name="cssOrJs">This says if its css or javascript. NOTE: the enum string is used as the dir and the file type</param> /// <param name="forceState">if not null then true forces into debug state and false forces production state</param> /// <returns></returns> private static HtmlString CreateHtmlIncludes(this IUrlHelper urlHelper, string bundleName, CssOrJs cssOrJs, bool?forceState = null) { var isDebug = false; #if DEBUG isDebug = true; #endif if (forceState != null) { isDebug = (bool)forceState; } throw new NotImplementedException(); //var bundler = new BundlerForBowerDnx(GetAbsDataDirectory(), urlHelper.Content); //return new HtmlString(bundler.CalculateHtmlIncludes(bundleName, cssOrJs, isDebug)); }
public FileTypeConfigInfo GetFileTypeData(CssOrJs cssOrJs) { return cssOrJs == CssOrJs.Css ? new FileTypeConfigInfo(CssDirectory, CssDebugHtmlFormatString, CssNonDebugHtmlFormatString, CssCdnHtmlFormatString) : new FileTypeConfigInfo(JsDirectory, JsDebugHtmlFormatString, JsNonDebugHtmlFormatString, JsCdnHtmlFormatString); }
//--------------------------------------- //private methods /// <summary> /// This returns the html to include either CSS or JavaScript files /// </summary> /// <param name="helper"></param> /// <param name="bundleName">The name of the bundle property and the name of the minified file</param> /// <param name="cssOrJs">This says if its css or javascript. NOTE: the enum string is used as the dir and the file type</param> /// <param name="forceState">if not null then true forces into debug state and false forces production state</param> /// <returns></returns> private static MvcHtmlString CreateHtmlIncludes(this HtmlHelper helper, string bundleName, CssOrJs cssOrJs, bool? forceState = null) { var isDebug = false; #if DEBUG isDebug = true; #endif if (forceState != null) isDebug = (bool)forceState; var bundler = GetBundlerForBowerCached(helper); var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); return new MvcHtmlString(bundler.CalculateHtmlIncludes(bundleName, cssOrJs, isDebug, urlHelper.Content)); }
public FileTypeConfigInfo GetFileTypeData(CssOrJs cssOrJs) { return(cssOrJs == CssOrJs.Css ? new FileTypeConfigInfo(CssDirectory, CssDebugHtmlFormatString, CssNonDebugHtmlFormatString, CssCdnHtmlFormatString) : new FileTypeConfigInfo(JsDirectory, JsDebugHtmlFormatString, JsNonDebugHtmlFormatString, JsCdnHtmlFormatString)); }
//--------------------------------------- //private methods /// <summary> /// This returns the html to include either CSS or JavaScript files /// </summary> /// <param name="urlHelper"></param> /// <param name="bundleName">The name of the bundle property and the name of the minified file</param> /// <param name="cssOrJs">This says if its css or javascript. NOTE: the enum string is used as the dir and the file type</param> /// <param name="forceState">if not null then true forces into debug state and false forces production state</param> /// <returns></returns> private static HtmlString CreateHtmlIncludes(this IUrlHelper urlHelper, string bundleName, CssOrJs cssOrJs, bool? forceState = null) { var isDebug = false; #if DEBUG isDebug = true; #endif if (forceState != null) isDebug = (bool)forceState; throw new NotImplementedException(); //var bundler = new BundlerForBowerDnx(GetAbsDataDirectory(), urlHelper.Content); //return new HtmlString(bundler.CalculateHtmlIncludes(bundleName, cssOrJs, isDebug)); }