UNPKG

@babel/node

Version:
185 lines (183 loc) 5.15 kB
import Module, { createRequire } from 'node:module'; import { inspect } from 'node:util'; import path from 'node:path'; import repl from 'node:repl'; import * as babel from '@babel/core'; import vm from 'node:vm'; import 'core-js/stable/index.js'; import register from '@babel/register'; import { fileURLToPath } from 'node:url'; import { program } from './program-setup-shared.js'; const require$1 = createRequire(import.meta.url); program.parse(process.argv); const opts = program.opts(); const babelOptions = { caller: { name: "@babel/node", supportsStaticESM: false, supportsDynamicImport: false, supportsExportNamespaceFrom: false }, extensions: opts.extensions, ignore: opts.ignore, only: opts.only, plugins: opts.plugins, presets: opts.presets, configFile: opts.configFile, envName: opts.envName, rootMode: opts.rootMode, babelrc: opts.babelrc === true ? undefined : opts.babelrc }; for (const key of Object.keys(babelOptions)) { if (babelOptions[key] === undefined) { delete babelOptions[key]; } } register(babelOptions); let hasTopLevelAwait = false; const replPlugin = ({ types: t }) => ({ visitor: { Program(path) { hasTopLevelAwait = path.node.extra?.topLevelAwait; let hasExpressionStatement; for (const bodyPath of path.get("body")) { if (bodyPath.isExpressionStatement()) { hasExpressionStatement = true; } else if (bodyPath.isExportDeclaration() || bodyPath.isImportDeclaration()) { throw bodyPath.buildCodeFrameError("Modules aren't supported in the REPL"); } } if (hasTopLevelAwait) { const body = path.node.body; for (let i = body.length - 1; i >= 0; i--) { if (t.isExpressionStatement(body[i])) { body[i] = t.returnStatement(body[i].expression); break; } } return; } if (hasExpressionStatement) return; path.pushContainer("body", t.expressionStatement(t.identifier("undefined"))); } } }); const _eval = function (code, filename) { code = code.trim(); if (!code) return undefined; hasTopLevelAwait = false; code = babel.transformSync(code, { filename: filename, ...babelOptions, parserOpts: { allowAwaitOutsideFunction: true }, plugins: (opts.plugins || []).concat([replPlugin]) }).code; if (hasTopLevelAwait) { code = `(async () => { ${code} })()`; } return vm.runInThisContext(code, { filename: filename }); }; if (opts.eval || opts.print) { let code = opts.eval; if (!code || code === true) code = opts.print; global.__filename = "[eval]"; global.__dirname = process.cwd(); const module = new Module(global.__filename); module.filename = global.__filename; module.paths = Module._nodeModulePaths(global.__dirname); global.exports = module.exports; global.module = module; global.require = module.require.bind(module); const result = _eval(code, global.__filename); if (opts.print) { const output = typeof result === "string" ? result : inspect(result); process.stdout.write(output + "\n"); } } else { if (program.args.length) { let args = process.argv.slice(2); let i = 0; let ignoreNext = false; args.some(function (arg, i2) { if (ignoreNext) { ignoreNext = false; return; } if (arg.startsWith("-")) { const parsedOption = program.options.find(option => { return option.long === arg || option.short === arg; }); if (parsedOption === undefined) { return; } const optionName = parsedOption.attributeName(); const parsedArg = opts[optionName]; if (optionName === "require" || parsedArg && parsedArg !== true) { ignoreNext = true; } } else { i = i2; return true; } }); args = args.slice(i); requireArgs(); const filename = args[0]; if (!path.isAbsolute(filename)) { args[0] = path.join(process.cwd(), filename); } process.argv = ["node", ...args]; process.execArgv.push(fileURLToPath(import.meta.url)); Module.runMain(); } else { requireArgs(); replStart(); } } function requireArgs() { if (opts.require) { require$1(require$1.resolve(opts.require, { paths: [process.cwd()] })); } } function replEval(code, context, filename, callback) { let err; let result; try { if (code.startsWith("(") && code.endsWith(")")) { code = code.slice(1, -1); } result = _eval(code, filename); } catch (e) { err = e; } if (hasTopLevelAwait && !err) { result.then(v => { callback(null, v); }).catch(e => { callback(e, null); }); } else { callback(err, result); } } function replStart() { const replServer = repl.start({ prompt: "babel > ", input: process.stdin, output: process.stdout, eval: replEval, useGlobal: true, preview: true }); const NODE_REPL_HISTORY = process.env.NODE_REPL_HISTORY; replServer.setupHistory(NODE_REPL_HISTORY, () => {}); } //# sourceMappingURL=_babel-node.js.map