From 0765401eaa93c9c6ecf9b14c7e2e2c1a253fccde Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 29 Sep 2019 19:45:13 +0800 Subject: [PATCH] wp4 compatible --- dist/index.js | 5 ----- dist/index.js.map | 1 - package.json | 7 ++----- src/index.js | 10 ++++++++-- 4 files changed, 10 insertions(+), 13 deletions(-) delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index e5f0ffc..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict';var _createClass=function(){function defineProperties(target,props){for(var i=0;i0&&arguments[0]!==undefined?arguments[0]:{};_classCallCheck(this,DeployServerWebpackPlugin);this.config=config;}_createClass(DeployServerWebpackPlugin,[{key:'apply',value:function apply(compiler){var _this=this;compiler.plugin('after-emit',function(compilation,callback){_this.validateConfig(compilation);_this.deployHandler(callback);});}},{key:'validateConfig',value:function validateConfig(compilation){var _config=this.config,receiver=_config.receiver,_config$token=_config.token,token=_config$token===undefined?'':_config$token;var mapping=this.config.mapping;if(!receiver){this.error('Missing param: receiver');}if(!mapping){this.error('Missing param: mapping');}var type=Object.prototype.toString.call(mapping);if(type==='[object Object]'){mapping=[mapping];}else if(type==='[object Array]'){// do nothing -}else{this.error('Invalid param: mapping');}var assets=compilation.assets;var avalAssets={};mapping.map(function(item,index){for(var key in assets){var asset=assets[key];var assetPath=asset.existsAt;var from=item.from;// limit "src" path within compiled files -if(assetPath.startsWith(from)){if(!avalAssets[from]){avalAssets[from]=[assetPath];continue;}avalAssets[from].push(assetPath);}}});var avalKeys=Object.keys(avalAssets);if(!avalKeys.length){this.error('No available mapping files');}this.receiver=receiver;this.mapping=mapping;this.token=token;this.avalKeys=avalKeys;this.avalAssets=avalAssets;}},{key:'deployHandler',value:function deployHandler(callback){var _this2=this;var formData={};this.avalKeys.map(function(item,index){_this2.avalAssets[item].map(function(from){// compatible with windows -var dest=(_this2.mapping[index].dest+from.replace(item,'')).replace(/\\/g,'/');_this2.deploy({file:fs.createReadStream(from),dest:dest,token:_this2.token});});});callback();}},{key:'deploy',value:function deploy(formData){request.post({url:this.receiver,formData:formData},function(err){var _ref=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{},statusCode=_ref.statusCode;var body=arguments[2];var time=new Date().toLocaleTimeString();if(!err&&statusCode===200){console.log(chalk.green('['+time+'] [success] => '+formData.dest));return;}console.log(chalk.yellow('['+time+'] [failed] => '+formData.dest+' '+body));});}},{key:'error',value:function error(err){console.log('\n'+chalk.yellow('[deploy-server-webpack-plugin] '+err+'. Deploy interrupted.'));process.exit();}}]);return DeployServerWebpackPlugin;}(); -//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 3504698..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../src/index.js"],"names":["fs","require","chalk","request","module","exports","config","compiler","plugin","compilation","callback","validateConfig","deployHandler","receiver","token","mapping","error","type","Object","prototype","toString","call","assets","avalAssets","map","item","index","key","asset","assetPath","existsAt","from","startsWith","push","avalKeys","keys","length","formData","dest","replace","deploy","file","createReadStream","post","url","err","statusCode","body","time","Date","toLocaleTimeString","console","log","green","yellow","process","exit"],"mappings":"4pBAAA,GAAMA,IAAKC,QAAQ,IAAR,CAAX,CACA,GAAMC,OAAQD,QAAQ,OAAR,CAAd,CACA,GAAME,SAAUF,QAAQ,SAAR,CAAhB,CAEAG,OAAOC,OAAP,YACC,oCAAyB,IAAbC,OAAa,2DAAJ,EAAI,iDACxB,KAAKA,MAAL,CAAcA,MAAd,CACA,CAHF,0EAKOC,QALP,CAKiB,gBACfA,SAASC,MAAT,CAAgB,YAAhB,CAA8B,SAACC,WAAD,CAAcC,QAAd,CAA2B,CACxD,MAAKC,cAAL,CAAoBF,WAApB,EACA,MAAKG,aAAL,CAAmBF,QAAnB,EACA,CAHD,EAIA,CAVF,sDAYgBD,WAZhB,CAY6B,aACM,KAAK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,uBAAvD,CAAjB,EACAU,QAAQC,IAAR,GACA,CAhGF","file":"index.js","sourcesContent":["const fs = require('fs');\r\nconst chalk = require('chalk');\r\nconst request = require('request');\r\n\r\nmodule.exports = class DeployServerWebpackPlugin {\r\n\tconstructor(config = {}) {\r\n\t\tthis.config = config;\r\n\t}\r\n\r\n\tapply(compiler) {\r\n\t\tcompiler.plugin('after-emit', (compilation, callback) => {\r\n\t\t\tthis.validateConfig(compilation);\r\n\t\t\tthis.deployHandler(callback);\r\n\t\t});\r\n\t}\r\n\r\n\tvalidateConfig(compilation) {\r\n\t\tconst { receiver, token = '' } = this.config;\r\n\t\tlet { mapping } = this.config;\r\n\r\n\t\tif (!receiver) {\r\n\t\t\tthis.error('Missing param: receiver');\r\n\t\t}\r\n\t\tif (!mapping) {\r\n\t\t\tthis.error('Missing param: mapping');\r\n\t\t}\r\n\r\n\t\tconst type = Object.prototype.toString.call(mapping);\r\n\t\tif (type === '[object Object]') {\r\n\t\t\tmapping = [mapping];\r\n\t\t} else if (type === '[object Array]') {\r\n\t\t\t// do nothing\r\n\t\t} else {\r\n\t\t\tthis.error('Invalid param: mapping');\r\n\t\t}\r\n\r\n\t\tconst assets = compilation.assets;\r\n\t\tconst avalAssets = {};\r\n\t\tmapping.map((item, index) => {\r\n\t\t\tfor (let key in assets) {\r\n\t\t\t\tconst asset = assets[key];\r\n\t\t\t\tconst assetPath = asset.existsAt;\r\n\t\t\t\tconst from = item.from;\r\n\t\t\t\t// limit \"src\" path within compiled files\r\n\t\t\t\tif (assetPath.startsWith(from)) {\r\n\t\t\t\t\tif (!avalAssets[from]) {\r\n\t\t\t\t\t\tavalAssets[from] = [assetPath];\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tavalAssets[from].push(assetPath);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\tconst avalKeys = Object.keys(avalAssets);\r\n\t\tif (!avalKeys.length) {\r\n\t\t\tthis.error('No available mapping files');\r\n\t\t}\r\n\r\n\t\tthis.receiver = receiver;\r\n\t\tthis.mapping = mapping;\r\n\t\tthis.token = token;\r\n\t\tthis.avalKeys = avalKeys;\r\n\t\tthis.avalAssets = avalAssets;\r\n\t}\r\n\r\n\tdeployHandler(callback) {\r\n\t\tconst formData = {};\r\n\t\tthis.avalKeys.map((item, index) => {\r\n\t\t\tthis.avalAssets[item].map(from => {\r\n\t\t\t\t// compatible with windows\r\n\t\t\t\tconst dest = (this.mapping[index].dest + from.replace(item, '')).replace(/\\\\/g, '/');\r\n\t\t\t\tthis.deploy({\r\n\t\t\t\t\tfile: fs.createReadStream(from),\r\n\t\t\t\t\tdest,\r\n\t\t\t\t\ttoken: this.token\r\n\t\t\t\t});\r\n\t\t\t});\r\n\t\t});\r\n\r\n\t\tcallback();\r\n\t}\r\n\r\n\tdeploy(formData) {\r\n\t\trequest.post({\r\n\t\t\turl: this.receiver,\r\n\t\t\tformData\r\n\t\t}, (err, { statusCode } = {}, body) => {\r\n\t\t\tconst time = new Date().toLocaleTimeString();\r\n\t\t\tif (!err && statusCode === 200) {\r\n\t\t\t\tconsole.log(chalk.green(`[${time}] [success] => ${formData.dest}`));\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\tconsole.log(chalk.yellow(`[${time}] [failed] => ${formData.dest} ${body}`));\r\n\t\t});\r\n\t}\r\n\r\n\terror(err) {\r\n\t\tconsole.log(`\\n${chalk.yellow('[deploy-server-webpack-plugin] ' + err + '. Deploy interrupted.')}`);\r\n\t\tprocess.exit();\r\n\t}\r\n};\r\n"]} \ No newline at end of file diff --git a/package.json b/package.json index 22fc932..11fd9a0 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,8 @@ { "name": "deploy-server-webpack-plugin", - "version": "0.0.4", + "version": "0.0.5", "description": "A plugin that deploys wepack bundles to server, and Koa as server to receive bundles", - "main": "dist/index.js", - "scripts": { - "babel": "babel ./src -d ./dist --compact --source-maps" - }, + "main": "src/index.js", "repository": { "type": "git", "url": "git+https://github.com/mingmingwon/deploy-server-webpack-plugin.git" diff --git a/src/index.js b/src/index.js index 2881146..b4b3fa9 100644 --- a/src/index.js +++ b/src/index.js @@ -8,10 +8,16 @@ module.exports = class DeployServerWebpackPlugin { } apply(compiler) { - compiler.plugin('after-emit', (compilation, callback) => { + let afterEmit = (compilation, callback) => { this.validateConfig(compilation); this.deployHandler(callback); - }); + } + + if (compiler.hooks) { + compiler.hooks.afterEmit.tapAsync('afterEmit', afterEmit); + } else { + compiler.plugin('after-emit', afterEmit); + }, } validateConfig(compilation) {