Skip to main content

@babel/plugin-transform-template-literals

信息

此插件包含在 @babel/preset-env

¥This plugin is included in @babel/preset-env

示例

¥Example

输入

¥In

JavaScript
`foo${bar}`;

输出

¥Out

JavaScript
"foo".concat(bar);

安装

¥Installation

npm install --save-dev @babel/plugin-transform-template-literals

用法

¥Usage

¥With a configuration file (Recommended)

没有选项:

¥Without options:

babel.config.json
{
"plugins": ["@babel/plugin-transform-template-literals"]
}

有选项:

¥With options:

babel.config.json
{
"plugins": [
[
"@babel/plugin-transform-template-literals",
{
"loose": true
}
]
]
}

通过 CLI

¥Via CLI

Shell
babel --plugins @babel/plugin-transform-template-literals script.js

通过 Node API

¥Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-template-literals"],
});

选项

¥Options

loose

boolean,默认为 false

¥boolean, defaults to false.

提醒

考虑迁移到顶层 mutableTemplateObject 假设。

¥Consider migrating to the top level mutableTemplateObject assumption.

babel.config.json
{
"assumptions": {
"mutableTemplateObject": true
}
}

mutableTemplateObjecttrue 时,标记的模板字面量对象不会被冻结。所有模板字面量表达式和准字都与 + 运算符而不是 String.prototype.concat 组合。

¥When mutableTemplateObject is true, tagged template literal objects aren't frozen. All template literal expressions and quasis are combined with the + operator instead of with String.prototype.concat.

false 或未设置时,所有模板字面量表达式和准词都与 String.prototype.concat 组合。如果模板字面量表达式是 Symbol(),它将正确处理 Symbol.toPrimitive 的情况并正确抛出。见 babel/babel#5791

¥When false or not set, all template literal expressions and quasis are combined with String.prototype.concat. It will handle cases with Symbol.toPrimitive correctly and throw correctly if template literal expression is a Symbol(). See babel/babel#5791.

输入

¥In

JavaScript
`foo${bar}`;

输出

¥Out

JavaScript
"foo" + bar;
提示

你可以阅读有关配置插件选项 此处 的更多信息

¥You can read more about configuring plugin options here