/// <summary> /// This function bundles CSS, JavaScript, and Images into a single html file /// </summary> /// <param name="inputPath">The path of the main html file</param> /// <returns>A WebFile containg the bundle</returns> private WebFile Bundle(String inputPath) { var inputFile = new WebFile(inputPath); var outputFile = inputFile; //Adding Font and Images to 2nd Layer foreach (var f in inputFile.DependencyList.Where(x => x.DependencyList.Any())) { foreach (var d in f.DependencyList) { f.Body = f.Body.Replace(d.ReferenceUri, d.DataUri); } } //Bundling 1st Layer Text-Based (JS and CSS) var html = inputFile.Body; var css = BundleDependenciesByType(inputFile.DependencyList, "css"); var js = BundleDependenciesByType(inputFile.DependencyList, "javascript"); //Optimization if (OptimizeHtml != null) { html = OptimizeHtml(html); } if (OptimizeCss != null) { css = OptimizeCss(css); } if (OptimizeJs != null) { js = OptimizeJs(js); } //Remove Imported dependency nodes foreach (var dependency in inputFile.DependencyList.Where(x => (x.MimeType.Contains("css") || x.MimeType.Contains("javascript")))) { WebFileUtilities.RemoveReferenceNodes(ref html, dependency.ReferenceUri); } //Add Images to HTML file html = inputFile.DependencyList.Where(x => (!x.MimeType.Contains("css") || !x.MimeType.Contains("javascript"))) .Aggregate(html, (current, d) => current.Replace(d.ReferenceUri, d.DataUri)); WebFileUtilities.InsertNode(ref html, "head", "style", css); //Add Styles WebFileUtilities.InsertNode(ref html, "body", "script", js); //Add Scripts outputFile.Body = html; return(outputFile); }
/// <summary> /// Populates dependency list with Webfiles from the references in the body of the file passed in /// </summary> /// <param name="bodyString">The body content of the file</param> /// <returns>A list of WebFiles</returns> private IList <WebFile> GetDependencies(String bodyString) { var dependencyList = new List <WebFile>(); IEnumerable <string> references = null; switch (MimeType) { case "text/html": references = WebFileUtilities.GetHtmlReferences(bodyString); break; case "text/css": references = WebFileUtilities.GetCssReferences(bodyString); break; } if (references != null) { dependencyList.AddRange(GetValidWebFiles(references)); } return(dependencyList); }
/// <summary> /// Checks a list of Uri if the physical files exist. If they exist a matching webfile is created. /// </summary> /// <param name="uriList">Enumerable List of URIs</param> /// <returns>Enumerable List of Validated Webfiles</returns> private IEnumerable <WebFile> GetValidWebFiles(IEnumerable <string> uriList) { return(uriList.Select(uri => new { Path = WebFileUtilities.GetFullPathFromUri(_basePath, uri), Uri = uri }) .Where(reference => File.Exists(reference.Path)) .Select(reference => new WebFile(reference.Path, reference.Uri))); }