babel-plugin-transform-eval is a Babel plugin that transforms eval() calls containing string literals by parsing and compiling the string content at transform time. This plugin optimizes static eval expressions during compilation, reducing runtime evaluation overhead while maintaining code functionality.
npm install --save-dev babel-plugin-transform-eval// CommonJS (for Babel configuration)
const transformEval = require("babel-plugin-transform-eval");
// ES6 Modules (for programmatic usage)
import transformEval from "babel-plugin-transform-eval";.babelrc
{
"plugins": ["transform-eval"]
}babel --plugins transform-eval script.jsconst babel = require("babel-core");
const result = babel.transform(code, {
plugins: ["transform-eval"]
});The plugin implements Babel's visitor pattern to traverse the Abstract Syntax Tree (AST) and identify CallExpression nodes that match eval() calls. It uses Babel's evaluation engine to safely determine string literal values at compile time, then parses and replaces those eval calls with their corresponding AST structures.
Key components:
The main export creates a Babel plugin that transforms eval() calls with string literals.
/**
* Creates a Babel plugin that transforms eval() calls containing string literals
* @param {Object} context - Babel plugin context
* @param {Function} context.parse - Babel's parse function for parsing JavaScript code
* @param {Object} context.traverse - Babel's traverse utility with removeProperties method
* @returns {Object} Babel plugin object with visitor pattern
*/
export default function ({ parse, traverse }) {
return {
visitor: {
CallExpression(path) {
// Plugin implementation
}
}
};
}Transformation Examples:
Input:
eval("(() => 'foo')");Output:
(function () { return 'foo'; });Note: The plugin replaces the entire eval() call with the parsed AST of the string content, effectively inlining the evaluated code at compile time.
The plugin returns a standard Babel plugin object implementing the visitor pattern.
/**
* Babel plugin object structure returned by the factory function
* @typedef {Object} BabelPlugin
* @property {Object} visitor - AST node visitors for transformation
* @property {Function} visitor.CallExpression - Visitor function for CallExpression nodes
*/
const BabelPlugin = {
visitor: {
/**
* Visitor function for CallExpression nodes
* @param {Object} path - Babel path object for the CallExpression node
* @returns {Object|undefined} Replacement AST program or undefined for no transformation
*/
CallExpression(path) {
// Implementation handles eval() transformation
}
}
};Handles the transformation logic for eval() calls with string literal arguments.
/**
* Visitor function that processes CallExpression nodes
* @param {Object} path - Babel path object for the CallExpression node
* @returns {Object|undefined} Replacement AST program or undefined for no transformation
*/
CallExpression(path) {
// Validates eval call with single argument
// Evaluates argument to get confident string value
// Parses string as JavaScript and returns program AST
}Transformation Conditions:
Safety Features:
The plugin gracefully handles various edge cases without throwing errors:
The plugin can be configured in multiple ways:
Package name registration:
{
"plugins": ["transform-eval"]
}Full path registration:
{
"plugins": ["babel-plugin-transform-eval"]
}Programmatic registration:
const babel = require("babel-core");
const transformEval = require("babel-plugin-transform-eval");
babel.transform(code, {
plugins: [transformEval]
});Runtime Dependencies:
babel-runtime@^6.22.0 - Babel runtime supportDevelopment Dependencies:
babel-helper-plugin-test-runner@^6.22.0 - Test runner helperThe plugin is designed for Babel 6.x and integrates with the broader Babel ecosystem for JavaScript transformation workflows.