UNPKG

7.55 kBJavaScriptView Raw
1"use strict";exports.__esModule=!0;var tslib_1=require("tslib"),LarkLogger_1=require("@kemao/lark/Server/Log/LarkLogger/LarkLogger"),LarkEJSON_1=require("@kemao/lark/Server/Utils/LarkEJSON/LarkEJSON"),PackageTools_1=require("@kemao/package-tools/PackageTools/PackageTools"),FileSystemWatch_1=require("@kemao/package-tools/FileSystemWatch/FileSystemWatch"),FileSystemWatchDto_1=require("@kemao/package-tools/FileSystemWatch/FileSystemWatchDto"),LarkSequence_1=require("@kemao/package-tools/Utils/LarkSequence/LarkSequence"),cssnano=require("cssnano"),fs=require("fs-extra"),HTMLMinifier=require("html-minifier"),Less=require("less"),Path=require("path"),Typescript=require("typescript"),UglifyJS=require("uglify-js"),yargs_1=require("yargs"),logger=new LarkLogger_1.LarkLogger("Task/WeixinApp"),WeixinApp=function(){function e(){var a=this;this.rootDir="./",this.sourceDir="src/WeixinApp",this.outDir="build/WeixinApp",this.watch=!1,this.fileSystemWatch=null,this.compilerOptions=null,this.count=0,this.complieFileMap={ts:function(e,t,r){var i=Path.dirname(r);logger.log("complieFileMap.ts",{filename:t,destOutdir:i});var n=[],o=e.toString("utf8");try{o=Typescript.transpile(o,a.getCompilerOptions())}catch(e){logger.error("ts编译失败",t,e)}var s=UglifyJS.minify(o);return s.error?logger.error("压缩代码失败",t,s.error):o=s.code,n.push({content:o,extname:"js"}),n},json:function(e,t){return{content:a.minifyJSON(e.toString("utf8"),t)}},js:function(e,t){var r=UglifyJS.minify(e.toString("utf8"));return r.error?logger.error("压缩代码失败",t):e=r.code,{content:e}},ejs:function(e){return e},png:function(e){return e},jpg:function(e){return e},gif:function(e){return e},wxss:function(t){return tslib_1.__awaiter(a,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,this.minifyCSS(t)];case 1:return[2,{content:t=e.sent()}]}})})},less:function(i,n){return tslib_1.__awaiter(a,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=Path.dirname(n),[4,Less.render(i.toString(),{paths:[t]})];case 1:return r=e.sent().css,[4,this.minifyCSS(r)];case 2:return[2,{content:r=e.sent(),extname:"wxss"}]}})})},wxml:function(e,t){return{content:e=a.minifyHTML(e.toString("utf8"),t)}}}}return e.prototype.setRootDir=function(e){this.rootDir=e},e.prototype.setSourceDir=function(e){this.sourceDir=e},e.prototype.setOutDir=function(e){this.outDir=e},e.prototype.build=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var t,r,i=this;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=Path.resolve(this.rootDir+"/"+this.sourceDir),logger.log(t),!1===fs.pathExistsSync(t)?[2]:(logger.log("---------------编译任务启动--------------------"),[4,this.buildPackageJSON()]);case 1:return e.sent(),[4,this.copyPackageAssets()];case 2:return e.sent(),(r=new FileSystemWatch_1.FileSystemWatch).setRootDir(t),r.setWatch(this.watch),r.addEventListener("data",function(u){return tslib_1.__awaiter(i,void 0,void 0,function(){var t,r,i,n,o,s,a,c;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return 0<this.count?(this.count++,[4,this.buildPackageJSON()]):[3,2];case 1:e.sent(),e.label=2;case 2:if(u.mode!==FileSystemWatchDto_1.E_FILE_MODE.CHANGE&&u.mode!==FileSystemWatchDto_1.E_FILE_MODE.FOUND)return[3,8];if(logger.log("build file",u),"tsconfig.json"===Path.basename(u.filename))return[2];if(/\.src$/.test(Path.parse(u.filename).name))return logger.warn("源码文件过滤",u),[2];if(t=fs.readFileSync(u.filename),"function"!=typeof(r=this.complieFileMap[u.extname.replace(".","")]))return[3,7];i={content:t,extname:u.extname},n=Path.resolve(this.rootDir+"/"+this.outDir+u.filename.replace(Path.resolve(this.rootDir+"/"+this.sourceDir),"")),fs.ensureDirSync(Path.dirname(n)),e.label=3;case 3:return e.trys.push([3,5,,6]),[4,r(t,u.filename,n)];case 4:return i=e.sent(),[3,6];case 5:return o=e.sent(),logger.error("文件内容编译失败",{error:o,file:u}),[3,6];case 6:if(i instanceof Array)for(s=0,a=i;s<a.length;s++)c=a[s],this.writeContent(c,u,n);else this.writeContent(i,u,n);e.label=7;case 7:return[3,9];case 8:logger.warn("文件被删除",u),e.label=9;case 9:return[2]}})})}),r.addEventListener("end",function(){i.count++}),[4,r.on()];case 3:return e.sent(),this.fileSystemWatch=r,logger.log("---------------编译任务结束--------------------"),[2]}})})},e.prototype.setWatch=function(e){this.watch=e},e.prototype.abort=function(){this.fileSystemWatch.off()},e.prototype.buildPackageJSON=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var t;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,(new PackageTools_1.PackageTools).buildPackageJSON()];case 1:return e.sent(),t=fs.readJSONSync("build/package.json"),[4,this.outputVersion(t)];case 2:return e.sent(),[2]}})})},e.prototype.outputVersion=function(i){return tslib_1.__awaiter(this,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){return t=LarkEJSON_1.LarkEJSON.encode(new Date),r='\n // this is auto create, do not edit\n export const name = "'+i.name+'";\n export const version = "'+i.version+'";\n export const versionDate = '+t+";\n export const packageJSON = "+JSON.stringify(i)+";\n ",fs.outputFileSync(this.rootDir+"/"+this.outDir+"/version.js",Typescript.transpile(r)),fs.outputFileSync(this.rootDir+"/"+this.outDir+"/package.js",Typescript.transpile('\n // this is auto create, do not edit\n export const name = "'+i.name+'";\n export const version = "'+i.version+'";\n export const versionDate = '+t+";\n export const PACKAGE_JSON = "+JSON.stringify(i)+";\n export const ARGV = "+JSON.stringify(yargs_1.argv)+"\n ")),[2]})})},e.prototype.writeContent=function(e,t,r){var i=null;i=e instanceof Buffer==!1?("string"==typeof e.extname&&(r=r.replace(new RegExp(Path.extname(t.filename)+"$"),"."+e.extname)),logger.log("walkFileHander#Object",{dest:r}),e.content):e,fs.writeFileSync(r,i),logger.log("walkFileHander",t.filename+"->"+r)},e.prototype.getCompilerOptions=function(){if(null===this.compilerOptions){var e=fs.readJSONSync(this.rootDir+"/tsconfig.json");this.compilerOptions=e.compilerOptions}return this.compilerOptions},e.prototype.copyPackageAssets=function(){fs.existsSync(this.rootDir+"/logo.png")&&fs.copySync(this.rootDir+"/logo.png",this.rootDir+"/"+this.outDir+"/logo.png")},e.prototype.minifyHTML=function(e,t){try{e=HTMLMinifier.minify(e,{collapseWhitespace:!0,removeComments:!0,keepClosingSlash:!0})}catch(e){logger.error("minifyHTML#error",t,e)}return e},e.prototype.minifyJSON=function(t,r){try{var e=JSON.parse(t);return logger.log("minifyJSON#success",e,r),JSON.stringify(e)}catch(e){return logger.error("json压缩失败",r,e),t}},e.prototype.minifyCSS=function(i){return tslib_1.__awaiter(this,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,cssnano.process(i,{from:void 0})];case 1:return t=e.sent(),i=t.css,[3,3];case 2:return r=e.sent(),logger.error(r),[3,3];case 3:return[2,i]}})})},e.prototype.getPackageVersion=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var t,r,i,n;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=fs.readJSONSync(this.rootDir+"/package.json"),r=t.name+"@"+t.version,[4,LarkSequence_1.LarkSequence.getSequence(r)];case 1:return i=e.sent(),(n=t.version.split("."))[2]=i,[2,n.join(".")]}})})},e}();exports.WeixinApp=WeixinApp;
\No newline at end of file