Skip to content

Commit

Permalink
fix(polyfill): use minified regenerator-runtime source when minifying
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Mar 9, 2021
1 parent 441d6d4 commit b255887
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {join} from "path";
import {generateRandomHash} from "../util/hash-util/hash-util";
import {unlinkSync, writeFileSync} from "fs";
import {transform} from "@swc/core";
import {REGENERATOR_SOURCE} from "../constant/regenerator-source";
import {REGENERATOR_SOURCE, REGENERATOR_SOURCE_MINIFIED} from "../constant/regenerator-source";

const swcBug1461Match = /var regeneratorRuntime\s*=\s*require\(["'`]regenerator-runtime["'`]\);/;
const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;
Expand Down Expand Up @@ -44,8 +44,8 @@ function workAroundSwcBug1227(str: string): string {
/**
* TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
*/
function workAroundSwcBug1461(str: string): string {
return str.replace(swcBug1461Match, REGENERATOR_SOURCE);
function workAroundSwcBug1461(str: string, minify = false): string {
return str.replace(swcBug1461Match, minify ? REGENERATOR_SOURCE_MINIFIED : REGENERATOR_SOURCE);
}

function stringifyPolyfillFeature(feature: IPolyfillFeature): string {
Expand Down Expand Up @@ -135,7 +135,7 @@ export async function build({paths, features, featuresRequested, ecmaVersion, co

// TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
if (swcBug1461Match.test(code)) {
code = workAroundSwcBug1461(code);
code = workAroundSwcBug1461(code, minify);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/constant/regenerator-source.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const REGENERATOR_SOURCE_MINIFIED = `var regeneratorRuntime=(()=>{var t,r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,r,e,n){var o=r&&r.prototype instanceof y?r:y,i=Object.create(o.prototype),a=new k(n||[]);return i._invoke=function(t,r,e){var n=u;return function(o,i){if(n===f)throw new Error("Generator is already running");if(n===s){if("throw"===o)throw i;return S()}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=_(a,e);if(c){if(c===p)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if(n===u)throw s,e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);f;var y=h(t,r,e);if("normal"===y.type){if(e.done?s:l,y.arg===p)continue;return{value:y.arg,done:e.done}}"throw"===y.type&&(s,e.method="throw",e.arg=y.arg)}}}(t,e,a),i}function h(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}var u="suspendedStart",l="suspendedYield",f="executing",s="completed",p={};function y(){}function v(){}function g(){}var d={};d[o]=function(){return this};var m=Object.getPrototypeOf,w=m&&m(m(O([])));w&&w!==r&&e.call(w,o)&&(d=w);var L=g.prototype=y.prototype=Object.create(d);function x(t){["next","throw","return"].forEach((function(r){t[r]=function(t){return this._invoke(r,t)}}))}function E(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===v||"GeneratorFunction"===(r.displayName||r.name))}function b(t,r){function n(o,i,a,c){var u=h(t[o],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&e.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):r.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}var o;this._invoke=function(t,e){function i(){return new r((function(r,o){n(t,e,r,o)}))}return o=o?o.then(i,i):i()}}function _(r,e){var n=r.iterator[e.method];if(n===t){if(e.delegate=null,"throw"===e.method){if(r.iterator.return&&(e.method="return",e.arg=t,_(r,e),"throw"===e.method))return p;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var o=h(n,r.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,p;var i=o.arg;return i?i.done?(e[r.resultName]=i.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,p):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,p)}function N(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function j(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function k(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(N,this),this.reset(!0)}function O(r){if(r){var n=r[o];if(n)return n.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var i=-1,a=function n(){for(;++i<r.length;)if(e.call(r,i))return n.value=r[i],n.done=!1,n;return n.value=t,n.done=!0,n};return a.next=a}}return{next:S}}function S(){return{value:t,done:!0}}v.prototype=L.constructor=g,g.constructor=v,g[a]=v.displayName="GeneratorFunction",x(b.prototype),b.prototype[i]=function(){return this},x(L),L[a]="Generator",L[o]=function(){return this},L.toString=function(){return"[object Generator]"},k.prototype={constructor:k,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(j),!r)for(var n in this)"t"===n.charAt(0)&&e.call(this,n)&&!isNaN(+n.slice(1))&&(this[n]=t)},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 o(e,o){return c.type="throw",c.arg=r,n.next=e,o&&(n.method="next",n.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var h=e.call(a,"catchLoc"),u=e.call(a,"finallyLoc");if(h&&u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(h){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,p):this.complete(a)},complete:function(t,r){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&&r&&(this.next=r),p},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),j(e),p}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;j(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:O(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),p}}})();`;
export const REGENERATOR_SOURCE = `var regeneratorRuntime = (() => {
/**
* Copyright (c) 2014-present, Facebook, Inc.
Expand Down Expand Up @@ -694,8 +695,7 @@ export const REGENERATOR_SOURCE = `var regeneratorRuntime = (() => {
return ContinueSentinel;
}
};
// Export a default namespace that plays well with Rollup
return {
wrap,
isGeneratorFunction,
Expand Down

0 comments on commit b255887

Please sign in to comment.