diff options
Diffstat (limited to 'node_modules/@babel/helpers/scripts')
| -rw-r--r-- | node_modules/@babel/helpers/scripts/generate-helpers.js | 74 |
1 files changed, 64 insertions, 10 deletions
diff --git a/node_modules/@babel/helpers/scripts/generate-helpers.js b/node_modules/@babel/helpers/scripts/generate-helpers.js index e2a31886..b9d1e5f1 100644 --- a/node_modules/@babel/helpers/scripts/generate-helpers.js +++ b/node_modules/@babel/helpers/scripts/generate-helpers.js @@ -1,7 +1,11 @@ +/* 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"; +import { gzipSync } from "zlib"; const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url); const IGNORED_FILES = new Set(["package.json"]); @@ -30,14 +34,16 @@ 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) { @@ -45,16 +51,64 @@ export default Object.freeze({ } 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 }, - }); + 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: mangleFns ? new RegExp(noMangleFns.join("|")) : true, + }, + // The _typeof helper has a custom directive that we must keep + compress: { directives: false, passes: 10 }, + }) + ).code; output += `\ + // size: ${code.length}, gzip size: ${gzipSync(code).length} ${JSON.stringify(helperName)}: helper( ${JSON.stringify(minVersion)}, - ${JSON.stringify(source.code)}, + ${JSON.stringify(code)}, ), `; } |
