/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v7.29.8
scripts/rollup-plugin-babel-source.js
122 строки
4 KB
Huáng Jùnliàng
Enable type checking for `scripts` and `babel-worker.cjs` (#17454)
30 июл 2025, 01:26
Не верифицирован
30 июл 2025, 01:26
ac0b069
Код
Авторство
О чём код?
// @ts-check import path from "node:path"; import fs from "node:fs"; import { fileURLToPath } from "node:url"; import { createRequire } from "node:module"; const require = createRequire(import.meta.url); const monorepoRoot = path.join( path.dirname(fileURLToPath(import.meta.url)), ".." ); const BABEL_SRC_REGEXP = path.sep === "/" ? /packages\/(babel-[^/]+)\/src\// : /packages\\(babel-[^\\]+)\\src\\/; /** * Rollup plugin to load Babel source files directly from the monorepo. * It resolves Babel packages to their `src` directory and handles the `browser` * field in `package.json`. * It also resolves `@babel/runtime/regenerator` to the correct path. * @returns {import("rollup").Plugin} - The Rollup plugin. */ export default function () { return { name: "babel-source", load(id) { const matches = id.match(BABEL_SRC_REGEXP); if (matches) { // check if browser field exists for this file and replace const packageFolder = path.join(monorepoRoot, "packages", matches[1]); const packageJson = require(path.join(packageFolder, "package.json")); if (packageJson.browser && typeof packageJson.browser === "object") { for (const nodeFile in packageJson.browser) { const browserFileAsJs = packageJson.browser[nodeFile].replace( /^(\.\/)?lib\//, "src/" ); const browserFileAsTs = browserFileAsJs.replace(/.js$/, ".ts"); const browserFile = fs.existsSync(browserFileAsTs) ? browserFileAsTs : browserFileAsJs; const nodeFileSrcAsJs = path.normalize( nodeFile.replace(/^[./]?lib\//, "src/") ); const nodeFileSrcAsTs = nodeFileSrcAsJs.replace(/.js$/, ".ts"); const nodeFileSrc = fs.existsSync(nodeFileSrcAsTs) ? nodeFileSrcAsTs : nodeFileSrcAsJs; if (id.endsWith(nodeFileSrc)) { if (browserFile === false) { return ""; } return fs.readFileSync( path.join(packageFolder, path.normalize(browserFile)), "utf8" ); } } } } return null; }, resolveId(importee) { if (importee === "@babel/runtime/regenerator") { return path.join( monorepoRoot, "packages", "babel-runtime", "regenerator", "index.js" ); } const matches = importee.match( /^@babel\/(?<pkg>[^/]+)(?:\/lib\/(?<internal>.*))?$/ ); 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 (e) { // 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; }, }; }