@babel/plugin-transform-template-literals
此插件包含在 @babel/preset-env
中
¥This plugin is included in @babel/preset-env
示例
¥Example
输入
¥In
`foo${bar}`;
输出
¥Out
"foo".concat(bar);
安装
¥Installation
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-template-literals
yarn add --dev @babel/plugin-transform-template-literals
pnpm add --save-dev @babel/plugin-transform-template-literals
用法
¥Usage
使用配置文件(推荐)
¥With a configuration file (Recommended)
没有选项:
¥Without options:
{
"plugins": ["@babel/plugin-transform-template-literals"]
}
有选项:
¥With options:
{
"plugins": [
[
"@babel/plugin-transform-template-literals",
{
"loose": true
}
]
]
}
通过 CLI
¥Via CLI
babel --plugins @babel/plugin-transform-template-literals script.js
通过 Node API
¥Via Node API
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.
{
"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()
,它将正确处理 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
`foo${bar}`;
输出
¥Out
"foo" + bar;