chensenlai 10988628a0 语音房项目初始化 | %!s(int64=5) %!d(string=hai) anos | |
---|---|---|
.. | ||
lib | %!s(int64=5) %!d(string=hai) anos | |
node_modules | %!s(int64=5) %!d(string=hai) anos | |
.npmignore | %!s(int64=5) %!d(string=hai) anos | |
README.md | %!s(int64=5) %!d(string=hai) anos | |
package.json | %!s(int64=5) %!d(string=hai) anos |
Generate an AST from a string template.
In computer science, this is known as an implementation of quasiquotes.
$ npm install 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');