- Babel 是什么?
- 使用指南
- 配置 Babel
- Learn ES2015
- 升级到 Babel 7
- 升级到 Babel 7 (API)
- 编辑器
- 插件
- 预设(Presets)
- 附加说明
- FAQ
- Babel 路线图
- Options
- Config Files
- @babel/cli
- @babel/polyfill
- @babel/plugin-transform-runtime
- @babel/register
- @babel/preset-env
- @babel/preset-stage-0
- @babel/preset-stage-1
- @babel/preset-stage-2
- @babel/preset-stage-3
- @babel/preset-flow
- @babel/preset-react
- babel-preset-minify
- @babel/preset-typescript
- @babel/parser
- @babel/core
- @babel/generator
- @babel/code-frame
- @babel/helpers
- @babel/runtime
- @babel/template
- @babel/traverse
- @babel/types
@babel/template
In computer science, this is known as an implementation of quasiquotes.
Install
npm install --save-dev @babel/template
String Usage
When calling template
as a function with a string argument, you can provide placeholders which will get substituted when the template is used.
You can use two different kinds of placeholders: syntactic placeholders (e.g. %%name%%
) or identifier placeholders (e.g. NAME
). @babel/template
supports both those approaches by default, but they can't be mixed. If you need to be explicit about what syntax you are using, you can use the .ast
If no placeholders are in use and you just want a simple way to parse a string into an AST, you can use the .ast
version of the template.
const ast = template.ast(`
var myModule = require("my-module");
`);
which will parse and return the AST directly.
Template Literal Usage
import template from "@babel/template";
import generate from "@babel/generator";
import * as t from "@babel/types";
const source = "my-module";
const fn = template`
var IMPORT_NAME = require('${source}');
`;
const ast = fn({
IMPORT_NAME: t.identifier("myModule");
});
console.log(generate(ast).code);
Note that placeholders can be passed directly as part of the template literal in order to make things as readable as possible, or they can be passed into the template function.
.ast
If no placeholders are in use and you just want a simple way to parse a string into an AST, you can use the .ast
version of the template.
const name = "my-module";
const mod = "myModule";
const ast = template.ast`
var ${mod} = require("${name}");
`;
which will parse and return the AST directly. Note that unlike the string-based version mentioned earlier, since this is a template literal, it is still valid to perform replacements using template literal replacements.
AST results
The @babel/template
API exposes a few flexible APIs to make it as easy as possible to create ASTs with an expected structure. Each of these also has the .ast
property mentioned above.
template
template
returns either a single statement, or an array of statements, depending on the parsed result.
template.smart
This is the same as the default template
API, returning either a single node, or an array of nodes, depending on the parsed result.
template.statement
template.statement("foo;")()
returns a single statement node, and throw an exception if the result is anything but a single statement.
template.statements
template.statements("foo;foo;")()
returns an array of statement nodes.
template.expression
template.expression("foo")()
returns the expression node.
template.program
template.program("foo;")()
returns the Program
node for the template.
API
template(code, [opts])
code
Type: string
options
@babel/template
accepts all of the options from Babel Parser, and specifies some defaults of its own:
allowReturnOutsideFunction
is set totrue
by default.allowSuperOutsideMethod
is set totrue
by default.sourceType
is set tomodule
by default.
syntacticPlaceholders
Type: boolean
Default: true
if %%foo%%
-style placeholders are used; false
otherwise.
When this option is true
, you can use %%foo%%
to mark placeholders in your templates. When it is false
, placeholders are identifiers determined by the placeholderWhitelist
and placeholderPattern
options.
placeholderWhitelist
Type: Set<string>
Default: undefined
This option is not compatible with
syntacticPlaceholders: true
A set of placeholder names to automatically accept. Items in this list do not need to match the given placeholder pattern.
placeholderPattern
Type: RegExp | false
Default: /^[_$A-Z0-9]+$/
This option is not compatible with
syntacticPlaceholders: true
A pattern to search for when looking for Identifier and StringLiteral nodes that should be considered placeholders. 'false' will disable placeholder searching entirely, leaving only the 'placeholderWhitelist' value to find placeholders.
preserveComments
Type: boolean
Default: false
Set this to true
to preserve any comments from the code
parameter.
Return value
By default @babel/template
returns a function
which is invoked with an optional object of replacements. See the usage section for an example.
When using .ast
, the AST will be returned directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论