示例#1
0
        public ProjectBuilder PrebuildVue()
        {
            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 =>
                    {
                        var source  = File.ReadAllText(file);
                        var vueFile = new VueFile(source);
                        if (vueFile.Script is null)
                        {
                            return;
                        }
                        if (vueFile.ScriptLang == "ts" || vueFile.ScriptLang == "typescript")
                        {
                            File.WriteAllText(file + ".ts", vueFile.Script);
                        }
                    });
                }
            }
            return(this);
        }
示例#2
0
        public ProjectBuilder BuildTypeScripts()
        {
            var tsc   = new TypeScriptCompiler();
            var files = ResourceMinifier.GetFiles(file =>
                                                  (file.FullName.Contains(@"style\") ||
                                                   file.FullName.Contains(@"touch\") ||
                                                   file.FullName.Contains(@"utils\") ||
                                                   file.FullName.Contains(@"video\")) &&
                                                  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.ForEach(file =>
                    {
                        cache.AddCache(file);
                        WriteInfo($"TypeScript build: {file}");
                    });
                    Console.Write(tsc.Run().Trim());
                    changedFiles.ForEach(file =>
                    {
                        WriteHint($"\t=> {file.Replace(".ts", ".js")}");
                    });
                }
                cache.SaveCache();
            }
            WriteSuccess("TypeScript build complete.");
            return(this);
        }
示例#3
0
        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.ForEach(file =>
                    {
                        cache.AddCache(file);
                        WriteInfo($"TypeScript build: {file}");
                    });
                    Console.Write(tsc.Run().Trim());
                    Parallel.ForEach(changedFiles.Select(f => ".ts-output/" + f.Replace(".ts", ".js").Replace($"src{Path.DirectorySeparatorChar}", "")), file => {
                        var min     = uglifyJs.Minify(File.ReadAllText(file));
                        var minFile = ResourceMinifier.GetMinimizedFileName(file);
                        File.WriteAllText(minFile, min);
                        WriteHint($"\t=> {minFile}");
                    });
                }
                cache.SaveCache();
            }
            WriteSuccess("TypeScript build complete.");
            return(this);
        }
示例#4
0
        public ProjectBuilder BuildSass()
        {
            var sass     = new SassCompiler();
            var cleancss = new CssMinifier();
            var files    = ResourceMinifier.GetFiles(file =>
                                                     file.FullName.Contains("src" + Path.DirectorySeparatorChar) &&
                                                     file.Extension == ".scss"
                                                     );

            using (var cache = new BuildCache())
            {
                var changedFiles = files.Where(file => !cache.Contains(file)).ToArray();
                if (changedFiles.Any())
                {
                    changedFiles.ForEach(file =>
                    {
                        cache.AddCache(file);
                        WriteInfo($"Sass build: {file}");
                    });
                    Console.Write(sass.Run().Trim());
                    var results = ResourceMinifier.GetFiles(f => f.FullName.Contains(".sass-output" + Path.DirectorySeparatorChar));
                    Parallel.ForEach(results, file =>
                    {
                        var min     = cleancss.Minify(File.ReadAllText(file).Replace("@charset \"UTF-8\";", ""));
                        var minFile = ResourceMinifier.GetMinimizedFileName(file);
                        File.WriteAllText(minFile, min);
                        // WriteHint($"\t=> {minFile}");
                    });
                }
                cache.SaveCache();
            }
            WriteSuccess("Sass build complete.");
            return(this);
        }
        public ProjectBuilder BuildSass()
        {
            var sass     = new SassCompiler();
            var cleancss = new CssMinifier();
            var files    = ResourceMinifier.GetFiles(file =>
                                                     file.Extension == ".scss"
                                                     );

            using (var cache = new BuildCache())
            {
                var changedFiles = files.Where(file => !cache.Contains(file)).ToArray();
                if (changedFiles.Any())
                {
                    string getOutputCacheFilename(string f)
                    {
                        return(".sass-output/" + f
                               .Replace(".scss", ".css")
                               .Replace($"src{Path.DirectorySeparatorChar}", ""));
                    }

                    changedFiles.ForEach(file => {
                        cache.AddCache(file);
                        WriteInfo($"Sass build: {file}");
                    });
                    WriteInfo(sass.Run("").Trim());
                    Parallel.ForEach(changedFiles
                                     .Where(f => !Path.GetFileName(f).StartsWith("_")),
                                     f => {
                        // var sass = new SassSingleCompiler();
                        // var css = sass.Run(File.ReadAllText(file));
                        // File.WriteAllText(getOutputCacheFilename(file), css);
                        var file    = getOutputCacheFilename(f);
                        var min     = cleancss.Minify(File.ReadAllText(file).Replace("@charset \"UTF-8\";", ""));
                        var minFile = ResourceMinifier.GetMinimizedFileName(file);
                        File.WriteAllText(minFile, min);
                        UpdateCachedMinFile(minFile);
                        // WriteHint($"\t=> {minFile}");
                    });
                    // var results = ResourceMinifier.GetFiles(f => f.FullName.Contains(".sass-output" + Path.DirectorySeparatorChar));
                    // Parallel.ForEach(results, file =>
                    // {
                    //   var min = cleancss.Minify(File.ReadAllText(file).Replace("@charset \"UTF-8\";", ""));
                    //   var minFile = ResourceMinifier.GetMinimizedFileName(file);
                    //   File.WriteAllText(minFile, min);
                    // });
                }
                cache.SaveCache();
            }
            WriteSuccess("Sass build complete.");
            return(this);
        }
        public ProjectBuilder BuildTypeScripts()
        {
            var tsc      = new BabelCompiler();
            var uglifyJs = new JavascriptMinifier();
            var files    = ResourceMinifier.GetFiles(file =>
                                                     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())
                {
                    string getOutputCacheFilename(string f)
                    {
                        return(".ts-output/" + f
                               .Replace(".ts", ".js")
                               .Replace($"src{Path.DirectorySeparatorChar}", ""));
                    }

                    changedFiles.ForEach(file => {
                        cache.AddCache(file);
                        WriteInfo($"TypeScript build: {file}");
                    });
                    WriteInfo(tsc.Run("").Trim());
                    Parallel.ForEach(changedFiles
                                     .Where(f => !f.EndsWith(".vue.ts")),
                                     f => {
                        // var tsc = new BabelSingleCompiler(file);
                        // var js = tsc.Run(File.ReadAllText(file));
                        // File.WriteAllText(getOutputCacheFilename(file), js);
                        var file = getOutputCacheFilename(f);
                        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);
                        UpdateCachedMinFile(minFile);
                        // WriteHint($"\t=> {minFile}");
                    });
                }
                cache.SaveCache();
            }
            WriteSuccess("TypeScript build complete.");
            return(this);
        }
示例#7
0
 public ProjectBuilder GetBundleFiles()
 {
     using (var cache = new BuildCache())
     {
         var extensions = new string[] {
             ".ts", ".js", ".css", ".scss", ".sass", ".vue", ".html", ".htm"
         };
         var files = ResourceMinifier.GetFiles(file =>
                                               extensions.Contains(file.Extension) &&
                                               !file.Name.EndsWith(".d.ts") &&
                                               !file.FullName.Contains("client" + Path.DirectorySeparatorChar)
                                               );
         changedBundleFiles = files.Where(file => !cache.Contains(file)).ToArray();
     }
     return(this);
 }
        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);
                                hashDict.Add(filename, hash);
                            }
                        }
                    File.WriteAllText("min/bundle.json", JsonConvert.SerializeObject(hashDict, Formatting.Indented));
                }
            }
            WriteSuccess("Bundle build complete.");
            return(this);
        }
示例#9
0
        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);
        }