2019-10-29 NodeJS auto export al
2019-10-29 本文已影响0人
五大RobertWu伍洋
acorn helps fix the issue of esprima
const autoParse = () => {
const acorn = require("acorn");
const fs = require("fs");
const program = fs.readFileSync(__filename, "utf8");
const parsed = acorn.parse(program);
parsed.body.forEach(fn => {
if (fn.type.endsWith("VariableDeclaration")) {
const fnv = fn.declarations[0];
module.exports[fnv.id.name] = eval(fnv.id.name);
}
if (fn.type.endsWith("FunctionDeclaration")) {
module.exports[fn.id.name] = eval(fn.id.name);
}
});
};
autoParse();