UNPKG

@_gz/upload-sourcemap

Version:

webpack收集sourcemap

2 lines (1 loc) 12.7 kB
function unwrapExports(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function createCommonjsModule(t,e){return t(e={exports:{}},e.exports),e.exports}var asyncToGenerator=createCommonjsModule(function(t){function u(t,e,r,n,o,a,i){try{var c=t[a](i),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}t.exports=function(c){return function(){var t=this,i=arguments;return new Promise(function(e,r){var n=c.apply(t,i);function o(t){u(n,e,r,o,a,"next",t)}function a(t){u(n,e,r,o,a,"throw",t)}o(void 0)})}},t.exports.__esModule=!0,t.exports.default=t.exports}),_asyncToGenerator=unwrapExports(asyncToGenerator),classCallCheck=createCommonjsModule(function(t){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},t.exports.__esModule=!0,t.exports.default=t.exports}),_classCallCheck=unwrapExports(classCallCheck),createClass=createCommonjsModule(function(t){function n(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}t.exports=function(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t},t.exports.__esModule=!0,t.exports.default=t.exports}),_createClass=unwrapExports(createClass),defineProperty=createCommonjsModule(function(t){t.exports=function(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},t.exports.__esModule=!0,t.exports.default=t.exports}),_defineProperty=unwrapExports(defineProperty),runtime_1=createCommonjsModule(function(e){e=function(i){var u,t=Object.prototype,s=t.hasOwnProperty,e="function"==typeof Symbol?Symbol:{},o=e.iterator||"@@iterator",r=e.asyncIterator||"@@asyncIterator",n=e.toStringTag||"@@toStringTag";function a(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{a({},"")}catch(t){a=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var o,a,i,c,e=e&&e.prototype instanceof g?e:g,e=Object.create(e.prototype),n=new L(n||[]);return e._invoke=(o=t,a=r,i=n,c=f,function(t,e){if(c===h)throw new Error("Generator is already running");if(c===y){if("throw"===t)throw e;return E()}for(i.method=t,i.arg=e;;){var r=i.delegate;if(r){var n=function t(e,r){var n=e.iterator[r.method];if(n===u){if(r.delegate=null,"throw"===r.method){if(e.iterator.return&&(r.method="return",r.arg=u,t(e,r),"throw"===r.method))return d;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return d}var n=l(n,e.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,d;n=n.arg;if(!n)return r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,d;{if(!n.done)return n;r[e.resultName]=n.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=u)}r.delegate=null;return d}(r,i);if(n){if(n===d)continue;return n}}if("next"===i.method)i.sent=i._sent=i.arg;else if("throw"===i.method){if(c===f)throw c=y,i.arg;i.dispatchException(i.arg)}else"return"===i.method&&i.abrupt("return",i.arg);c=h;n=l(o,a,i);if("normal"===n.type){if(c=i.done?y:p,n.arg!==d)return{value:n.arg,done:i.done}}else"throw"===n.type&&(c=y,i.method="throw",i.arg=n.arg)}}),e}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}i.wrap=c;var f="suspendedStart",p="suspendedYield",h="executing",y="completed",d={};function g(){}function m(){}function v(){}var w={};a(w,o,function(){return this});e=Object.getPrototypeOf,e=e&&e(e(k([])));e&&e!==t&&s.call(e,o)&&(w=e);var b=v.prototype=g.prototype=Object.create(w);function x(t){["next","throw","return"].forEach(function(e){a(t,e,function(t){return this._invoke(e,t)})})}function _(i,c){var e;this._invoke=function(r,n){function t(){return new c(function(t,e){!function e(t,r,n,o){t=l(i[t],i,r);if("throw"!==t.type){var a=t.arg;return(r=a.value)&&"object"==typeof r&&s.call(r,"__await")?c.resolve(r.__await).then(function(t){e("next",t,n,o)},function(t){e("throw",t,n,o)}):c.resolve(r).then(function(t){a.value=t,n(a)},function(t){return e("throw",t,n,o)})}o(t.arg)}(r,n,t,e)})}return e=e?e.then(t,t):t()}}function j(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function O(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(j,this),this.reset(!0)}function k(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){function r(){for(;++n<t.length;)if(s.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=u,r.done=!0,r}var n=-1;return r.next=r}}return{next:E}}function E(){return{value:u,done:!0}}return a(b,"constructor",m.prototype=v),a(v,"constructor",m),m.displayName=a(v,n,"GeneratorFunction"),i.isGeneratorFunction=function(t){t="function"==typeof t&&t.constructor;return!!t&&(t===m||"GeneratorFunction"===(t.displayName||t.name))},i.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,a(t,n,"GeneratorFunction")),t.prototype=Object.create(b),t},i.awrap=function(t){return{__await:t}},x(_.prototype),a(_.prototype,r,function(){return this}),i.AsyncIterator=_,i.async=function(t,e,r,n,o){void 0===o&&(o=Promise);var a=new _(c(t,e,r,n),o);return i.isGeneratorFunction(e)?a:a.next().then(function(t){return t.done?t.value:a.next()})},x(b),a(b,n,"Generator"),a(b,o,function(){return this}),a(b,"toString",function(){return"[object Generator]"}),i.keys=function(r){var t,n=[];for(t in r)n.push(t);return n.reverse(),function t(){for(;n.length;){var e=n.pop();if(e in r)return t.value=e,t.done=!1,t}return t.done=!0,t}},i.values=k,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=u,this.done=!1,this.delegate=null,this.method="next",this.arg=u,this.tryEntries.forEach(O),!t)for(var e in this)"t"===e.charAt(0)&&s.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=u)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var n=this;function t(t,e){return a.type="throw",a.arg=r,n.next=t,e&&(n.method="next",n.arg=u),!!e}for(var e=this.tryEntries.length-1;0<=e;--e){var o=this.tryEntries[e],a=o.completion;if("root"===o.tryLoc)return t("end");if(o.tryLoc<=this.prev){var i=s.call(o,"catchLoc"),c=s.call(o,"finallyLoc");if(i&&c){if(this.prev<o.catchLoc)return t(o.catchLoc,!0);if(this.prev<o.finallyLoc)return t(o.finallyLoc)}else if(i){if(this.prev<o.catchLoc)return t(o.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return t(o.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;0<=r;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&s.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}var a=(o=o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc?null:o)?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.finallyLoc,d):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),d},finish:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),O(r),d}},catch:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n,o=r.completion;return"throw"===o.type&&(n=o.arg,O(r)),n}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:k(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=u),d}},i}(e.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}),regenerator=runtime_1;function _createForOfIteratorHelper(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){r&&(t=r);function n(){}var o=0;return{s:n,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:n}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,c=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return i=t.done,t},e:function(t){c=!0,a=t},f:function(){try{i||null==r.return||r.return()}finally{if(c)throw a}}}}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Map"===(r="Object"===r&&t.constructor?t.constructor.name:r)||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function ownKeys(e,t){var r,n=Object.keys(e);return Object.getOwnPropertySymbols&&(r=Object.getOwnPropertySymbols(e),t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)),n}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach(function(t){_defineProperty(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}var FormData=require("form-data"),axios=require("axios"),getLogger=require("webpack-log"),execSync=require("child_process").execSync,fs=require("fs"),log=getLogger({name:"@_gz/upload-sourcemap"}),commit=execSync("git show -s --format=%H").toString().trim(),packageJsonData=JSON.parse(fs.readFileSync("package.json","utf8")),uploadSourceMap=function(){function e(){var a=this,t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,e),_defineProperty(this,"getFiles",function(t,e){try{e=e||[];var r,n=fs.readdirSync(t);for(r in n){var o=t+"/"+n[r];fs.statSync(o).isDirectory()?a.getFiles(o,e):e.push(o)}return e.filter(function(t){return/\.js\.map$/i.test(t)})}catch(t){log.warn("error",t)}}),_defineProperty(this,"uploadFile",function(){var e=_asyncToGenerator(regenerator.mark(function t(e){var r,n;return regenerator.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return r=fs.createReadStream(e),(n=new FormData).append("file",r),n.append("commitHash",a.gitCommitHash),n.append("projectName",packageJsonData.name),t.next=7,axios.default({url:a.options.url,method:"post",headers:_objectSpread({},n.getHeaders()),timeout:1e4,data:n,maxContentLength:1/0,maxBodyLength:1/0}).then(function(t){log.warn("uploadResponse------\x3esourceMap上传成功")}).catch(function(t){log.warn("uploadError---------\x3esourceMap上传失败",t)});case 7:case"end":return t.stop()}},t)}));return function(t){return e.apply(this,arguments)}}()),_defineProperty(this,"deleteSourceMap",function(t){var e,r=_createForOfIteratorHelper(t);try{for(r.s();!(e=r.n()).done;){var n=e.value;fs.unlinkSync(n)}}catch(t){r.e(t)}finally{r.f()}}),this.options=t,this.gitCommitHash=commit}return _createClass(e,[{key:"apply",value:function(t){var i=this,e=t.hooks,c=t.options;e.afterEmit.tapAsync("done",function(){var r=_asyncToGenerator(regenerator.mark(function t(e,r){var n,o,a;return regenerator.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(i.options.url){t.next=4;break}return log.warn("url参数未配置"),r(),t.abrupt("return");case 4:if(packageJsonData.name){t.next=8;break}return log.warn("package.json未配置name"),r(),t.abrupt("return");case 8:if((n=i.getFiles(c.output.path))&&Array.isArray(n)&&0===n.length)return r(),t.abrupt("return",log.warn("@_gz/upload-sourcemap: please set devtool to source-map"));t.next=12;break;case 12:o=_createForOfIteratorHelper(n),t.prev=13,o.s();case 15:if((a=o.n()).done){t.next=21;break}return a=a.value,t.next=19,i.uploadFile(a);case 19:t.next=15;break;case 21:t.next=26;break;case 23:t.prev=23,t.t0=t.catch(13),o.e(t.t0);case 26:return t.prev=26,o.f(),t.finish(26);case 29:i.options.remove&&i.deleteSourceMap(n),r();case 31:case"end":return t.stop()}},t,null,[[13,23,26,29]])}));return function(t,e){return r.apply(this,arguments)}}())}}]),e}();module.exports=uploadSourceMap;