Skip to content

Commit

Permalink
Optimize decorator helper size (#16160)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed Dec 11, 2023
1 parent ed365d1 commit d2d54c6
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 553 deletions.
66 changes: 48 additions & 18 deletions packages/babel-helpers/scripts/generate-helpers.js
Expand Up @@ -5,6 +5,7 @@ import { URL, fileURLToPath } from "url";
import { minify } from "terser";
import { transformSync } from "@babel/core";
import presetTypescript from "@babel/preset-typescript";
import { gzipSync } from "zlib";

const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url);
const IGNORED_FILES = new Set(["package.json"]);
Expand Down Expand Up @@ -50,32 +51,61 @@ export default Object.freeze({
}
const { minVersion } = minVersionMatch.groups;

if (isTs) {
code = transformSync(code, {
configFile: false,
babelrc: false,
filename: filePath,
presets: [
[
presetTypescript,
{
onlyRemoveTypeImports: true,
optimizeConstEnums: true,
},
],
],
}).code;
}
const mangleFns = code.includes("@mangleFns");
const noMangleFns = [];

code = transformSync(code, {
configFile: false,
babelrc: false,
filename: filePath,
presets: [
[
presetTypescript,
{
onlyRemoveTypeImports: true,
optimizeConstEnums: true,
},
],
],
plugins: [
/**
* @type {import("@babel/core").PluginObj}
*/
{
visitor: {
ImportDeclaration(path) {
const source = path.node.source;
source.value = source.value
.replace(/\.ts$/, "")
.replace(/^\.\//, "");
},
FunctionDeclaration(path) {
if (
mangleFns &&
path.node.leadingComments?.find(c =>
c.value.includes("@no-mangle")
)
) {
const name = path.node.id.name;
if (name) noMangleFns.push(name);
}
},
},
},
],
}).code;
code = (
await minify(code, {
mangle: { keep_fnames: true },
mangle: {
keep_fnames: mangleFns ? new RegExp(noMangleFns.join("|")) : true,
},
// The _typeof helper has a custom directive that we must keep
compress: { directives: false },
compress: { directives: false, passes: 10 },
})
).code;

output += `\
// size: ${code.length}, gzip size: ${gzipSync(code).length}
${JSON.stringify(helperName)}: helper(
${JSON.stringify(minVersion)},
${JSON.stringify(code)},
Expand Down
36 changes: 31 additions & 5 deletions packages/babel-helpers/src/helpers-generated.ts

Large diffs are not rendered by default.

0 comments on commit d2d54c6

Please sign in to comment.