babel-generator

Babel Generator 模块是 Babel 的代码生成器。它将 AST 输出为代码并包括源码映射(sourcemaps)。

安装:

yarn add babel-generator
# or
yarn add @babel/generator

使用:

import * as babylon from "babylon";
import generate from "babel-generator";
const code = `function square(n) {
  return n * n;
}`;
const ast = babylon.parse(code);
generate(ast, null, code);
// {
//   code: "...",
//   map: "..."
// }

你也可以给 generate() 传递选项。

generate(
  ast,
  {
    retainLines: false,
    compact: "auto",
    concise: false,
    quotes: "double"
    // ...
  },
  code
);

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress