/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
scripts/babel-plugin-transform-node-protocol-import/plugin.ts
50 строк
1 KB
liuxingbaoyu
chore: Migrate scripts to `.ts` (#17767)
13 фев 2026, 17:26
Не верифицирован
13 фев 2026, 17:26
931ddd6
Код
Авторство
О чём код?
import type { PluginAPI, PluginObject } from "@babel/core"; export default pluginBabelTransformNodeProtocolImports; /** * Remove leading `node:` from the input string */ function replaceNodeProtocol(input: string): string { return input.replace(/^node:/, ""); } /** * Plugin definition */ function pluginBabelTransformNodeProtocolImports({ types: t, targets, }: PluginAPI): PluginObject { let target; // Skip this plugin for Node.js >= 16, api.targets could be undefined in e2e-old-babel test if ( targets && (target = targets()).node && +target.node.split(".")[0] >= 16 ) { return {}; } return { name: "babel-plugin-transform-node-protocol-imports", visitor: { ImportDeclaration({ node }) { node.source.value = replaceNodeProtocol(node.source.value); }, ImportExpression({ node }) { if (t.isStringLiteral(node.source)) { node.source.value = replaceNodeProtocol(node.source.value); } }, CallExpression({ node }) { if ( t.isIdentifier(node.callee, { name: "require" }) && t.isStringLiteral(node.arguments[0]) ) { node.arguments[0].value = replaceNodeProtocol( node.arguments[0].value ); } }, }, }; }