| 1 |
import path from "path"; |
| 2 |
import fs from "fs"; |
| 3 |
import { cpus } from "os"; |
| 4 |
import { createRequire } from "module"; |
| 5 |
import { fileURLToPath } from "url"; |
| 6 |
import plumber from "gulp-plumber"; |
| 7 |
import through from "through2"; |
| 8 |
import chalk from "chalk"; |
| 9 |
import filter from "gulp-filter"; |
| 10 |
import gulp from "gulp"; |
| 11 |
import { rollup } from "rollup"; |
| 12 |
import { babel as rollupBabel } from "@rollup/plugin-babel"; |
| 13 |
import rollupCommonJs from "@rollup/plugin-commonjs"; |
| 14 |
import rollupJson from "@rollup/plugin-json"; |
| 15 |
import rollupPolyfillNode from "rollup-plugin-polyfill-node"; |
| 16 |
import rollupNodeResolve from "@rollup/plugin-node-resolve"; |
| 17 |
import rollupReplace from "@rollup/plugin-replace"; |
| 18 |
import { terser as rollupTerser } from "rollup-plugin-terser"; |
| 19 |
import rollupDts from "rollup-plugin-dts"; |
| 20 |
import { Worker as JestWorker } from "jest-worker"; |
| •21 |
import { glob } from "glob"; |
| 22 |
import { resolve as importMetaResolve } from "import-meta-resolve"; |
| 23 |
|
| 24 |
import rollupBabelSource from "./scripts/rollup-plugin-babel-source.js"; |
| 25 |
import rollupStandaloneInternals from "./scripts/rollup-plugin-standalone-internals.js"; |
| 26 |
import formatCode from "./scripts/utils/formatCode.js"; |
| 27 |
import { log } from "./scripts/utils/logger.cjs"; |
| 28 |
|
| 29 |
let USE_ESM = false; |
| 30 |
try { |
| 31 |
const type = fs |
| 32 |
.readFileSync(new URL(".module-type", import.meta.url), "utf-8") |
| 33 |
.trim(); |
| 34 |
USE_ESM = type === "module"; |
| 35 |
} catch {} |
| 36 |
|
| 37 |
const require = createRequire(import.meta.url); |
| 38 |
const monorepoRoot = path.dirname(fileURLToPath(import.meta.url)); |
| 39 |
|
| 40 |
const defaultPackagesGlob = "./@(codemods|packages|eslint)/*"; |
| 41 |
const defaultSourcesGlob = `${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts}`; |
| 42 |
|
| 43 |
const babelStandalonePluginConfigGlob = |
| 44 |
"./packages/babel-standalone/scripts/pluginConfig.json"; |
| 45 |
|
| 46 |
const buildTypingsWatchGlob = [ |
| 47 |
"./packages/babel-types/lib/definitions/**/*.js", |
| 48 |
"./packages/babel-types/scripts/generators/*.js", |
| 49 |
]; |
| 50 |
|
| 51 |
|
| 52 |
function bool(value) { |
| 53 |
return value && value !== "false" && value !== "0"; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
function mapSrcToLib(srcPath) { |
| 71 |
const parts = srcPath.replace(/(?<!\.d)\.ts$/, ".js").split("/"); |
| 72 |
parts[2] = "lib"; |
| 73 |
return parts.join("/"); |
| 74 |
} |
| 75 |
|
| 76 |
function mapToDts(packageName) { |
| 77 |
return packageName.replace( |
| 78 |
/(?<=\\|\/|^)(packages|eslint|codemods)(?=\\|\/)/, |
| 79 |
"dts/$1" |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
function getIndexFromPackage(name) { |
| 84 |
try { |
| 85 |
fs.statSync(`./${name}/src/index.ts`); |
| 86 |
return `${name}/src/index.ts`; |
| 87 |
} catch { |
| 88 |
return `${name}/src/index.js`; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
function errorsLogger() { |
| 93 |
return plumber({ |
| 94 |
errorHandler(err) { |
| 95 |
log(err.stack); |
| 96 |
}, |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
function rename(fn) { |
| 101 |
return through.obj(function (file, enc, callback) { |
| 102 |
file.path = fn(file); |
| 103 |
callback(null, file); |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
function generateHelpers(generator, dest, filename, message) { |
| 114 |
const stream = gulp |
| 115 |
.src(".", { base: monorepoRoot }) |
| 116 |
.pipe(errorsLogger()) |
| 117 |
.pipe( |
| 118 |
through.obj(async (file, enc, callback) => { |
| 119 |
const { default: generateCode } = await import(generator); |
| 120 |
|
| 121 |
file.path = filename; |
| 122 |
file.contents = Buffer.from( |
| 123 |
formatCode(await generateCode(filename), dest + file.path) |
| 124 |
); |
| 125 |
log(`${chalk.green("✔")} Generated ${message}`); |
| 126 |
callback(null, file); |
| 127 |
}) |
| 128 |
) |
| 129 |
.pipe(gulp.dest(dest, { mode: 0o644 })); |
| 130 |
|
| 131 |
return finish(stream); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
async function generateTypeHelpers(helperKind, filename = "index.ts") { |
| 141 |
return generateHelpers( |
| 142 |
`./packages/babel-types/scripts/generators/${helperKind}.js`, |
| 143 |
`./packages/babel-types/src/${helperKind}/generated/`, |
| 144 |
filename, |
| 145 |
`@babel/types -> ${helperKind}` |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
function generateTraverseHelpers(helperKind) { |
| 155 |
return generateHelpers( |
| 156 |
`./packages/babel-traverse/scripts/generators/${helperKind}.js`, |
| 157 |
`./packages/babel-traverse/src/path/generated/`, |
| 158 |
`${helperKind}.d.ts`, |
| 159 |
`@babel/traverse -> ${helperKind}` |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
async function generateRuntimeHelpers() { |
| 164 |
await generateHelpers( |
| 165 |
`./packages/babel-helpers/scripts/generate-regenerator-runtime.js`, |
| 166 |
`./packages/babel-helpers/src/helpers`, |
| 167 |
"regeneratorRuntime.js", |
| 168 |
"@babel/helpers -> regeneratorRuntime" |
| 169 |
); |
| 170 |
|
| 171 |
return generateHelpers( |
| 172 |
`./packages/babel-helpers/scripts/generate-helpers.js`, |
| 173 |
`./packages/babel-helpers/src/`, |
| 174 |
"helpers-generated.ts", |
| 175 |
"@babel/helpers" |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
const kebabToCamel = str => str.replace(/-[a-z]/g, c => c[1].toUpperCase()); |
| 180 |
|
| 181 |
function generateStandalone() { |
| 182 |
const dest = "./packages/babel-standalone/src/generated/"; |
| 183 |
return gulp |
| 184 |
.src(babelStandalonePluginConfigGlob, { base: monorepoRoot }) |
| 185 |
.pipe( |
| 186 |
through.obj((file, enc, callback) => { |
| 187 |
log("Generating @babel/standalone files"); |
| 188 |
const pluginConfig = JSON.parse(file.contents); |
| 189 |
let imports = `import makeNoopPlugin from "../make-noop-plugin";`; |
| 190 |
let exportDecls = ""; |
| 191 |
let exportsList = ""; |
| 192 |
let allList = ""; |
| 193 |
|
| 194 |
for (const plugin of pluginConfig.noopPlugins) { |
| 195 |
const camelPlugin = kebabToCamel(plugin); |
| 196 |
exportDecls += `${camelPlugin} = makeNoopPlugin(),`; |
| 197 |
allList += `"${plugin}": ${camelPlugin},`; |
| 198 |
} |
| 199 |
|
| 200 |
for (const plugin of pluginConfig.externalPlugins) { |
| 201 |
const camelPlugin = kebabToCamel(plugin); |
| 202 |
imports += `import ${camelPlugin} from "@babel/plugin-${plugin}";`; |
| 203 |
exportsList += `${camelPlugin},`; |
| 204 |
allList += `"${plugin}": ${camelPlugin},`; |
| 205 |
} |
| 206 |
|
| 207 |
const fileContents = `/* |
| 208 |
* This file is auto-generated! Do not modify it directly. |
| 209 |
* To re-generate run 'yarn gulp generate-standalone' |
| 210 |
*/ |
| 211 |
${imports} |
| 212 |
export const ${exportDecls.slice(0, -1)}; |
| 213 |
export {${exportsList}}; |
| 214 |
export const all: { [k: string]: any } = {${allList}};`; |
| 215 |
file.path = "plugins.ts"; |
| 216 |
file.contents = Buffer.from(formatCode(fileContents, dest)); |
| 217 |
callback(null, file); |
| 218 |
}) |
| 219 |
) |
| 220 |
.pipe(gulp.dest(dest)); |
| 221 |
} |
| 222 |
|
| 223 |
function finish(stream) { |
| 224 |
return new Promise((resolve, reject) => { |
| 225 |
stream.on("end", resolve); |
| 226 |
stream.on("finish", resolve); |
| 227 |
stream.on("error", reject); |
| 228 |
}); |
| 229 |
} |
| 230 |
|
| 231 |
function getFiles(glob, { include, exclude }) { |
| 232 |
let stream = gulp.src(glob, { base: monorepoRoot }); |
| 233 |
|
| 234 |
if (exclude) { |
| 235 |
const filters = exclude.map(p => `!**/${p}/**`); |
| 236 |
filters.unshift("**"); |
| 237 |
stream = stream.pipe(filter(filters)); |
| 238 |
} |
| 239 |
if (include) { |
| 240 |
const filters = include.map(p => `**/${p}/**`); |
| 241 |
stream = stream.pipe(filter(filters)); |
| 242 |
} |
| 243 |
|
| 244 |
return stream; |
| 245 |
} |
| 246 |
|
| 247 |
function createWorker(useWorker) { |
| 248 |
const numWorkers = Math.ceil(Math.max(cpus().length, 1) / 2) - 1; |
| 249 |
if (numWorkers === 0 || !useWorker) { |
| 250 |
return require("./babel-worker.cjs"); |
| 251 |
} |
| 252 |
const worker = new JestWorker(require.resolve("./babel-worker.cjs"), { |
| 253 |
numWorkers, |
| 254 |
exposedMethods: ["transform"], |
| 255 |
}); |
| 256 |
worker.getStdout().pipe(process.stdout); |
| 257 |
worker.getStderr().pipe(process.stderr); |
| 258 |
return worker; |
| 259 |
} |
| 260 |
|
| 261 |
async function buildBabel(useWorker, ignore = []) { |
| 262 |
const worker = createWorker(useWorker); |
| •263 |
const files = await glob( |
| 264 |
defaultSourcesGlob, |
| 265 |
{ |
| 266 |
ignore: ignore.map(p => `./${p.src}/**`), |
| •267 |
dotRelative: true |
| 268 |
}, |
| 269 |
); |
| 270 |
|
| 271 |
const promises = []; |
| 272 |
for (const file of files) { |
| 273 |
|
| 274 |
const dest = "./" + mapSrcToLib(file.slice(2)); |
| 275 |
promises.push( |
| 276 |
worker.transform(file, dest, { |
| 277 |
sourceMaps: !file.endsWith(".d.ts"), |
| 278 |
}) |
| 279 |
); |
| 280 |
} |
| 281 |
return Promise.allSettled(promises) |
| 282 |
.then(results => { |
| 283 |
results.forEach(result => { |
| 284 |
if (result.status == "rejected") { |
| 285 |
if (process.env.WATCH_SKIP_BUILD) { |
| 286 |
console.error(result.reason); |
| 287 |
} else { |
| 288 |
throw result.reason; |
| 289 |
} |
| 290 |
} |
| 291 |
}); |
| 292 |
}) |
| 293 |
.finally(() => { |
| 294 |
if (worker.end !== undefined) { |
| 295 |
worker.end(); |
| 296 |
} |
| 297 |
}); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
function resolveChain(baseUrl, ...packages) { |
| 304 |
const require = createRequire(baseUrl); |
| 305 |
|
| 306 |
return packages.reduce( |
| 307 |
(base, pkg) => |
| 308 |
path.dirname(require.resolve(pkg + "/package.json", { paths: [base] })), |
| 309 |
path.dirname(fileURLToPath(baseUrl)) |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
let versionSuffix = ""; |
| 316 |
if (process.env.CIRCLE_PR_NUMBER) { |
| 317 |
versionSuffix = "+pr." + process.env.CIRCLE_PR_NUMBER; |
| 318 |
} |
| 319 |
|
| 320 |
const babelVersion = |
| 321 |
require("./packages/babel-core/package.json").version + versionSuffix; |
| 322 |
function buildRollup(packages, buildStandalone) { |
| 323 |
const sourcemap = process.env.NODE_ENV === "production"; |
| 324 |
return Promise.all( |
| 325 |
packages.map( |
| 326 |
async ({ |
| 327 |
src, |
| 328 |
format, |
| 329 |
input, |
| 330 |
dest, |
| 331 |
name, |
| 332 |
filename, |
| 333 |
envName = "rollup", |
| 334 |
}) => { |
| 335 |
const pkgJSON = require("./" + src + "/package.json"); |
| 336 |
const version = pkgJSON.version + versionSuffix; |
| 337 |
const { dependencies = {}, peerDependencies = {} } = pkgJSON; |
| 338 |
const external = [ |
| 339 |
...Object.keys(dependencies), |
| 340 |
...Object.keys(peerDependencies), |
| 341 |
|
| 342 |
/@babel\/compat-data\/.*/, |
| 343 |
|
| 344 |
|
| 345 |
/babel-plugin-dynamic-import-node\/utils/, |
| 346 |
|
| 347 |
/@babel\/preset-modules\/.*/, |
| 348 |
]; |
| 349 |
|
| 350 |
log(`Compiling '${chalk.cyan(input)}' with rollup ...`); |
| 351 |
const bundle = await rollup({ |
| 352 |
input, |
| 353 |
external: buildStandalone ? [] : external, |
| 354 |
|
| 355 |
preserveSymlinks: true, |
| 356 |
onwarn(warning, warn) { |
| 357 |
switch (warning.code) { |
| •358 |
case "THIS_IS_UNDEFINED": |
| 359 |
case "CIRCULAR_DEPENDENCY": |
| 360 |
case "SOURCEMAP_ERROR": |
| 361 |
return; |
| 362 |
case "UNUSED_EXTERNAL_IMPORT": |
| 363 |
warn(warning); |
| 364 |
return; |
| 365 |
case "MISSING_EXPORT": |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if ( |
| 370 |
warning.exporter === "packages/babel-core/src/index.ts" && |
| 371 |
warning.missing === "default" && |
| 372 |
[ |
| 373 |
"@babel/helper-define-polyfill-provider", |
| 374 |
"babel-plugin-polyfill-corejs2", |
| 375 |
"babel-plugin-polyfill-corejs3", |
| 376 |
"babel-plugin-polyfill-regenerator", |
| 377 |
].some(pkg => warning.importer.includes(pkg)) |
| 378 |
) { |
| 379 |
return; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
console.warn(warning); |
| 386 |
|
| 387 |
|
| •388 |
|
| 389 |
}, |
| 390 |
plugins: [ |
| 391 |
buildStandalone && rollupStandaloneInternals(), |
| 392 |
rollupBabelSource(), |
| 393 |
rollupReplace({ |
| 394 |
preventAssignment: true, |
| 395 |
values: { |
| 396 |
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV), |
| 397 |
BABEL_VERSION: JSON.stringify(babelVersion), |
| 398 |
VERSION: JSON.stringify(version), |
| 399 |
}, |
| 400 |
}), |
| 401 |
rollupCommonJs({ |
| 402 |
include: [ |
| 403 |
|
| 404 |
buildStandalone ? /node_modules/ : "./node_modules/*/*.js", |
| 405 |
"packages/babel-runtime/regenerator/**", |
| 406 |
"packages/babel-runtime/helpers/*.js", |
| 407 |
"packages/babel-preset-env/data/*.js", |
| 408 |
|
| 409 |
"packages/babel-compat-data/*.js", |
| 410 |
"packages/*/src/**/*.cjs", |
| •411 |
/nodejs/, |
| 412 |
"../**/nodejs/**", |
| 413 |
"/usr/share/**", |
| 414 |
"/usr/lib/**", |
| 415 |
"debian/**", |
| 416 |
], |
| 417 |
dynamicRequireTargets: [ |
| 418 |
|
| 419 |
resolveChain( |
| 420 |
import.meta.url, |
| 421 |
"./packages/babel-helper-create-regexp-features-plugin", |
| 422 |
"regexpu-core", |
| 423 |
"regenerate-unicode-properties" |
| 424 |
) + "/**/*.js", |
| 425 |
], |
| 426 |
|
| 427 |
ignoreDynamicRequires: false, |
| 428 |
|
| 429 |
defaultIsModuleExports: true, |
| 430 |
}), |
| 431 |
rollupBabel({ |
| 432 |
envName, |
| 433 |
babelrc: false, |
| 434 |
babelHelpers: "bundled", |
| 435 |
extends: "./babel.config.js", |
| 436 |
extensions: [".ts", ".js", ".mjs", ".cjs"], |
| 437 |
ignore: ["packages/babel-runtime/helpers/*.js"], |
| 438 |
}), |
| 439 |
rollupNodeResolve({ |
| 440 |
extensions: [".ts", ".js", ".mjs", ".cjs", ".json"], |
| 441 |
browser: buildStandalone, |
| 442 |
exportConditions: buildStandalone ? ["browser"] : [], |
| 443 |
|
| 444 |
|
| 445 |
preferBuiltins: !buildStandalone, |
| 446 |
}), |
| 447 |
rollupJson(), |
| 448 |
buildStandalone && |
| 449 |
rollupPolyfillNode({ |
| 450 |
sourceMap: sourcemap, |
| 451 |
include: "**/*.{js,mjs,cjs,ts}", |
| 452 |
}), |
| 453 |
].filter(Boolean), |
| 454 |
acorn: { |
| 455 |
|
| 456 |
allowHashBang: true, |
| 457 |
}, |
| 458 |
}); |
| 459 |
|
| 460 |
const outputFile = path.join(src, dest, filename || "index.js"); |
| 461 |
await bundle.write({ |
| 462 |
file: outputFile, |
| 463 |
format, |
| 464 |
name, |
| 465 |
sourcemap: sourcemap, |
| 466 |
exports: "named", |
| 467 |
interop(id) { |
| 468 |
|
| 469 |
|
| 470 |
|
| •471 |
if (!id.startsWith("@babel/")) return 'auto'; |
| 472 |
|
| 473 |
|
| 474 |
if (id.includes("plugin-syntax")) { |
| 475 |
const srcPath = new URL( |
| 476 |
"./packages/" + id.replace("@babel/", "babel-"), |
| 477 |
import.meta.url |
| 478 |
); |
| •479 |
if (!fs.existsSync(srcPath)) return 'auto'; |
| 480 |
} |
| 481 |
|
| 482 |
if (id.includes("@babel/preset-modules")) { |
| •483 |
return 'auto'; |
| 484 |
} |
| 485 |
|
| •486 |
return 'auto'; |
| 487 |
}, |
| 488 |
}); |
| 489 |
|
| 490 |
|
| 491 |
if (!buildStandalone) { |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
if (!process.env.IS_PUBLISH) { |
| 496 |
log( |
| 497 |
chalk.yellow( |
| 498 |
`Skipped minification of '${chalk.cyan( |
| 499 |
outputFile |
| 500 |
)}' because not publishing` |
| 501 |
) |
| 502 |
); |
| 503 |
return undefined; |
| 504 |
} |
| 505 |
log(`Minifying '${chalk.cyan(outputFile)}'...`); |
| 506 |
|
| 507 |
await bundle.write({ |
| 508 |
file: outputFile.replace(/\.js$/, ".min.js"), |
| 509 |
format, |
| 510 |
name, |
| 511 |
sourcemap: sourcemap, |
| 512 |
exports: "named", |
| 513 |
plugins: [ |
| 514 |
rollupTerser({ |
| 515 |
|
| 516 |
output: { |
| 517 |
ascii_only: true, |
| 518 |
}, |
| 519 |
numWorkers: process.env.CIRCLECI ? 1 : undefined, |
| 520 |
}), |
| 521 |
], |
| 522 |
}); |
| 523 |
} |
| 524 |
) |
| 525 |
); |
| 526 |
} |
| 527 |
|
| 528 |
function buildRollupDts(packages) { |
| 529 |
async function build(input, output, banner) { |
| 530 |
log(`Bundling '${chalk.cyan(output)}' with rollup ...`); |
| 531 |
|
| 532 |
const bundle = await rollup({ |
| 533 |
input, |
| 534 |
plugins: [rollupDts()], |
| 535 |
}); |
| 536 |
|
| 537 |
await bundle.write({ |
| 538 |
file: output, |
| 539 |
format: "es", |
| 540 |
banner, |
| 541 |
}); |
| 542 |
} |
| 543 |
|
| 544 |
const tasks = packages.map(async packageName => { |
| 545 |
const input = `${mapToDts(packageName)}/src/index.d.ts`; |
| 546 |
const output = `${packageName}/lib/index.d.ts`; |
| 547 |
|
| 548 |
await build(input, output); |
| 549 |
}); |
| 550 |
|
| 551 |
tasks.push( |
| 552 |
build( |
| 553 |
"packages/babel-parser/typings/babel-parser.source.d.ts", |
| 554 |
"packages/babel-parser/typings/babel-parser.d.ts", |
| 555 |
"// This file is auto-generated! Do not modify it directly.\n/* eslint-disable import/no-extraneous-dependencies, @typescript-eslint/consistent-type-imports, prettier/prettier */" |
| 556 |
) |
| 557 |
); |
| 558 |
|
| 559 |
return Promise.all(tasks); |
| 560 |
} |
| 561 |
|
| 562 |
function copyDts(packages) { |
| 563 |
return getFiles(`${defaultPackagesGlob}/src/**/*.d.ts`, { include: packages }) |
| 564 |
.pipe(rename(file => path.resolve(file.base, mapToDts(file.relative)))) |
| 565 |
.pipe(gulp.dest(monorepoRoot)); |
| 566 |
} |
| 567 |
|
| 568 |
function* libBundlesIterator() { |
| 569 |
const noBundle = new Set([ |
| 570 |
|
| 571 |
"babel-helper-fixtures", |
| 572 |
|
| 573 |
"babel-standalone", |
| 574 |
|
| 575 |
"babel-cli", |
| 576 |
|
| 577 |
|
| 578 |
"babel-register", |
| 579 |
"babel-core", |
| 580 |
"babel-plugin-transform-runtime", |
| 581 |
|
| 582 |
"babel-node", |
| 583 |
|
| 584 |
"babel-helpers", |
| 585 |
|
| 586 |
"babel-plugin-transform-react-jsx", |
| 587 |
]); |
| 588 |
for (const packageDir of ["packages", "codemods"]) { |
| 589 |
for (const dir of fs.readdirSync(new URL(packageDir, import.meta.url))) { |
| 590 |
if (noBundle.has(dir)) continue; |
| 591 |
|
| 592 |
const src = `${packageDir}/${dir}`; |
| 593 |
|
| 594 |
let pkgJSON; |
| 595 |
try { |
| 596 |
pkgJSON = JSON.parse( |
| 597 |
fs.readFileSync(new URL(`${src}/package.json`, import.meta.url)) |
| 598 |
); |
| 599 |
} catch (err) { |
| 600 |
if (err.code !== "ENOENT" && err.code !== "ENOTDIR") throw err; |
| 601 |
continue; |
| 602 |
} |
| 603 |
if (pkgJSON.main) { |
| 604 |
yield { |
| 605 |
src, |
| 606 |
format: USE_ESM ? "esm" : "cjs", |
| 607 |
dest: "lib", |
| 608 |
input: getIndexFromPackage(src), |
| 609 |
}; |
| 610 |
} else if (pkgJSON.bin) { |
| 611 |
for (const binPath of Object.values(pkgJSON.bin)) { |
| 612 |
const filename = binPath.slice(binPath.lastIndexOf("/") + 1); |
| 613 |
const input = |
| 614 |
dir === "babel-cli" && filename === "babel.js" |
| 615 |
? `${src}/src/babel/index.ts` |
| 616 |
: `${src}/src/${filename.slice(0, -3) + ".ts"}`; |
| 617 |
yield { |
| 618 |
src, |
| 619 |
format: USE_ESM ? "esm" : "cjs", |
| 620 |
dest: "lib", |
| 621 |
filename, |
| 622 |
input, |
| 623 |
}; |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
let libBundles; |
| 631 |
if (bool(process.env.BABEL_8_BREAKING)) { |
| 632 |
libBundles = Array.from(libBundlesIterator()); |
| 633 |
} else { |
| 634 |
libBundles = [ |
| 635 |
"packages/babel-parser", |
| 636 |
"packages/babel-plugin-proposal-destructuring-private", |
| 637 |
"packages/babel-plugin-proposal-object-rest-spread", |
| 638 |
"packages/babel-plugin-proposal-optional-chaining", |
| 639 |
"packages/babel-preset-react", |
| 640 |
"packages/babel-plugin-transform-destructuring", |
| 641 |
"packages/babel-preset-typescript", |
| 642 |
"packages/babel-helper-member-expression-to-functions", |
| 643 |
"packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining", |
| 644 |
"packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression", |
| 645 |
].map(src => ({ |
| 646 |
src, |
| 647 |
format: USE_ESM ? "esm" : "cjs", |
| 648 |
dest: "lib", |
| 649 |
input: getIndexFromPackage(src), |
| 650 |
})); |
| 651 |
} |
| 652 |
|
| 653 |
const cjsBundles = [ |
| 654 |
|
| 655 |
{ src: "packages/babel-parser" }, |
| 656 |
]; |
| 657 |
|
| 658 |
const dtsBundles = ["packages/babel-types"]; |
| 659 |
|
| 660 |
const standaloneBundle = [ |
| 661 |
{ |
| 662 |
src: "packages/babel-standalone", |
| 663 |
format: "umd", |
| 664 |
name: "Babel", |
| 665 |
filename: "babel.js", |
| 666 |
dest: "", |
| 667 |
version: babelVersion, |
| 668 |
envName: "standalone", |
| 669 |
input: getIndexFromPackage("packages/babel-standalone"), |
| 670 |
}, |
| 671 |
]; |
| 672 |
|
| 673 |
gulp.task("generate-type-helpers", () => { |
| 674 |
log("Generating @babel/types and @babel/traverse dynamic functions"); |
| 675 |
|
| 676 |
return Promise.all([ |
| 677 |
generateTypeHelpers("asserts"), |
| 678 |
generateTypeHelpers("builders"), |
| 679 |
generateTypeHelpers("builders", "uppercase.js"), |
| 680 |
generateTypeHelpers("constants"), |
| 681 |
generateTypeHelpers("validators"), |
| 682 |
generateTypeHelpers("ast-types"), |
| 683 |
generateTraverseHelpers("asserts"), |
| 684 |
generateTraverseHelpers("validators"), |
| 685 |
]); |
| 686 |
}); |
| 687 |
|
| 688 |
gulp.task("generate-runtime-helpers", () => { |
| 689 |
log("Generating @babel/helpers runtime helpers"); |
| 690 |
|
| 691 |
return generateRuntimeHelpers(); |
| 692 |
}); |
| 693 |
|
| •694 |
gulp.task("generate-standalone", async () => generateStandalone()); |
| 695 |
|
| 696 |
gulp.task("build-rollup", () => buildRollup(libBundles)); |
| •697 |
gulp.task("rollup-babel-standalone", async () => buildRollup(standaloneBundle, true)); |
| 698 |
gulp.task( |
| 699 |
"build-babel-standalone", |
| •700 |
async () => gulp.series("generate-standalone", "rollup-babel-standalone") |
| 701 |
); |
| 702 |
|
| 703 |
gulp.task("copy-dts", () => copyDts(dtsBundles)); |
| 704 |
gulp.task( |
| 705 |
"bundle-dts", |
| 706 |
gulp.series("copy-dts", () => buildRollupDts(dtsBundles)) |
| 707 |
); |
| 708 |
|
| 709 |
gulp.task("build-babel", () => buildBabel(true, libBundles)); |
| 710 |
|
| 711 |
gulp.task("build-vendor", async () => { |
| 712 |
const input = fileURLToPath( |
| 713 |
await importMetaResolve("import-meta-resolve", import.meta.url) |
| 714 |
); |
| 715 |
const output = "./packages/babel-core/src/vendor/import-meta-resolve.js"; |
| 716 |
|
| 717 |
const bundle = await rollup({ |
| 718 |
input, |
| 719 |
onwarn(warning, warn) { |
| •720 |
if (warning.code === 'THIS_IS_UNDEFINED') return; |
| 721 |
if (warning.code === "CIRCULAR_DEPENDENCY") return; |
| 722 |
warn(warning); |
| 723 |
}, |
| 724 |
plugins: [ |
| 725 |
rollupCommonJs({ defaultIsModuleExports: true }), |
| 726 |
rollupNodeResolve({ |
| 727 |
extensions: [".js", ".mjs", ".cjs", ".json"], |
| 728 |
preferBuiltins: true, |
| 729 |
}), |
| 730 |
], |
| 731 |
}); |
| 732 |
|
| 733 |
await bundle.write({ |
| 734 |
file: output, |
| 735 |
format: "es", |
| 736 |
sourcemap: false, |
| 737 |
exports: "named", |
| 738 |
banner: String.raw` |
| 739 |
/****************************************************************************\ |
| 740 |
* NOTE FROM BABEL AUTHORS * |
| 741 |
* This file is inlined from https://github.com/wooorm/import-meta-resolve, * |
| 742 |
* because we need to compile it to CommonJS. * |
| 743 |
\****************************************************************************/ |
| 744 |
|
| 745 |
`, |
| 746 |
}); |
| 747 |
|
| 748 |
fs.writeFileSync( |
| 749 |
output.replace(".js", ".d.ts"), |
| 750 |
`export function resolve(specifier: string, parent: string): Promise<string>;` |
| 751 |
); |
| 752 |
}); |
| 753 |
|
| 754 |
gulp.task("build-cjs-bundles", () => { |
| 755 |
if (!USE_ESM) { |
| 756 |
log( |
| 757 |
chalk.yellow( |
| 758 |
"Skipping CJS-compat bundles for ESM-based builds, because not compiling to ESM" |
| 759 |
) |
| 760 |
); |
| 761 |
return Promise.resolve(); |
| 762 |
} |
| 763 |
return Promise.all( |
| 764 |
cjsBundles.map(async ({ src, external = [] }) => { |
| 765 |
const input = `./${src}/lib/index.js`; |
| 766 |
const output = `./${src}/lib/index.cjs`; |
| 767 |
|
| 768 |
const bundle = await rollup({ |
| 769 |
input, |
| 770 |
external, |
| 771 |
onwarn(warning, warn) { |
| 772 |
if (warning.code === "CIRCULAR_DEPENDENCY") return; |
| 773 |
warn(warning); |
| 774 |
}, |
| 775 |
plugins: [ |
| 776 |
rollupCommonJs({ defaultIsModuleExports: true }), |
| 777 |
rollupNodeResolve({ |
| 778 |
extensions: [".js", ".mjs", ".cjs", ".json"], |
| 779 |
preferBuiltins: true, |
| 780 |
}), |
| 781 |
], |
| 782 |
}); |
| 783 |
|
| 784 |
await bundle.write({ |
| 785 |
file: output, |
| 786 |
format: "cjs", |
| 787 |
sourcemap: false, |
| 788 |
}); |
| 789 |
}) |
| 790 |
); |
| 791 |
}); |
| 792 |
|
| 793 |
gulp.task( |
| 794 |
"build", |
| 795 |
gulp.series( |
| 796 |
"build-vendor", |
| 797 |
gulp.parallel("build-rollup", "build-babel"), |
| 798 |
gulp.parallel("generate-type-helpers", "generate-runtime-helpers"), |
| 799 |
|
| 800 |
|
| 801 |
"build-babel", |
| 802 |
gulp.parallel("generate-standalone", "build-cjs-bundles") |
| 803 |
) |
| 804 |
); |
| 805 |
|
| 806 |
gulp.task("default", gulp.series("build")); |
| 807 |
|
| 808 |
|
| 809 |
gulp.task("build-no-bundle", () => buildBabel(true)); |
| 810 |
|
| 811 |
gulp.task("build-no-bundle-watch", () => buildBabel(false)); |
| 812 |
|
| 813 |
gulp.task( |
| 814 |
"build-dev", |
| 815 |
gulp.series( |
| 816 |
"build-vendor", |
| 817 |
"build-no-bundle", |
| 818 |
gulp.parallel( |
| 819 |
"generate-standalone", |
| 820 |
"generate-runtime-helpers", |
| 821 |
gulp.series( |
| 822 |
"generate-type-helpers", |
| 823 |
|
| 824 |
"build-no-bundle", |
| 825 |
"build-cjs-bundles" |
| 826 |
) |
| 827 |
) |
| 828 |
) |
| 829 |
); |
| 830 |
|
| 831 |
function watch() { |
| 832 |
gulp.watch(defaultSourcesGlob, gulp.task("build-no-bundle-watch")); |
| 833 |
gulp.watch(babelStandalonePluginConfigGlob, gulp.task("generate-standalone")); |
| 834 |
gulp.watch(buildTypingsWatchGlob, gulp.task("generate-type-helpers")); |
| 835 |
gulp.watch( |
| 836 |
[ |
| 837 |
"./packages/babel-helpers/src/helpers/*.js", |
| 838 |
"!./packages/babel-helpers/src/helpers/regeneratorRuntime.js", |
| 839 |
], |
| 840 |
gulp.task("generate-runtime-helpers") |
| 841 |
); |
| 842 |
if (USE_ESM) { |
| 843 |
gulp.watch( |
| 844 |
cjsBundles.map(({ src }) => `./${src}/lib/**.js`), |
| 845 |
gulp.task("build-cjs-bundles") |
| 846 |
); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
gulp.task( |
| 851 |
"watch", |
| 852 |
process.env.WATCH_SKIP_BUILD ? watch : gulp.series("build-dev", watch) |
| 853 |
); |