public ProjectBuilder BuildBundle() { using (var cache = new BuildCache()) { var files = ResourceMinifier.GetFiles(file => file.FullName.Contains("src" + Path.DirectorySeparatorChar) && !file.FullName.Contains("client" + Path.DirectorySeparatorChar) ); var changedFiles = files.Where(file => !cache.Contains(file)).ToArray(); if (changedFiles.Any()) { var urlList = from file in Directory.GetFiles("min") where !file.Contains("dark-slice") && !Path.GetFileName(file).StartsWith("bundle.") select file.Replace(@"\", "/"); var hashDict = new Dictionary <string, string>(); var zipName = "min/bundle.zip"; if (File.Exists(zipName)) { File.Delete(zipName); } using (var sha256 = new SHA256Managed()) using (var zip = ZipFile.Open(zipName, ZipArchiveMode.Update)) { foreach (var url in urlList) { var filename = Path.GetFileName(url); var hash = string.Join("", sha256.ComputeHash(File.OpenRead(url)).Select(b => b.ToString("X2")).ToArray()); zip.CreateEntryFromFile(url, filename, CompressionLevel.NoCompression); hashDict.Add(filename, hash); } } File.WriteAllText("min/bundle.json", JsonConvert.SerializeObject(hashDict, Formatting.Indented)); } } WriteSuccess("Bundle build complete."); return(this); }
public virtual ProjectBuilder Build(ProjectBuilder builder) { var files = GetFiles(file => FileFilter(file) && !file.FullName.Contains(@".vs\") && !file.FullName.Contains(@".vscode\") && !file.FullName.Contains(@"build-scripts\") && !file.FullName.Contains(@"node_modules\") && !file.FullName.Contains(@".backup.") ); using (var cache = new BuildCache()) { var changedFiles = files.Where(file => !cache.Contains(file)); Parallel.ForEach(changedFiles, path => { builder.WriteInfo($"{ResourceType} minify: {path}"); var text = File.ReadAllText(path); var result = Minify(text); var outputPath = GetMinifiedFileName(path); File.WriteAllText(outputPath, result); cache.AddCache(path); builder.WriteHint($"\t=> {outputPath.PadRight(48)}{(100.0 * result.Length / text.Length):0.##}%"); }); cache.SaveCache(); } files.ForEach(file => { builder.OriginalResourceLength += new FileInfo(file).Length; builder.MinifiedResourceLength += new FileInfo(GetMinifiedFileName(file)).Length; }); builder.WriteSuccess($"{ResourceType} minify complete."); return(builder); }
public ProjectBuilder BuildTypeScripts() { var tsc = new TypeScriptCompiler(); var uglifyJs = new JavascriptMinifier(); var files = ResourceMinifier.GetFiles(file => file.FullName.Contains(@"src\") && file.Extension == ".ts" && !file.Name.EndsWith(".d.ts") ); using (var cache = new BuildCache()) { var changedFiles = files.Where(file => !cache.Contains(file)).ToArray(); if (changedFiles.Any()) { changedFiles.Where(f => !f.EndsWith(".vue.ts")).ForEach(file => { cache.AddCache(file); WriteInfo($"TypeScript build: {file}"); }); tsc.Run(); Parallel.ForEach(changedFiles.Where(f => !f.EndsWith(".vue.ts")).Select(f => ".ts-output/" + f.Replace(".ts", ".js").Replace($"src{Path.DirectorySeparatorChar}", "")), file => { var text = RegexReplacer.Replace(File.ReadAllText(file), @"import\(\(\(\)\s*=>\s*(.*)\)\(\)\)", match => { return($"import({match.Groups[1].Value})"); }); var min = uglifyJs.Minify(text); var minFile = ResourceMinifier.GetMinimizedFileName(file); File.WriteAllText(minFile, min); WriteHint($"\t=> {minFile}"); }); } cache.SaveCache(); } WriteSuccess("TypeScript build complete."); return(this); }
public ProjectBuilder BuildVue() { var files = ResourceMinifier.GetFiles(file => file.Extension == ".vue" ); using (var cache = new BuildCache()) { var changedFiles = files.Where(file => !cache.Contains(file)).ToArray(); if (changedFiles.Any()) { Parallel.ForEach(changedFiles, file => { cache.AddCache(file); WriteInfo($"Vue build: {file}"); var source = File.ReadAllText(file); var vueFile = new VueFile(source); var compiledText = new StringBuilder(""); if (vueFile.Tamplate is null) { throw new VueBuildException($"{file}: Missing <tamplate>"); } else { var uglifyHtml = new UglifyHtml(); if (vueFile.TamplateLang == "html") { compiledText.Append($"const template = /*html*/`{uglifyHtml.Run(vueFile.Tamplate)}`;"); } else { throw new VueBuildException($"{file}: Unsupported <template> lang '{vueFile.TamplateLang}'"); } } if (vueFile.Style != null) { var styleID = Path.ChangeExtension(Path.GetFileName(file), null).Replace(".", "-") + "-style"; var uglifyCss = new UglifyCss(); if (vueFile.StyleLang == "css") { compiledText.Append($"resources.applyStyleFromText(`{uglifyCss.Run(vueFile.Style)}`,'{styleID}');"); } else if (vueFile.StyleLang == "scss") { var sass = new SassSingleCompiler(); var css = uglifyCss.Run(sass.Run(vueFile.Style).Replace("@charset \"UTF-8\";", "")); compiledText.Append($"resources.applyStyleFromText(`{css}`,'{styleID}');"); } else { throw new VueBuildException($"{file}: Unsupported <style> lang '{vueFile.StyleLang}'"); } } if (vueFile.Script is null) { throw new VueBuildException($"{file}: Missing <script>"); } else { if (vueFile.ScriptLang == "js" || vueFile.ScriptLang == "javascript") { var script = vueFile.Script.Replace("export default ", "return {export:Object.assign({template},").Trim().TrimEnd(';'); compiledText.Append($"{script})}}"); } else if (vueFile.ScriptLang == "ts" || vueFile.ScriptLang == "typescript") { var tsFile = file + ".ts"; var jsFile = ".ts-output" + Path.DirectorySeparatorChar + file.Replace("src" + Path.DirectorySeparatorChar, "") + ".js"; var script = File.ReadAllText(jsFile).Replace("export default ", "return {export:Object.assign({template},").Trim().TrimEnd(';'); compiledText.Append($"{script})}}"); File.Delete(tsFile); // File.Delete(jsFile); } else { throw new VueBuildException($"{file}: Unsupported <script> lang '{vueFile.ScriptLang}'"); } } // compiledText.Append("}})()"); var minFile = "min" + Path.DirectorySeparatorChar + Path.GetFileName(file) + ".min.js"; var jsc = new JavascriptMinifier(); File.WriteAllText(minFile, jsc.Minify(compiledText.ToString())); UpdateCachedMinFile(minFile); WriteHint($"\t=> {minFile}"); }); } cache.SaveCache(); } WriteSuccess("Vue build complete."); return(this); }