@babel/plugin-transform-template-literals
这个插件包含在 @babel/preset-env 中
示例
🌐 Example
在
`foo${bar}`;
出
"foo".concat(bar);
安装
🌐 Installation
- npm
- Yarn
- pnpm
- Bun
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
bun add --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。
考虑迁移到顶层 mutableTemplateObject 假设。
{
"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.
在
`foo${bar}`;
出
"foo" + bar;
你可以在这里了解更多关于配置插件选项的内容