/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
scripts/rollup-plugin-babel-source.ts
72 строки
2 KB
liuxingbaoyu
chore: Migrate scripts to `.ts` (#17767)
13 фев 2026, 17:26
Не верифицирован
13 фев 2026, 17:26
931ddd6
Код
Авторство
О чём код?
import path from "node:path"; import fs from "node:fs"; import { fileURLToPath } from "node:url"; const monorepoRoot = path.join( path.dirname(fileURLToPath(import.meta.url)), ".." ); /** * Rollup plugin to load Babel source files directly from the monorepo. * It resolves Babel packages to their `src` directory. * It also resolves `@babel/runtime/regenerator` to the correct path. * @returns {import("rollup").Plugin} - The Rollup plugin. */ export default function (): import("rollup").Plugin { return { name: "babel-source", resolveId(importee) { if (importee === "@babel/runtime/regenerator") { return path.join( monorepoRoot, "packages", "babel-runtime", "regenerator", "index.js" ); } const matches = /^@babel\/(?<pkg>[^/]+)(?:\/lib\/(?<internal>.*))?$/.exec( importee ); if (!matches) return null; // @ts-expect-error pkg and internal are the names of capturing groups const { pkg, internal } = matches.groups; // resolve babel package names to their src index file const packageFolder = path.join(monorepoRoot, "packages", `babel-${pkg}`); let packageJsonSource; try { packageJsonSource = fs.readFileSync( path.join(packageFolder, "package.json"), "utf8" ); } catch (_) { // Some Babel packages aren't in this repository return null; } const packageJson = JSON.parse(packageJsonSource); const filename = internal ? `src/${internal}` : typeof packageJson.browser === "string" ? packageJson.browser : packageJson.main; let asJS = path.normalize( path.join( packageFolder, // replace lib with src in the package.json entry filename.replace(/^(\.\/)?lib\//, "src/") ) ); if (!/\.[a-z]+$/.test(asJS)) asJS += ".js"; const asTS = asJS.replace(/\.js$/, ".ts"); return fs.existsSync(asTS) ? asTS : asJS; }, }; }