public override IEnumerable<PvcCore.PvcStream> Execute(IEnumerable<PvcCore.PvcStream> inputStreams) { var resultStreams = new List<PvcStream>(); SwitchParser switchParser = null; if (!String.IsNullOrEmpty(this.commandLineSwitches)) { switchParser = new SwitchParser(); switchParser.Parse(this.commandLineSwitches); } foreach (var inputStream in inputStreams) { var dirName = Path.GetDirectoryName(inputStream.StreamName); var fileName = Path.GetFileNameWithoutExtension(inputStream.StreamName) + ".min" + Path.GetExtension(inputStream.StreamName); var resultName = Path.Combine(dirName, fileName); var fileContent = new StreamReader(inputStream).ReadToEnd(); var minifier = new Minifier(); var sourceStream = new MemoryStream(); var outputWriter = new StreamWriter(sourceStream); if (inputStream.StreamName.EndsWith(".js")) { // Currently AjaxMin only supports JS source maps if (this.generateSourceMaps) { var resultMapName = resultName + ".map"; var utf8 = new UTF8Encoding(false); var mapStream = new MemoryStream(); var mapWriter = new SourcemapStreamWriter(mapStream, utf8); var sourceMap = new V3SourceMap(mapWriter); if (sourceMap != null) { if (switchParser == null) { switchParser = new SwitchParser(); } switchParser.JSSettings.SymbolsMap = sourceMap; switchParser.JSSettings.TermSemicolons = true; sourceMap.StartPackage(resultName, resultMapName); outputWriter.Write(minifier.MinifyJavaScript(fileContent, switchParser.JSSettings)); sourceMap.EndPackage(); sourceMap.EndFile(outputWriter, "\r\n"); sourceMap.Dispose(); mapWriter.Flush(); resultStreams.Add(new PvcStream(() => mapStream).As(resultMapName)); } } else { CodeSettings settings = new CodeSettings(); if (switchParser != null) settings = switchParser.JSSettings; outputWriter.Write(minifier.MinifyJavaScript(fileContent, settings)); } } else { CssSettings settings = new CssSettings(); if (switchParser != null) settings = switchParser.CssSettings; outputWriter.Write(minifier.MinifyStyleSheet(fileContent, settings)); } foreach (var error in minifier.ErrorList) { Console.Error.WriteLine(error.ToString()); } outputWriter.Flush(); resultStreams.Add(new PvcStream(() => sourceStream).As(resultName)); } return resultStreams; }
public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable <BundleFile> files) { if (files == null) { return(string.Empty); } if (context == null) { throw new ArgumentNullException("context"); } if (bundle == null) { throw new ArgumentNullException("bundle"); } // Generates source map using an approach documented here: http://ajaxmin.codeplex.com/discussions/446616 // Get paths and create any directories required var mapVirtualPath = bundle.Path + ".map"; var sourcePath = HostingEnvironment.MapPath(bundle.Path); var mapPath = HostingEnvironment.MapPath(mapVirtualPath); var directoryPath = Path.GetDirectoryName(mapPath); if (directoryPath == null) { throw new InvalidOperationException("directoryPath was invalid."); } if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } // Concatenate file contents to be minified, including the sourcemap hints var contentConcated = new StringBuilder(); foreach (var file in files) { var filePath = HostingEnvironment.MapPath(file.VirtualFile.VirtualPath); if (bundleFileType == BundleFileTypes.JavaScript) { contentConcated.AppendLine(";///#SOURCE 1 1 " + filePath); } contentConcated.AppendLine(file.ApplyTransforms()); } var contentConcatedString = contentConcated.ToString(); // Try minify (+ source map) using AjaxMin dll try { var contentBuilder = new StringBuilder(); using (var contentWriter = new StringWriter(contentBuilder)) using (var mapWriter = new StreamWriter(mapPath, false, new UTF8Encoding(false))) using (var sourceMap = new V3SourceMap(mapWriter)) { sourceMap.StartPackage(sourcePath, mapPath); var settings = new CodeSettings() { EvalTreatment = EvalTreatment.MakeImmediateSafe, PreserveImportantComments = false, SymbolsMap = sourceMap, TermSemicolons = true }; var minifier = new Minifier(); string contentMinified; switch (bundleFileType) { case BundleFileTypes.JavaScript: contentMinified = minifier.MinifyJavaScript(contentConcatedString, settings); break; case BundleFileTypes.StyleSheet: var cssSettings = new CssSettings { TermSemicolons = true }; contentMinified = minifier.MinifyStyleSheet(contentConcatedString, cssSettings, settings); break; default: throw new ArgumentOutOfRangeException("Unrecognised BundleFileTypes enum value. Could not find minifier method to handle."); } if (minifier.ErrorList.Count > 0) { return(GenerateMinifierErrorsContent(contentConcatedString, minifier)); } contentWriter.Write(contentMinified); sourceMap.EndPackage(); sourceMap.EndFile(contentWriter, "\r\n"); } return(contentBuilder.ToString()); } catch (Exception ex) { Trace.TraceWarning("An exception occurred trying to build bundle contents for bundle with virtual path: " + bundle.Path + ". See Exception details.", ex, typeof(AjaxMinBundleBuilder)); return(GenerateGenericErrorsContent(contentConcatedString)); } }
public override IEnumerable <PvcCore.PvcStream> Execute(IEnumerable <PvcCore.PvcStream> inputStreams) { var resultStreams = new List <PvcStream>(); SwitchParser switchParser = null; if (!String.IsNullOrEmpty(this.commandLineSwitches)) { switchParser = new SwitchParser(); switchParser.Parse(this.commandLineSwitches); } foreach (var inputStream in inputStreams) { var dirName = Path.GetDirectoryName(inputStream.StreamName); var fileName = Path.GetFileNameWithoutExtension(inputStream.StreamName) + ".min" + Path.GetExtension(inputStream.StreamName); var resultName = Path.Combine(dirName, fileName); var fileContent = new StreamReader(inputStream).ReadToEnd(); var minifier = new Minifier(); var sourceStream = new MemoryStream(); var outputWriter = new StreamWriter(sourceStream); if (inputStream.StreamName.EndsWith(".js")) { // Currently AjaxMin only supports JS source maps if (this.generateSourceMaps) { var resultMapName = resultName + ".map"; var utf8 = new UTF8Encoding(false); var mapStream = new MemoryStream(); var mapWriter = new SourcemapStreamWriter(mapStream, utf8); var sourceMap = new V3SourceMap(mapWriter); if (sourceMap != null) { if (switchParser == null) { switchParser = new SwitchParser(); } switchParser.JSSettings.SymbolsMap = sourceMap; switchParser.JSSettings.TermSemicolons = true; sourceMap.StartPackage(resultName, resultMapName); outputWriter.Write(minifier.MinifyJavaScript(fileContent, switchParser.JSSettings)); sourceMap.EndPackage(); sourceMap.EndFile(outputWriter, "\r\n"); sourceMap.Dispose(); mapWriter.Flush(); resultStreams.Add(new PvcStream(() => mapStream).As(resultMapName)); } } else { CodeSettings settings = new CodeSettings(); if (switchParser != null) { settings = switchParser.JSSettings; } outputWriter.Write(minifier.MinifyJavaScript(fileContent, settings)); } } else { CssSettings settings = new CssSettings(); if (switchParser != null) { settings = switchParser.CssSettings; } outputWriter.Write(minifier.MinifyStyleSheet(fileContent, settings)); } foreach (var error in minifier.ErrorList) { Console.Error.WriteLine(error.ToString()); } outputWriter.Flush(); resultStreams.Add(new PvcStream(() => sourceStream).As(resultName)); } return(resultStreams); }