UNPKG

8.54 kBJavaScriptView Raw
1"use strict";exports.__esModule=!0;var tslib_1=require("tslib"),LarkLogger_1=require("@kemao/lark/Server/Log/LarkLogger/LarkLogger"),DirectoryNamedWebpackPlugin=require("directory-named-webpack-plugin"),fs=require("fs-extra"),Notifier=require("node-notifier"),Path=require("path"),webpack=require("webpack"),BrowserBase_1=require("../Core/BrowserBase"),LarkDate_1=require("../Core/LarkDate"),logger=new LarkLogger_1.LarkLogger("Task/BrowserPack"),BrowserPack=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.packageTools=null,t.versionType="none",t.outDir="./build/Browser",t.publicPath="",t.eventMap=new Map,t.version=null,t.taskName="BrowserPack",t.compiler=null,t.env={NODE_ENV:null,outputPublicPath:null},t.count=0,t.sourceDir=null,t.watch=!1,t.assetsLimit=51200,t.bootFileList=[],t.vendorPack=null,t}return tslib_1.__extends(t,e),t.prototype.setWatch=function(t){this.watch=t},t.prototype.getOutDir=function(){return this.outDir},t.prototype.getVersionOutDir=function(){return""===this.version.getVersion()?this.outDir:this.outDir+"/"+this.version.getVersion()},t.prototype.setVersionType=function(t){this.versionType=t},t.prototype.setENV=function(t){this.env=t},t.prototype.setPublicPath=function(t){this.publicPath=t,logger.log("setPublicPath",this.publicPath)},t.prototype.getPublicPath=function(){return""===this.version.getVersion()?this.publicPath:this.publicPath+"/"+this.version.getVersion()},t.prototype.setAssetsLimit=function(t){logger.log("setAssetsLimit",t),this.assetsLimit=t},t.prototype.setVendorPack=function(t){this.vendorPack=t},t.prototype.setBootFileList=function(t){this.bootFileList=t},t.prototype.setSourceDir=function(t){this.sourceDir=Path.resolve(t)},t.prototype.addEventListener=function(t,e){this.eventMap.set(t,e)},t.prototype.close=function(){logger.log("close"),null!==this.compiler&&(this.compiler.close(),this.compiler=null)},t.prototype.setOutDir=function(t){this.outDir=t},t.prototype.trigger=function(s,r){return void 0===r&&(r=null),tslib_1.__awaiter(this,void 0,void 0,function(){var e;return tslib_1.__generator(this,function(t){switch(t.label){case 0:return"function"!=typeof(e=this.eventMap.get(s))?[3,2]:[4,e(r)];case 1:t.sent(),t.label=2;case 2:return[2]}})})},t.prototype.rebuild=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){return tslib_1.__generator(this,function(t){switch(t.label){case 0:return[4,this.close()];case 1:return t.sent(),[4,this.build()];case 2:return t.sent(),[2]}})})},t.prototype.build=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var e,s,r,i,o,n,a,c,u;return tslib_1.__generator(this,function(t){switch(t.label){case 0:if(!1===fs.pathExistsSync(this.sourceDir))return[2];for(e=this.bootFileList,logger.log("packList",e),s={},r=0,i=e;r<i.length;r++)o=i[r],/.ejs/.test(o)||(/.test.ts?/.test(o)&&!1===this.isWatch()?logger.log("skip test",o):(n=o.replace(this.sourceDir,""),a=Path.dirname(n),c=Path.basename(n).replace(".bootstrap.tsx","").replace(".bootstrap.ts","").replace(".test.tsx",".test").replace(".test.ts",".test"),"/"!==a&&(a+="/"),s[u=a+c]=o,logger.log("entryPaths",u,"=>",o)));return[4,this.pack(s)];case 1:return t.sent(),[4,this.copyLogo()];case 2:return t.sent(),!1!==this.watch?[3,4]:[4,this.close()];case 3:t.sent(),t.label=4;case 4:return[2]}})})},t.prototype.isWatch=function(){return this.watch},t.prototype.copyLogo=function(){fs.ensureDirSync(this.getVersionOutDir()),fs.existsSync(this.sourceDir+"/logo.png")?fs.copyFileSync(this.sourceDir+"/logo.png",this.getVersionOutDir()+"/logo.png"):fs.existsSync(this.sourceDir+"/../../logo.png")&&fs.copyFileSync(this.sourceDir+"/../../logo.png",this.getVersionOutDir()+"/logo.png")},t.prototype.pack=function(h){return tslib_1.__awaiter(this,void 0,void 0,function(){var e,s,r,i,o,n,a,c,u,l;return tslib_1.__generator(this,function(t){switch(t.label){case 0:return 0===Object.keys(h).length?(logger.warn("NOT_FOUND"),[2]):(logger.log("PACK_RUN"),s=!(e="[hash:base64:5]"),this.isWatch()&&(e="[name]__[local]",s=!0),logger.trace("pack.localIdentName",e),r=[],i=this.isWatch()?this.getPublicPath()+"/":this.getCDN()+this.getPublicPath()+"/",logger.trace("outputPublicPath",i),o={cache:!0,mode:this.isWatch()?"development":"production",devtool:!!this.isWatch()&&"source-map",entry:h,module:{rules:r.concat([{test:/\.ts?/,use:[{loader:"ts-loader",options:{compilerOptions:{declaration:!1,sourceMap:!0}}}]},{test:/\.css$/,use:[{loader:"style-loader"},{loader:"css-loader",options:{modules:{localIdentName:e},sourceMap:s}}]},{test:/\.less$/,use:[{loader:"style-loader"},{loader:"css-loader",options:{modules:{localIdentName:e},sourceMap:s}},{loader:"less-loader",options:{sourceMap:s}}]},{test:/\.xml$/,use:["xml-loader"]},{test:/\.(png|jpg|gif|svg|eot|ttf|woff|woff2|otf)$/,use:[{loader:"url-loader",options:{name:"[hash].[ext]",outputPath:"./assets/",limit:this.assetsLimit}}]}])},name:this.taskName,output:{chunkFilename:this.isWatch()?"chunk/chunk-[name].js?chunkhash=[chunkhash]":"chunk/chunk-[name]-[chunkhash:8].js?chunkhash=[chunkhash]",filename:"[name].js",path:Path.resolve(""+this.getVersionOutDir()),publicPath:i},plugins:[new webpack.HashedModuleIdsPlugin],resolve:{extensions:[".ts",".tsx",".js",".css",".less"],modules:[Path.resolve(this.sourceDir),Path.resolve("node_modules")],plugins:[new DirectoryNamedWebpackPlugin]}},n=JSON.stringify("development"),!1===this.isWatch()&&(delete o.devtool,n=JSON.stringify("production")),this.env.NODE_ENV=n,this.env.outputPublicPath=JSON.stringify(i),a={"process.env":this.env,"process.title":JSON.stringify(this.packageTools.getPackageName()),"process.version":JSON.stringify(this.packageTools.getVersion()),"process.DEBUG":this.isWatch(),"process.buildDatetime":JSON.stringify(LarkDate_1.LarkDate.format(new Date)),"process.outputPublicPath":JSON.stringify(i)},o.plugins.push(new webpack.DefinePlugin(a)),null!==this.vendorPack&&this.vendorPack.hasVendor()&&(c=this.vendorPack.getVendorJSON(),fs.existsSync(c)&&(u=fs.readJsonSync(c),l=new webpack.DllReferencePlugin({context:Path.resolve("./build/Browser"),manifest:u}),o.plugins.push(l))),[4,this.webpack(o)]);case 1:return t.sent(),[2]}})})},t.prototype.webpack=function(s){return tslib_1.__awaiter(this,void 0,void 0,function(){var e=this;return tslib_1.__generator(this,function(t){return[2,new Promise(function(n,i){return tslib_1.__awaiter(e,void 0,void 0,function(){var o,e=this;return tslib_1.__generator(this,function(t){return o=webpack(s),this.isWatch()?this.compiler=o.watch({},function(r,i){return tslib_1.__awaiter(e,void 0,void 0,function(){var e,s;return tslib_1.__generator(this,function(t){switch(t.label){case 0:return e=i.toJson(),s=e.outputPath,s=Path.normalize(s+"/index.json"),fs.writeJSONSync(s,e),[4,this.done(r,i)];case 1:return t.sent(),n(o),[2]}})})}):o.run(function(s,r){return tslib_1.__awaiter(e,void 0,void 0,function(){var e;return tslib_1.__generator(this,function(t){switch(t.label){case 0:if(s)return[2,i(s)];t.label=1;case 1:return t.trys.push([1,3,,4]),[4,this.done(s,r)];case 2:return t.sent(),[3,4];case 3:return e=t.sent(),[2,i(e)];case 4:return n(),[2]}})})}),[2]})})})]})})},t.prototype.doneCallback=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){return tslib_1.__generator(this,function(t){return logger.trace("doneCallback"),[2]})})},t.prototype.done=function(i,o){return tslib_1.__awaiter(this,void 0,void 0,function(){var e,s,r;return tslib_1.__generator(this,function(t){switch(t.label){case 0:return this.count++,1<this.count?[4,this.trigger("buildStart",{count:this.count})]:[3,2];case 1:t.sent(),t.label=2;case 2:if(i)return logger.error("webpackSelfError",i.stack||i),i.details&&logger.error("webpackSelfError.details",i.details),this.sendMessage("webpack运行有错"+this.packageTools.getVersion()+this.count),[2];if(e=o.toJson(),s=e.errors,logger.warnIf(0<s.length,"共有错误数:",s.length),o.hasErrors()){if(s.forEach(function(t){logger.error(t)}),r=s[0],this.sendMessage("代码有错误",r,"error"),!1===this.isWatch())throw new Error("fail")}else logger.log(this.sourceDir+".done",this.count),this.sendMessage("BrowserPack:"+this.count,this.sourceDir);return[4,this.trigger("build")];case 3:return t.sent(),[4,this.doneCallback()];case 4:return t.sent(),[2]}})})},t.prototype.sendMessage=function(t,e,s){void 0===e&&(e="通知"),void 0===s&&(s="info"),logger.log("sendMessage",t,e,s);var r={message:"string"==typeof t?t.substr(0,100):JSON.stringify(t),title:e,wait:!1};Notifier.notify(r)},t}(BrowserBase_1.BrowserBase);exports.BrowserPack=BrowserPack;
\No newline at end of file