示例#1
0
        public ProjectBuilder BuildVue()
        {
            var files = ResourceMinifier.GetFiles(file =>
                                                  file.FullName.Contains($"src{Path.DirectorySeparatorChar}") &&
                                                  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()));
                        WriteHint($"\t=> {minFile}");
                    });
                }
                cache.SaveCache();
            }
            WriteSuccess("Vue build complete.");
            return(this);
        }