Created
January 10, 2014 09:32
-
-
Save gr4b4z/8349097 to your computer and use it in GitHub Desktop.
YuiCompressorTransform implementation whitout bug "Method not found: 'System.Collections.Generic.IEnumerable`1<System.IO.FileInfo> System.Web.Optimization.BundleResponse.get_Files()'."
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using System.Web.Optimization; | |
namespace Yahoo.Yui.Compressor.Web.Optimization | |
{ | |
public class YuiCompressorTransform : IBundleTransform | |
{ | |
private readonly CompressorConfig _compressorConfig; | |
public YuiCompressorTransform(CompressorConfig compressorConfig) | |
{ | |
if (compressorConfig == null) | |
{ | |
throw new ArgumentNullException("compressorConfig"); | |
} | |
_compressorConfig = compressorConfig; | |
} | |
#region Implementation of IBundleTransform | |
public void Process(BundleContext context, BundleResponse response) | |
{ | |
if (context == null) | |
{ | |
throw new ArgumentNullException("context"); | |
} | |
if (response == null) | |
{ | |
throw new ArgumentNullException("response"); | |
} | |
// Grab all of the content. | |
var rawContent = new StringBuilder(); | |
foreach (var fileInfo in response.Files) | |
{ | |
using (var stream = fileInfo.VirtualFile.Open()) | |
{ | |
var streamReader = new StreamReader(stream); | |
rawContent.Append(streamReader.ReadToEnd()); | |
streamReader.Close(); | |
} | |
} | |
// Now lets compress. | |
var compressor = DetermineCompressor(_compressorConfig); | |
var output = compressor.Compress(rawContent.ToString()); | |
context.HttpContext.Response.Cache.SetLastModifiedFromFileDependencies(); | |
response.Content = output; | |
response.ContentType = compressor is CssCompressor ? "text/css" : "text/javascript"; | |
} | |
#endregion | |
private ICompressor DetermineCompressor(CompressorConfig compressorConfig) | |
{ | |
if (compressorConfig == null) | |
{ | |
throw new ArgumentNullException("compressorConfig"); | |
} | |
// Define the compressor we wish to use. | |
ICompressor compressor; | |
var config = _compressorConfig as CssCompressorConfig; | |
if (config != null) | |
{ | |
var cssCompressorConfig = config; | |
compressor = new CssCompressor | |
{ | |
CompressionType = cssCompressorConfig.CompressionType, | |
LineBreakPosition = cssCompressorConfig.LineBreakPosition, | |
RemoveComments = cssCompressorConfig.RemoveComments | |
}; | |
} | |
else if (_compressorConfig is JavaScriptCompressorConfig) | |
{ | |
var jsCompressorConfig = (JavaScriptCompressorConfig)_compressorConfig; | |
compressor = new JavaScriptCompressor | |
{ | |
CompressionType = jsCompressorConfig.CompressionType, | |
DisableOptimizations = jsCompressorConfig.DisableOptimizations, | |
Encoding = jsCompressorConfig.Encoding, | |
ErrorReporter = jsCompressorConfig.ErrorReporter, | |
IgnoreEval = jsCompressorConfig.IgnoreEval, | |
LineBreakPosition = jsCompressorConfig.LineBreakPosition, | |
LoggingType = jsCompressorConfig.LoggingType, | |
ObfuscateJavascript = jsCompressorConfig.ObfuscateJavascript, | |
PreserveAllSemicolons = jsCompressorConfig.PreserveAllSemicolons, | |
ThreadCulture = jsCompressorConfig.ThreadCulture | |
}; | |
} | |
else | |
{ | |
throw new InvalidOperationException("Unhandled CompressorConfig instance while trying to initialize internal compressor properties."); | |
} | |
return compressor; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment