Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize decorator helper size #16129

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions Gulpfile.mjs
Expand Up @@ -35,7 +35,10 @@ import { USE_ESM, commonJS } from "$repo-utils";
const { require, __dirname: monorepoRoot } = commonJS(import.meta.url);

const defaultPackagesGlob = "./@(codemods|packages|eslint)/*";
const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`;
const defaultSourcesGlob = [
`${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`,
"!./packages/babel-helpers/src/helpers/*",
];

const babelStandalonePluginConfigGlob =
"./packages/babel-standalone/scripts/pluginConfig.json";
Expand Down Expand Up @@ -898,7 +901,7 @@ function watch() {
gulp.watch(buildTypingsWatchGlob, gulp.task("generate-type-helpers"));
gulp.watch(
[
"./packages/babel-helpers/src/helpers/*.js",
"./packages/babel-helpers/src/helpers/*",
"!./packages/babel-helpers/src/helpers/regeneratorRuntime.js",
],
gulp.task("generate-runtime-helpers")
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Expand Up @@ -267,7 +267,7 @@ module.exports = [
},
},
{
files: ["packages/babel-helpers/src/helpers/**.js"],
files: ["packages/babel-helpers/src/helpers/**.{js,ts}"],
rules: {
"no-var": "off",
"comma-dangle": "off",
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-helpers/package.json
Expand Up @@ -23,8 +23,7 @@
"@babel/generator": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"@babel/parser": "workspace:^",
"regenerator-runtime": "^0.14.0",
"terser": "^5.19.2"
"regenerator-runtime": "^0.14.0"
},
"engines": {
"node": ">=6.9.0"
Expand Down
44 changes: 34 additions & 10 deletions packages/babel-helpers/scripts/generate-helpers.js
@@ -1,7 +1,10 @@
/* eslint-disable import/no-extraneous-dependencies */
import fs from "fs";
import { join } from "path";
import { URL, fileURLToPath } from "url";
import { minify } from "terser"; // eslint-disable-line import/no-extraneous-dependencies
import { minify } from "terser";
import { transformSync } from "@babel/core";
import presetTypescript from "@babel/preset-typescript";

const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url);
const IGNORED_FILES = new Set(["package.json"]);
Expand Down Expand Up @@ -30,31 +33,52 @@ export default Object.freeze({

const [helperName] = file.split(".");

const isTs = file.endsWith(".ts");

const filePath = join(fileURLToPath(HELPERS_FOLDER), file);
if (!file.endsWith(".js")) {
if (!file.endsWith(".js") && !isTs) {
console.error("ignoring", filePath);
continue;
}

const fileContents = await fs.promises.readFile(filePath, "utf8");
const minVersionMatch = fileContents.match(
let code = await fs.promises.readFile(filePath, "utf8");
const minVersionMatch = code.match(
/^\s*\/\*\s*@minVersion\s+(?<minVersion>\S+)\s*\*\/\s*$/m
);
if (!minVersionMatch) {
throw new Error(`@minVersion number missing in ${filePath}`);
}
const { minVersion } = minVersionMatch.groups;

const source = await minify(fileContents, {
mangle: { keep_fnames: true },
// The _typeof helper has a custom directive that we must keep
compress: { directives: false },
});
if (isTs) {
code = transformSync(code, {
configFile: false,
babelrc: false,
filename: filePath,
presets: [
[
presetTypescript,
{
onlyRemoveTypeImports: true,
optimizeConstEnums: true,
},
],
],
}).code;
}

code = (
await minify(code, {
mangle: { keep_fnames: true },
// The _typeof helper has a custom directive that we must keep
compress: { directives: false },
})
).code;

output += `\
${JSON.stringify(helperName)}: helper(
${JSON.stringify(minVersion)},
${JSON.stringify(source.code)},
${JSON.stringify(code)},
),
`;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts
Expand Up @@ -39,7 +39,7 @@ export default Object.freeze({
),
applyDecs2305: helper(
"7.21.0",
'import checkInRHS from"checkInRHS";function createAddInitializerMethod(e,t){return function(r){assertNotFinished(t,"addInitializer"),assertCallable(r,"An initializer"),e.push(r)}}function assertInstanceIfPrivate(e,t){if(!e(t))throw new TypeError("Attempted to access private element on non-instance")}function memberDec(e,t,r,a,n,i,s,o,c,l,u){var f;switch(i){case 1:f="accessor";break;case 2:f="method";break;case 3:f="getter";break;case 4:f="setter";break;default:f="field"}var d,p,h={kind:f,name:o?"#"+r:r,static:s,private:o,metadata:u},v={v:!1};if(0!==i&&(h.addInitializer=createAddInitializerMethod(n,v)),o||0!==i&&2!==i)if(2===i)d=function(e){return assertInstanceIfPrivate(l,e),a.value};else{var y=0===i||1===i;(y||3===i)&&(d=o?function(e){return assertInstanceIfPrivate(l,e),a.get.call(e)}:function(e){return a.get.call(e)}),(y||4===i)&&(p=o?function(e,t){assertInstanceIfPrivate(l,e),a.set.call(e,t)}:function(e,t){a.set.call(e,t)})}else d=function(e){return e[r]},0===i&&(p=function(e,t){e[r]=t});var m=o?l.bind():function(e){return r in e};h.access=d&&p?{get:d,set:p,has:m}:d?{get:d,has:m}:{set:p,has:m};try{return e.call(t,c,h)}finally{v.v=!0}}function assertNotFinished(e,t){if(e.v)throw new Error("attempted to call "+t+" after decoration was finished")}function assertCallable(e,t){if("function"!=typeof e)throw new TypeError(t+" must be a function")}function assertValidReturnValue(e,t){var r=typeof t;if(1===e){if("object"!==r||null===t)throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");void 0!==t.get&&assertCallable(t.get,"accessor.get"),void 0!==t.set&&assertCallable(t.set,"accessor.set"),void 0!==t.init&&assertCallable(t.init,"accessor.init")}else if("function"!==r){var a;throw a=0===e?"field":5===e?"class":"method",new TypeError(a+" decorators must return a function or void 0")}}function curryThis1(e){return function(){return e(this)}}function curryThis2(e){return function(t){e(this,t)}}function applyMemberDec(e,t,r,a,n,i,s,o,c,l,u){var f,d,p,h,v,y,m=r[0];a||Array.isArray(m)||(m=[m]),o?f=0===i||1===i?{get:curryThis1(r[3]),set:curryThis2(r[4])}:3===i?{get:r[3]}:4===i?{set:r[3]}:{value:r[3]}:0!==i&&(f=Object.getOwnPropertyDescriptor(t,n)),1===i?p={get:f.get,set:f.set}:2===i?p=f.value:3===i?p=f.get:4===i&&(p=f.set);for(var g=a?2:1,b=m.length-1;b>=0;b-=g){var I;if(void 0!==(h=memberDec(m[b],a?m[b-1]:void 0,n,f,c,i,s,o,p,l,u)))assertValidReturnValue(i,h),0===i?I=h:1===i?(I=h.init,v=h.get||p.get,y=h.set||p.set,p={get:v,set:y}):p=h,void 0!==I&&(void 0===d?d=I:"function"==typeof d?d=[d,I]:d.push(I))}if(0===i||1===i){if(void 0===d)d=function(e,t){return t};else if("function"!=typeof d){var w=d;d=function(e,t){for(var r=t,a=w.length-1;a>=0;a--)r=w[a].call(e,r);return r}}else{var M=d;d=function(e,t){return M.call(e,t)}}e.push(d)}0!==i&&(1===i?(f.get=p.get,f.set=p.set):2===i?f.value=p:3===i?f.get=p:4===i&&(f.set=p),o?1===i?(e.push((function(e,t){return p.get.call(e,t)})),e.push((function(e,t){return p.set.call(e,t)}))):2===i?e.push(p):e.push((function(e,t){return p.call(e,t)})):Object.defineProperty(t,n,f))}function applyMemberDecs(e,t,r,a){for(var n,i,s,o=[],c=new Map,l=new Map,u=0;u<t.length;u++){var f=t[u];if(Array.isArray(f)){var d,p,h=f[1],v=f[2],y=f.length>3,m=16&h,g=!!(8&h),b=r;if(h&=7,g?(d=e,0!==h&&(p=i=i||[]),y&&!s&&(s=function(t){return checkInRHS(t)===e}),b=s):(d=e.prototype,0!==h&&(p=n=n||[])),0!==h&&!y){var I=g?l:c,w=I.get(v)||0;if(!0===w||3===w&&4!==h||4===w&&3!==h)throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: "+v);I.set(v,!(!w&&h>2)||h)}applyMemberDec(o,d,f,m,v,h,g,y,p,b,a)}}return pushInitializers(o,n),pushInitializers(o,i),o}function pushInitializers(e,t){t&&e.push((function(e){for(var r=0;r<t.length;r++)t[r].call(e);return e}))}function applyClassDecs(e,t,r,a){if(t.length){for(var n=[],i=e,s=e.name,o=r?2:1,c=t.length-1;c>=0;c-=o){var l={v:!1};try{var u=t[c].call(r?t[c-1]:void 0,i,{kind:"class",name:s,addInitializer:createAddInitializerMethod(n,l),metadata:a})}finally{l.v=!0}void 0!==u&&(assertValidReturnValue(5,u),i=u)}return[defineMetadata(i,a),function(){for(var e=0;e<n.length;e++)n[e].call(i)}]}}function defineMetadata(e,t){return Object.defineProperty(e,Symbol.metadata||Symbol.for("Symbol.metadata"),{configurable:!0,enumerable:!0,value:t})}export default function applyDecs2305(e,t,r,a,n,i){if(arguments.length>=6)var s=i[Symbol.metadata||Symbol.for("Symbol.metadata")];var o=Object.create(void 0===s?null:s),c=applyMemberDecs(e,t,n,o);return r.length||defineMetadata(e,o),{e:c,get c(){return applyClassDecs(e,r,a,o)}}}',
'import checkInRHS from"checkInRHS";function _bindPropCall(e,t){return function(r,a){return e[t].call(r,a)}}function createAddInitializerMethod(e,t){return function(r){if(t.v)throw new Error("attempted to call addInitializer after decoration was finished");assertCallable(r,"An initializer",!0),e.push(r)}}function memberDec(e,t,r,a,n,i,o,s,l,c,u){function assertInstanceIfPrivate(e){return function(t,r){if(!c(t))throw new TypeError("Attempted to access private element on non-instance");return e(t,r)}}var f,d,p={kind:["field","accessor","method","getter","setter","field"][i],name:s?"#"+r:r,static:o,private:s,metadata:u},v={v:!1};if(0!==i&&(p.addInitializer=createAddInitializerMethod(n,v)),s||0!==i&&2!==i)if(2===i)f=assertInstanceIfPrivate((function(){return a.value}));else{var h=0===i||1===i;(h||3===i)&&(f=_bindPropCall(a,"get"),s&&(f=assertInstanceIfPrivate(f))),(h||4===i)&&(d=_bindPropCall(a,"set"),s&&(d=assertInstanceIfPrivate(d)))}else f=function(e){return e[r]},0===i&&(d=function(e,t){e[r]=t});var m=s?c.bind():function(e){return r in e},b=p.access={has:m};f&&(b.get=f),d&&(b.set=d);try{return e.call(t,l,p)}finally{v.v=!0}}function assertCallable(e,t,r){if("function"!=typeof e&&(r||void 0!==e))throw new TypeError(t+" must be a function")}function assertValidReturnValue(e,t){var r=typeof t;if(1===e){if("object"!==r||!t)throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");assertCallable(t.get,"accessor.get"),assertCallable(t.set,"accessor.set"),assertCallable(t.init,"accessor.init")}else if("function"!==r)throw new TypeError((0===e?"field":5===e?"class":"method")+" decorators must return a function or void 0")}function applyMemberDec(e,t,r,a,n,i,o,s,l,c,u){var f,d,p,v,h=r[0],m=r[3];a||Array.isArray(h)||(h=[h]),s?f=0===i||1===i?{get:function(){return m(this)},set:function(e){r[4](this,e)}}:3===i?{get:m}:4===i?{set:m}:{value:m}:0!==i&&(f=Object.getOwnPropertyDescriptor(t,n)),1===i?p={get:f.get,set:f.set}:2===i?p=f.value:3===i?p=f.get:4===i&&(p=f.set);for(var b=a?2:1,y=h.length-1;y>=0;y-=b){var g;if(void 0!==(v=memberDec(h[y],a?h[y-1]:void 0,n,f,l,i,o,s,p,c,u)))assertValidReturnValue(i,v),0===i?g=v:1===i?(g=v.init,p={get:v.get||p.get,set:v.set||p.set}):p=v,void 0!==g&&(void 0===d?d=g:"function"==typeof d?d=[d,g]:d.push(g))}if(0===i||1===i){if(void 0===d)d=function(e,t){return t};else if("function"!=typeof d){var I=d;d=function(e,t){for(var r=t,a=I.length-1;a>=0;a--)r=I[a].call(e,r);return r}}else{var w=d;d=d.call.bind(w)}e.push(d)}0!==i&&(1===i?(f.get=p.get,f.set=p.set):2===i?f.value=p:3===i?f.get=p:4===i&&(f.set=p),s?1===i?e.push(_bindPropCall(f,"get"),_bindPropCall(f,"set")):e.push(2===i?p:Function.call.bind(p)):Object.defineProperty(t,n,f))}function applyMemberDecs(e,t,r,a){var n,i,o,s=[],l=new Map,c=new Map;function pushInitializers(e){e&&s.push((function(t){for(var r=0;r<e.length;r++)e[r].call(t);return t}))}for(var u=0;u<t.length;u++){var f=t[u];if(Array.isArray(f)){var d,p,v=f[1],h=f[2],m=f.length>3,b=16&v,y=!!(8&v),g=r;if(v&=7,y?(d=e,0!==v&&(p=i=i||[]),m&&!o&&(o=function(t){return checkInRHS(t)===e}),g=o):(d=e.prototype,0!==v&&(p=n=n||[])),0!==v&&!m){var I=y?c:l,w=I.get(h)||0;if(!0===w||3===w&&4!==v||4===w&&3!==v)throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: "+h);I.set(h,!(!w&&v>2)||v)}applyMemberDec(s,d,f,b,h,v,y,m,p,g,a)}}return pushInitializers(n),pushInitializers(i),s}function applyClassDecs(e,t,r,a){if(t.length){for(var n=[],i=e,o=e.name,s=r?2:1,l=t.length-1;l>=0;l-=s){var c={v:!1};try{var u=t[l].call(r?t[l-1]:void 0,i,{kind:"class",name:o,addInitializer:createAddInitializerMethod(n,c),metadata:a})}finally{c.v=!0}void 0!==u&&(assertValidReturnValue(5,u),i=u)}return[defineMetadata(i,a),function(){for(var e=0;e<n.length;e++)n[e].call(i)}]}}function defineMetadata(e,t){return Object.defineProperty(e,Symbol.metadata||Symbol.for("Symbol.metadata"),{configurable:!0,enumerable:!0,value:t})}export default function applyDecs2305(e,t,r,a,n,i){if(arguments.length>=6)var o=i[Symbol.metadata||Symbol.for("Symbol.metadata")];var s=Object.create(void 0===o?null:o),l=applyMemberDecs(e,t,n,s);return r.length||defineMetadata(e,s),{e:l,get c(){return applyClassDecs(e,r,a,s)}}}',
),
asyncGeneratorDelegate: helper(
"7.0.0-beta.0",
Expand Down