What is @babel/plugin-transform-literals?
The @babel/plugin-transform-literals package is a plugin for Babel, a JavaScript compiler, that transforms modern JavaScript literal syntax into a form compatible with older JavaScript engines. This includes transforming template literals, binary literals, octal literals, and Unicode string literals into strings and numbers that can be understood by environments that do not support these features natively.
What are @babel/plugin-transform-literals's main functionalities?
Template Literals Transformation
Converts template literals into string concatenation to ensure compatibility with older JavaScript engines.
`Hello, ${name}!` -> 'Hello, ' + name + '!'
Binary Literals Transformation
Transforms binary literals into their decimal number equivalents.
0b101001 -> 41
Octal Literals Transformation
Converts octal literals into their decimal number equivalents.
0o755 -> 493
Unicode String Literals Transformation
Transforms Unicode string literals into escaped sequences compatible with older JavaScript engines.
'\u{1F680}' -> '\uD83D\uDE80'
Other packages similar to @babel/plugin-transform-literals
@babel/plugin-transform-template-literals
This package specifically focuses on transforming template literals into string concatenation. While @babel/plugin-transform-literals covers a broader range of literal transformations, @babel/plugin-transform-template-literals provides a more focused approach on template literals, offering optimizations for cases where tagged templates are not used.
@babel/plugin-transform-unicode-escapes
Similar to the Unicode string literals transformation provided by @babel/plugin-transform-literals, this package transforms Unicode escapes in string literals and identifiers. It offers a more specialized focus on Unicode transformations, potentially providing more detailed handling of edge cases in Unicode escape sequences.
@babel/plugin-transform-literals
Compile ES2015 unicode string and number literals to ES5
Example
In
var b = 0b11;
var o = 0o7;
const u = 'Hello\u{000A}\u{0009}!';
Out
var b = 3;
var o = 7;
const u = 'Hello\n\t!';
Installation
npm install --save-dev @babel/plugin-transform-literals
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/plugin-transform-literals"]
}
Via CLI
babel --plugins @babel/plugin-transform-literals script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-literals"]
});