public static int BuildAssetBundle(IEnumerable <string> fileEntries, MinificationType minificationType, string applicationRootPath) { if (fileEntries == null) { throw new ArgumentNullException(nameof(fileEntries)); } if (applicationRootPath == null) { throw new ArgumentNullException(nameof(applicationRootPath)); } var files = fileEntries.BuildFileList(applicationRootPath).ToArray(); var nonHtmlFileContents = files .Where(file => file.EndsWith(".html", StringComparison.OrdinalIgnoreCase) == false) .Select(file => $"\n/* {file} */\n{ReadFile(file)}") .Aggregate(new StringBuilder(), (a, b) => a.Append("\n").Append(b)); var htmlFileContents = files .Where(file => file.EndsWith(".html", StringComparison.OrdinalIgnoreCase)) .AsAngluarModule(applicationRootPath); var contents = Minify(string.Join("\n", nonHtmlFileContents + htmlFileContents), minificationType); var bytes = Encoding.UTF8.GetBytes(contents); var etag = ETag(bytes); var hash = etag.GetHashCode(); AssetBundles.TryAdd(hash, new AssetBundle { ETag = etag, Bytes = bytes }); return(hash); }
/// <summary> /// Combines and minifies various files /// </summary> /// <param name="Input">input strings (file contents)</param> /// <param name="Type">Type of minification</param> /// <returns>A minified/packed string</returns> public static string Combine(this IEnumerable<string> Input, MinificationType Type = MinificationType.HTML) { StringBuilder Output = new StringBuilder(); foreach (string Temp in Input) Output.Append(Temp).Append("\n"); return Minify(Output.ToString(), Type); }
/// <summary> /// Constructor /// </summary> /// <param name="StreamUsing">The stream for the page</param> /// <param name="Compression">The compression we're using (gzip or deflate)</param> /// <param name="Type">Minification type to use (defaults to HTML)</param> public UglyStream(Stream StreamUsing, CompressionType Compression, MinificationType Type = MinificationType.HTML) : base() { this.Compression = Compression; this.StreamUsing = StreamUsing; this.Type = Type; }
/// <summary> /// Combines and minifies various files /// </summary> /// <param name="Input">input strings (file contents)</param> /// <param name="Type">Type of minification</param> /// <returns>A minified/packed string</returns> public static string Combine(this IEnumerable<FileInfo> Input, MinificationType Type = MinificationType.HTML) { StringBuilder Output = new StringBuilder(); foreach (FileInfo Temp in Input.Where(x => x.Exists)) Output.Append(Temp.Read()).Append("\n"); return Minify(Output.ToString(), Type); }
/// <summary> /// Minifies the file based on the data type specified /// </summary> /// <param name="Input">Input file</param> /// <returns>A stripped file</returns> public static string Minify(this FileInfo Input, MinificationType Type = MinificationType.HTML) { Input.ThrowIfNull("Input"); if (!Input.Exists) { throw new ArgumentException("Input file does not exist"); } return(Input.Read().Minify(Type)); }
/// <summary> /// Combines and minifies various files /// </summary> /// <param name="Input">input strings (file contents)</param> /// <returns>A minified/packed string</returns> public static string Combine(this IEnumerable <string> Input, MinificationType Type = MinificationType.HTML) { StringBuilder Output = new StringBuilder(); foreach (string Temp in Input) { Output.Append(Temp).Append("\n"); } return(Minify(Output.ToString(), Type)); }
/// <summary> /// Combines and minifies various files /// </summary> /// <param name="Input">input strings (file contents)</param> /// <returns>A minified/packed string</returns> public static string Combine(this IEnumerable <FileInfo> Input, MinificationType Type = MinificationType.HTML) { StringBuilder Output = new StringBuilder(); foreach (FileInfo Temp in Input.Where(x => x.Exists)) { Output.Append(Temp.Read()).Append("\n"); } return(Minify(Output.ToString(), Type)); }
/// <summary> /// Minifies the file based on the data type specified /// </summary> /// <param name="Input">Input text</param> /// <param name="Type">Type of minification to run</param> /// <returns>A stripped file</returns> public static string Minify(this string Input, MinificationType Type = MinificationType.HTML) { if (string.IsNullOrEmpty(Input)) return ""; if (Type == MinificationType.CSS) return CSSMinify(Input); if (Type == MinificationType.JavaScript) return JavaScriptMinify(Input); return HTMLMinify(Input); }
private static string Minify(string text, MinificationType minificationType) { if (minificationType == MinificationType.StyleSheet) { return(MinifyStyleSheet(text)); } if (minificationType == MinificationType.JavaScript) { return(MinifyJavaScript(text)); } return(text); }
/// <summary> /// Minifies the file based on the data type specified /// </summary> /// <param name="Input">Input text</param> /// <returns>A stripped file</returns> public static string Minify(this string Input, MinificationType Type = MinificationType.HTML) { if (string.IsNullOrEmpty(Input)) { return(""); } if (Type == MinificationType.CSS) { return(CSSMinify(Input)); } if (Type == MinificationType.JavaScript) { return(JavaScriptMinify(Input)); } return(HTMLMinify(Input)); }
/// <summary> /// Adds HTTP compression to the current context /// </summary> /// <param name="Context">Current context</param> /// <param name="RemovePrettyPrinting"> /// Sets the response filter to a special stream that removes pretty printing from content /// </param> /// <param name="Type"> /// The minification type to use (defaults to HTML if RemovePrettyPrinting is set to true, /// but can also deal with CSS and Javascript) /// </param> public static void HTTPCompress(this HttpContext Context, bool RemovePrettyPrinting = false, MinificationType Type = MinificationType.HTML) { Contract.Requires <ArgumentNullException>(Context != null, "Context"); if (Context.Request.UserAgent != null && Context.Request.UserAgent.Contains("MSIE 6")) { return; } Context.Response.Filter = RemovePrettyPrinting ? (System.IO.Stream) new UglyStream(Context.Response.Filter, CompressionType.GZip, Type) : new GZipStream(Context.Response.Filter, CompressionMode.Compress); }
/// <summary> /// Adds HTTP compression to the current context /// </summary> /// <param name="Context">Current context</param> /// <param name="RemovePrettyPrinting">Sets the response filter to a special stream that /// removes pretty printing from content</param> /// <param name="Type">The minification type to use (defaults to HTML if RemovePrettyPrinting /// is set to true, but can also deal with CSS and Javascript)</param> public static void HTTPCompress(this HttpContext Context, bool RemovePrettyPrinting = false, MinificationType Type = MinificationType.HTML) { Context.ThrowIfNull("Context"); if (Context.Request.UserAgent != null && Context.Request.UserAgent.Contains("MSIE 6")) { return; } if (RemovePrettyPrinting) { if (Context.IsEncodingAccepted(GZIP)) { Context.Response.Filter = new UglyStream(Context.Response.Filter, CompressionType.GZip, Type); Context.SetEncoding(GZIP); } else if (Context.IsEncodingAccepted(DEFLATE)) { Context.Response.Filter = new UglyStream(Context.Response.Filter, CompressionType.Deflate, Type); Context.SetEncoding(DEFLATE); } } else { if (Context.IsEncodingAccepted(GZIP)) { Context.Response.Filter = new GZipStream(Context.Response.Filter, CompressionMode.Compress); Context.SetEncoding(GZIP); } else if (Context.IsEncodingAccepted(DEFLATE)) { Context.Response.Filter = new DeflateStream(Context.Response.Filter, CompressionMode.Compress); Context.SetEncoding(DEFLATE); } } }
/// <summary> /// Combines and minifies various files /// </summary> /// <param name="Input">input strings (file contents)</param> /// <param name="Type">Type of minification</param> /// <returns>A minified/packed string</returns> public static string Minify(this IEnumerable <FileInfo> Input, MinificationType Type = MinificationType.HTML) { Contract.Requires <ArgumentNullException>(Input != null, "Input"); return(Minify(Input.Where(x => x.Exists).ToString(x => x.Read(), System.Environment.NewLine), Type)); }
/// <summary> /// Combines and minifies various files /// </summary> /// <param name="Input">input strings (file contents)</param> /// <param name="Type">Type of minification</param> /// <returns>A minified/packed string</returns> public static string Minify(this IEnumerable<FileInfo> Input, MinificationType Type = MinificationType.HTML) { Contract.Requires<ArgumentNullException>(Input != null, "Input"); return Minify(Input.Where(x => x.Exists).ToString(x => x.Read(), System.Environment.NewLine), Type); }
/// <summary> /// Minifies the file based on the data type specified /// </summary> /// <param name="Input">Input file</param> /// <param name="Type">Type of minification to run</param> /// <returns>A stripped file</returns> public static string Minify(this FileInfo Input, MinificationType Type = MinificationType.HTML) { Contract.Requires<ArgumentNullException>(Input != null, "Input"); Contract.Requires<System.IO.FileNotFoundException>(Input.Exists, "Input file does not exist"); return Input.Read().Minify(Type); }
/// <summary> /// Minifies the file based on the data type specified /// </summary> /// <param name="Input">Input file</param> /// <param name="Type">Type of minification to run</param> /// <returns>A stripped file</returns> public static string Minify(this FileInfo Input, MinificationType Type = MinificationType.HTML) { Contract.Requires <ArgumentNullException>(Input != null, "Input"); Contract.Requires <System.IO.FileNotFoundException>(Input.Exists, "Input file does not exist"); return(Input.Read().Minify(Type)); }
/// <summary> /// Minifies the file based on the data type specified /// </summary> /// <param name="Input">Input file</param> /// <param name="Type">Type of minification to run</param> /// <returns>A stripped file</returns> public static string Minify(this FileInfo Input, MinificationType Type = MinificationType.HTML) { Input.ThrowIfNull("Input"); if (!Input.Exists) throw new ArgumentException("Input file does not exist"); return Input.Read().Minify(Type); }
/// <summary> /// Adds HTTP compression to the current context /// </summary> /// <param name="Context">Current context</param> /// <param name="RemovePrettyPrinting"> /// Sets the response filter to a special stream that removes pretty printing from content /// </param> /// <param name="Type"> /// The minification type to use (defaults to HTML if RemovePrettyPrinting is set to true, /// but can also deal with CSS and Javascript) /// </param> public static void HTTPCompress(this HttpContext Context, bool RemovePrettyPrinting = false, MinificationType Type = MinificationType.HTML) { Contract.Requires<ArgumentNullException>(Context != null, "Context"); if (Context.Request.UserAgent != null && Context.Request.UserAgent.Contains("MSIE 6")) return; Context.Response.Filter = RemovePrettyPrinting ? (System.IO.Stream)new UglyStream(Context.Response.Filter, CompressionType.GZip, Type) : new GZipStream(Context.Response.Filter, CompressionMode.Compress); }
/// <summary> /// Adds HTTP compression to the current context /// </summary> /// <param name="Context">Current context</param> /// <param name="RemovePrettyPrinting">Sets the response filter to a special stream that /// removes pretty printing from content</param> /// <param name="Type">The minification type to use (defaults to HTML if RemovePrettyPrinting /// is set to true, but can also deal with CSS and Javascript)</param> public static void HTTPCompress(this HttpContext Context, bool RemovePrettyPrinting = false, MinificationType Type = MinificationType.HTML) { Contract.Requires<ArgumentNullException>(Context != null, "Context"); if (Context.Request.UserAgent != null && Context.Request.UserAgent.Contains("MSIE 6")) return; if (RemovePrettyPrinting) { if (Context.IsEncodingAccepted(GZIP)) { Context.Response.Filter = new UglyStream(Context.Response.Filter, CompressionType.GZip, Type); Context.SetEncoding(GZIP); } else if (Context.IsEncodingAccepted(DEFLATE)) { Context.Response.Filter = new UglyStream(Context.Response.Filter, CompressionType.Deflate, Type); Context.SetEncoding(DEFLATE); } } else { if (Context.IsEncodingAccepted(GZIP)) { Context.Response.Filter = new GZipStream(Context.Response.Filter, CompressionMode.Compress); Context.SetEncoding(GZIP); } else if (Context.IsEncodingAccepted(DEFLATE)) { Context.Response.Filter = new DeflateStream(Context.Response.Filter, CompressionMode.Compress); Context.SetEncoding(DEFLATE); } } }