Files
profile/themes/keepit/node_modules/babel-plugin-preval/dist/helpers.js
Spencer Pincott 97737ca1ae Initial commit
2024-07-15 22:20:13 -04:00

49 lines
983 B
JavaScript

"use strict";
const p = require('path');
const requireFromStringOfCode = require('require-from-string');
const objectToAST = require('./object-to-ast');
module.exports = {
getReplacement,
requireFromString
};
function requireFromString({
string: stringToPreval,
fileOpts: {
filename
},
args = []
}) {
let mod = requireFromStringOfCode(String(stringToPreval), filename);
mod = mod && mod.__esModule ? mod.default : mod;
if (typeof mod === 'function') {
mod = mod(...args);
} else if (args.length) {
throw new Error(`\`preval.require\`-ed module (${p.relative(process.cwd(), filename)}) cannot accept arguments because it does not export a function. You passed the arguments: ${args.join(', ')}`);
}
return mod;
}
function getReplacement({
string,
fileOpts,
args,
babel
}) {
const mod = requireFromString({
string,
fileOpts,
args,
babel
});
return objectToAST(mod, {
babel,
fileOptions: fileOpts
});
}