/// <summary> /// Renderiza todos blocos de scripts /// </summary> public static MvcHtmlString RenderScriptBlocks(this HtmlHelper helper) { var scriptBuilder = helper.ViewContext.HttpContext.Items[keyForBlockScript] as StringBuilder ?? new StringBuilder(); string content = scriptBuilder.ToString(); if (!HttpContext.Current.IsDebuggingEnabled && !string.IsNullOrEmpty(content)) { string clearContent = System.Text.RegularExpressions.Regex.Replace(content, "<script[^>]*>|</script>", string.Empty); var minifier = new Microsoft.Ajax.Utilities.Minifier(); content = string.Format("<script type=\"text/javascript\">{0}</script>", minifier.MinifyJavaScript(clearContent)); } return new MvcHtmlString(content); }
protected override void Render(string physicalFileName, TextWriter output) { if (!DebugMode) { var min = new Microsoft.Ajax.Utilities.Minifier(); var writer = new StringWriter(); renderScript(physicalFileName, writer); var compressedJs = min.MinifyJavaScript(writer.ToString()); output.Write(compressedJs); } else { renderScript(physicalFileName, output); } }
private void ExecuteCruncher(ITaskItem scriptItem) { string script = File.ReadAllText(scriptItem.ItemSpec); ScriptCruncher cruncher = new ScriptCruncher(); ScriptCruncherSettings cruncherSettings = new ScriptCruncherSettings() { StripDebugStatements = false, OutputMode = Microsoft.Ajax.Utilities.OutputMode.SingleLine, IgnorePreprocessorDefines = true, IgnoreConditionalCompilation = true, TermSemicolons = true, }; cruncherSettings.AddNoAutoRename("$"); cruncherSettings.SetDebugNamespaces(null); string crunchedScript = cruncher.MinifyJavaScript(script, cruncherSettings); File.WriteAllText(scriptItem.ItemSpec, crunchedScript); }
private string GetProxyJs(HttpContext context) { if (_proxyJs == null) lock (_syncRoot) if (_proxyJs == null) { var script = _generator.GenerateProxyScript(); if (!context.IsDebuggingEnabled) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); script = minifier.MinifyJavaScript(script); } _proxyJs = script; } return _proxyJs; }
public string Minify(string content) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); return(minifier.MinifyJavaScript(content)); }
public string MinifyJavascript(string script) { Microsoft.Ajax.Utilities.Minifier minifier = new Microsoft.Ajax.Utilities.Minifier(); script = minifier.MinifyJavaScript(script); return(script); }
private string MinifyStyleSheet(string script) { Microsoft.Ajax.Utilities.Minifier minifier = new Microsoft.Ajax.Utilities.Minifier(); script = minifier.MinifyStyleSheet(script); return(script); }
public string Minify(string content) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); return minifier.MinifyJavaScript(content); }
public CompilerResults Compile(CompileOptions compileOptions) { CompilerResults result = new CompilerResults(); GameLoader loader = new GameLoader(); UpdateStatus(string.Format("Compiling {0} to {1}", compileOptions.Filename, compileOptions.OutputFolder)); if (!loader.Load(compileOptions.Filename)) { result.Errors = loader.Errors; } else { UpdateStatus("Loaded successfully"); result.Warnings = loader.Warnings; result.Success = true; var substitutionText = GetSubstitutionText(loader, compileOptions.Profile); UpdateStatus("Copying dependencies"); result.IndexHtml = CopyDependenciesToOutputFolder(compileOptions.OutputFolder, substitutionText, compileOptions.DebugMode, compileOptions.Profile, compileOptions.Minify, loader, compileOptions); string saveData = string.Empty; UpdateStatus("Saving"); GameSaver saver = new GameSaver(loader.Elements); saver.Progress += saver_Progress; saveData = saver.Save(); UpdateStatus("Copying resources"); CopyResourcesToOutputFolder(loader.ResourcesFolder, compileOptions.OutputFolder); saveData += GetEmbeddedHtmlFileData(loader.ResourcesFolder); string saveJs = System.IO.Path.Combine(compileOptions.OutputFolder, "game.js"); saveData = System.IO.File.ReadAllText(saveJs) + saveData; if (compileOptions.Minify) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); saveData = minifier.MinifyJavaScript(saveData, new Microsoft.Ajax.Utilities.CodeSettings { MacSafariQuirks = true, RemoveUnneededCode = true, LocalRenaming = Microsoft.Ajax.Utilities.LocalRenaming.CrunchAll }); var encoding = (Encoding)Encoding.ASCII.Clone(); encoding.EncoderFallback = new Microsoft.Ajax.Utilities.JSEncoderFallback(); using (var writer = new System.IO.StreamWriter(saveJs, false, encoding)) { writer.Write(saveData); } } else { System.IO.File.WriteAllText(saveJs, saveData); } UpdateStatus("Finished"); } return result; }
protected override string Minify(string file) { //using AjaxMin as minifyer. less dependencies. var physicalFile = BundleTable.MapPathMethod(file); var minifier = new Microsoft.Ajax.Utilities.Minifier(); var content = File.ReadAllText(physicalFile); if (file.ToLower().EndsWith(".min.css")) return content; var result = minifier.MinifyStyleSheet(content, settings); return result; }
protected override string Minify(string file) { //using AjaxMin as minifyer. less dependencies. var physicalFile = BundleTable.MapPathMethod(file); var minifier = new Microsoft.Ajax.Utilities.Minifier(); var content = File.ReadAllText(physicalFile); string result; if (file.ToLower().EndsWith(".min.js")) result = minifier.MinifyJavaScript(content, new Microsoft.Ajax.Utilities.CodeSettings() { MinifyCode=false, PreserveImportantComments = false }); else result = minifier.MinifyJavaScript(content, settings); return result + ";"; }
public Minifier() { _minifier = new Microsoft.Ajax.Utilities.Minifier(); }
protected override void BuildResult(Stream fs, List<string> filePaths) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); var styleBuilder = new StringBuilder(); foreach (var style in filePaths) { styleBuilder.AppendLine(FileOperator.ReadAllText(style)); } var styles = minifier.MinifyJavaScript(styleBuilder.ToString()); if (minifier.Errors.Count > 0) { styles = styleBuilder.ToString(); } var buffer = Encoding.UTF8.GetBytes(styles); fs.Write(buffer, 0, buffer.Length); }
public void ProcessRequest(HttpContext context) { var js = new StringBuilder(); #if !DEBUG var minifier = new Microsoft.Ajax.Utilities.Minifier(); #endif context.Response.ContentType = "text/javascript"; // ReSharper disable RedundantAssignment var cachedStaticFilesResult = MemoryCache.Default.Get("ED47.Stack.Reflector.Static") as string; // ReSharper restore RedundantAssignment #if DEBUG //Disable caching during development to make debugging easier cachedStaticFilesResult = null; #endif // ReSharper disable ConditionIsAlwaysTrueOrFalse if (cachedStaticFilesResult == null) // ReSharper restore ConditionIsAlwaysTrueOrFalse { var staticScriptBuilder = new StringBuilder(); staticScriptBuilder.AppendLine("/*================== STATIC SCRIPTS ==================*/"); ReflectorHandler.AppendStaticScripts(staticScriptBuilder); staticScriptBuilder.AppendLine("/*=====================================================*/"); cachedStaticFilesResult = staticScriptBuilder.ToString(); } js.Append(cachedStaticFilesResult); var assemblyNames = context.Request.QueryString["assemblyName"]; if (String.IsNullOrWhiteSpace(assemblyNames)) { context.Response.ContentType = "text/javascript"; #if !DEBUG context.Response.Write(minifier.MinifyJavaScript(js.ToString())); #else context.Response.Write(js.ToString()); #endif return; } var assemblyNameArray = assemblyNames.Split(';'); foreach (var assemblyName in assemblyNameArray) { var cacheKey = "ED47.Stack.Reflector?assemblyName=" + assemblyName; // ReSharper disable RedundantAssignment var cachedResult = MemoryCache.Default.Get(cacheKey) as string; // ReSharper restore RedundantAssignment #if DEBUG cachedResult = null; //Disable caching during development to make debugging easier #endif // ReSharper disable ConditionIsAlwaysTrueOrFalse if (cachedResult == null) // ReSharper restore ConditionIsAlwaysTrueOrFalse { cachedResult = ApiReflector.GenerateControllerScript(assemblyName) + ModelReflector.GenerateModelScript(assemblyName); MemoryCache.Default.Add(new CacheItem(cacheKey, cachedResult), new CacheItemPolicy { Priority = CacheItemPriority.NotRemovable }); } js.Append(cachedResult); } #if DEBUG context.Response.Write(js.ToString()); #else context.Response.Write(minifier.MinifyJavaScript(js.ToString())); #endif }
static void Main(string[] args) { var dir = args.FirstOrDefault(x => !x.StartsWith("/") && !x.StartsWith("-") && System.IO.Directory.Exists(x)) .NotEmpty(Environment.CurrentDirectory); var options = new HashSet<string>(args.Where(x => x != dir).Select(x => x.TrimStart('/', '-').ToLower()).Distinct()); var buildAll = options.Contains("build"); var helper = new Helper(dir); HashSet<string> files_in_project = null; Action reindex_project_files = () => { files_in_project = helper.ProjectFiles = new HashSet<string>(FindProjectFiles(dir, false).SelectMany(x => GetFilesInProject(x)), StringComparer.OrdinalIgnoreCase); helper.IndexFiles(); }; reindex_project_files(); helper.OnChanged = file => Console.WriteLine(DateTime.Now.TimeOfDay + " - " + file); var minifier1 = new Microsoft.Ajax.Utilities.Minifier(); CssBundleHelper.MinifyCss = (file, css) => { var cssParser = new Microsoft.Ajax.Utilities.CssParser(); cssParser.FileContext = file; cssParser.Settings = new Microsoft.Ajax.Utilities.CssSettings { CommentMode = Microsoft.Ajax.Utilities.CssComment.None }; return cssParser.Parse(css); }; var vsExtDir = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\VisualStudio\11.0\Extensions"); var lessc_wsf = Utilities.GetFiles(vsExtDir, file => file.Split('\\').Last().ToLower() == "lessc.wsf").FirstOrDefault(); if (string.IsNullOrEmpty(lessc_wsf)) { Console.Error.WriteLine("lessc.wsf could not be found under {0}", vsExtDir); return; } JsHelper.MinifyJs = minifier1.MinifyJavaScript; JsHelper.MinifyJsWithSourceMap = (file, js) => { var baseFile = file.Substring(0, file.Length - ".js".Length); var min_file = baseFile + ".min.js"; var map_file = min_file + ".map"; var min_filename = Path.GetFileName(min_file); var map_filename = Path.GetFileName(map_file); using (var min_writer = new System.IO.StreamWriter(min_file, false, new System.Text.UTF8Encoding(true))) using (var map_writer = new System.IO.StreamWriter(map_file, false, new System.Text.UTF8Encoding(false))) using (var v3SourceMap = new Microsoft.Ajax.Utilities.V3SourceMap(map_writer)) { v3SourceMap.StartPackage(min_file, map_file); var jsParser = new Microsoft.Ajax.Utilities.JSParser(js); jsParser.FileContext = file; var block = jsParser.Parse(new Microsoft.Ajax.Utilities.CodeSettings { SymbolsMap = v3SourceMap, PreserveImportantComments = false, TermSemicolons = true }); min_writer.Write(block.ToCode() + Environment.NewLine + "//@ sourceMappingURL=" + map_filename); v3SourceMap.EndPackage(); } }; LessHelper.CompileLess = (fileName, less) => { var tempFileName = System.IO.Path.GetTempFileName(); var processStartInfo = new System.Diagnostics.ProcessStartInfo("cscript", "/nologo /s \"" + lessc_wsf + "\" \"" + fileName + "\" \"" + tempFileName + "\"") { CreateNoWindow = true, WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden, UseShellExecute = false }; using (var proc = new System.Diagnostics.Process { StartInfo = processStartInfo }) { proc.Start(); proc.WaitForExit(); var content = System.IO.File.ReadAllText(tempFileName); System.IO.File.Delete(tempFileName); return content; } }; var mre = new System.Threading.ManualResetEventSlim(false); using (var fileSystem = new System.IO.FileSystemWatcher(dir) { EnableRaisingEvents = true, IncludeSubdirectories = true }) { fileSystem.Changed += (s, e) => { if (rx_project_file.IsMatch(e.FullPath)) { reindex_project_files(); return; } if (!files_in_project.Contains(e.FullPath)) return; mre.Wait(500); if (!System.IO.File.Exists(e.FullPath)) return; helper.FileChangedAsync(e.FullPath).Wait(); }; fileSystem.Deleted += (s, e) => { if (!files_in_project.Contains(e.FullPath)) return; mre.Wait(500); helper.FileDeleted(e.FullPath); }; fileSystem.Renamed += (s, e) => { if (!files_in_project.Contains(e.FullPath)) return; mre.Wait(500); helper.FileRenamed(e.OldFullPath, e.FullPath); }; ((Action)(async () => { if (buildAll) { var alldependencies = helper.Files.SelectMany(x => x.Dependencies).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); await System.Threading.Tasks.Task.WhenAll(alldependencies.Select(x => helper.FileChangedAsync(x)).ToArray()); } Console.Beep(); Console.WriteLine("Build Complete"); }))(); while (true) { mre.Wait(500); } } }
public string Minify(string content) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); return(minifier.MinifyStyleSheet(content)); }
private void ExecuteCruncher(ITaskItem scriptItem) { string script = File.ReadAllText(scriptItem.ItemSpec); ScriptCruncher cruncher = new ScriptCruncher(); ScriptCruncherSettings cruncherSettings = new ScriptCruncherSettings() { StripDebugStatements = false, OutputMode = Microsoft.Ajax.Utilities.OutputMode.SingleLine, IgnorePreprocessorDefines = true, IgnoreConditionalCompilation = true }; cruncherSettings.AddNoAutoRename("$"); cruncherSettings.SetDebugNamespaces(null); string crunchedScript = cruncher.MinifyJavaScript(script, cruncherSettings); File.WriteAllText(scriptItem.ItemSpec, crunchedScript); }