/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
scripts/rollup-plugin-standalone-internals.ts
62 строки
2 KB
liuxingbaoyu
chore: Migrate scripts to `.ts` (#17767)
13 фев 2026, 17:26
Не верифицирован
13 фев 2026, 17:26
931ddd6
Код
Авторство
О чём код?
import { fileURLToPath } from "node:url"; import pluginConfig from "../packages/babel-standalone/scripts/pluginConfig.json" with { type: "json" }; const standaloneURL = new URL("../packages/babel-standalone/", import.meta.url); /** * Convert a file path to a @babel/standalone file path. * @param {string} path - The original file path. * @returns {string} - The @babel/standalone file path. */ const inStandalone = (path: string): string => fileURLToPath(new URL(path, standaloneURL)).replace(/\\/g, "/"); const { noopPlugins, unexposedNoopPlugins } = pluginConfig; const pluginUtilsID = "@babel/helper-plugin-utils"; const generatedPluginsPath = inStandalone("./src/generated/plugins.ts"); const makeNoopPluginPath = inStandalone("./src/make-noop-plugin.ts"); const pluginUtilsShimPath = inStandalone("./src/plugin-utils-shim.ts"); /** * Convert a kebab-case string to camelCase. * @param {string} str - The kebab-case string. * @returns {string} - The camelCase string. */ const toCamel = (str: string): string => str.replace(/-[a-z]/g, c => c[1].toUpperCase()); /** * @typedef {Map<string, string>} InternalPluginsMap * @type {InternalPluginsMap} * A map of internal plugins where the key is the plugin name and the value is the * code to be returned by the Rollup plugin. */ // @ts-expect-error TS does not recognize the dynamic nature of the map const internalPlugins = new Map<string, string>([ ...noopPlugins.map(plugin => [ `@babel/plugin-${plugin}`, `export { ${toCamel(plugin)} as default } from "${generatedPluginsPath}"`, ]), ...unexposedNoopPlugins.map(plugin => [ `@babel/plugin-${plugin}`, `import makeNoopPlugin from "${makeNoopPluginPath}"; export default makeNoopPlugin()`, ]), ]); /** * Rollup plugin to handle Babel standalone internals. * It resolves internal noop syntax-only plugins and provides a shim for * `@babel/helper-plugin-utils`. * @returns {import("rollup").Plugin} - The Rollup plugin. */ export default function (): import("rollup").Plugin { return { name: "standalone-internals", load(id) { return internalPlugins.get(id) ?? null; }, resolveId(specifier) { if (specifier === pluginUtilsID) return pluginUtilsShimPath; return internalPlugins.has(specifier) ? specifier : null; }, }; }