babel-template

Babel Template 模块是一个很小但却非常有用的模块。它能让你编写带有占位符的字符串形式的代码,你可以用此来替代大量的手工构建的 AST。

安装:

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

使用:

import template from "babel-template";
import generate from "babel-generator";
import * as t from "babel-types";
const buildRequire = template(`var IMPORT_NAME = require(SOURCE);`);
const ast = buildRequire({
  IMPORT_NAME: t.identifier("myModule"),
  SOURCE: t.stringLiteral("my-module")
});
console.log(generate(ast).code);
// var myModule = require("my-module");

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

Design by Quanzaiyu | Power by VuePress