예제 #1
0
        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);
        }
예제 #2
0
 /// <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);
 }
예제 #3
0
 /// <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>
 /// 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;
 }
예제 #5
0
 /// <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);
 }
예제 #6
0
 /// <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));
 }
예제 #7
0
        /// <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));
        }
예제 #8
0
        /// <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));
        }
예제 #9
0
 /// <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);
 }
예제 #10
0
 private static string Minify(string text, MinificationType minificationType)
 {
     if (minificationType == MinificationType.StyleSheet)
     {
         return(MinifyStyleSheet(text));
     }
     if (minificationType == MinificationType.JavaScript)
     {
         return(MinifyJavaScript(text));
     }
     return(text);
 }
예제 #11
0
 /// <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);
 }
예제 #13
0
 /// <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);
         }
     }
 }
예제 #14
0
 /// <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);
 }
예제 #17
0
 /// <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));
 }
예제 #18
0
 /// <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);
         }
     }
 }