コード例 #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
ファイル: Minification.cs プロジェクト: jerrymds/Concord
 /// <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;
 }
コード例 #4
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;
 }
コード例 #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));
 }
コード例 #12
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)
 {
     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));
 }
コード例 #15
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);
 }
コード例 #16
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);
 }
コード例 #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
ファイル: Minification.cs プロジェクト: jerrymds/Concord
 /// <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);
 }
コード例 #19
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)
 {
     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);
 }
コード例 #20
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)
 {
     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);
         }
     }
 }