Skip to main content

@babel/plugin-transform-template-literals

info

这个插件包含在 @babel/preset-env 中

示例​

🌐 Example

在

JavaScript
`foo${bar}`;

出

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。

caution

考虑迁移到顶层 mutableTemplateObject 假设。

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

当 mutableTemplateObject 是 true 时,标记的模板字面量对象不会被冻结。所有模板字面量的表达式和字面量部分都是用 + 操作符组合的,而不是用 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.toPrimitive 的情况,如果模板字面量表达式是 Symbol() 也会正确抛出错误。具体见 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.

在

JavaScript
`foo${bar}`;

出

JavaScript
"foo" + bar;
tip

你可以在这里了解更多关于配置插件选项的内容