or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-operations.mdbrowser-integration.mdindex.mdplugin-system.mdtransformation.mdutilities.md
tile.json

transformation.mddocs/

Code Transformation

Core synchronous and asynchronous transformation functions for JavaScript code strings and files, providing the fundamental API for programmatic code transformation.

Capabilities

Transform Function

Synchronously transforms JavaScript code string with the specified options.

/**
 * Transforms the passed in code string
 * @param {string} code - Source code to transform
 * @param {Object} opts - Transformation options
 * @returns {Object} Result object with code, map, ast, metadata, options, ignored properties
 */
function transform(code, opts);

// Transform result object structure:
// {
//   code: string,        // Transformed code (if opts.code !== false)
//   map: Object,         // Source map (if opts.sourceMaps is truthy)
//   ast: Object,         // AST (if opts.ast !== false)
//   metadata: Object,    // Transformation metadata (if opts.metadata !== false)
//   options: Object,     // Resolved options used
//   ignored: boolean     // Whether file was ignored
// }

Usage Examples:

const babel = require("babel-core");

// Basic transformation
const result = babel.transform("const fn = () => 42;", {
  presets: ["es2015"]
});
console.log(result.code); // "var fn = function fn() { return 42; };"

// With source maps
const result = babel.transform("const greeting = 'Hello';", {
  presets: ["es2015"],
  sourceMaps: true,
  filename: "input.js"
});
console.log(result.code); // transformed code
console.log(result.map);  // source map object

// Return only AST
const result = babel.transform("class MyClass {}", {
  code: false,
  ast: true
});
console.log(result.ast); // AST object
console.log(result.code); // null

Transform File Async

Asynchronously transforms the entire contents of a file.

/**
 * Asynchronously transforms entire file contents
 * @param {string} filename - Path to file to transform
 * @param {Object} opts - Transformation options (optional)
 * @param {Function} callback - Callback function (err, result)
 */
function transformFile(filename, opts, callback);

Usage Examples:

const babel = require("babel-core");

// Transform file with callback
babel.transformFile("src/input.js", {
  presets: ["es2015"],
  sourceMaps: true
}, function(err, result) {
  if (err) {
    console.error("Transform error:", err);
    return;
  }
  console.log(result.code);
  console.log(result.map);
});

// With options parameter optional
babel.transformFile("src/input.js", function(err, result) {
  if (err) throw err;
  console.log(result.code);
});

Transform File Sync

Synchronously transforms the entire contents of a file.

/**
 * Synchronously transforms entire file contents
 * @param {string} filename - Path to file to transform
 * @param {Object} opts - Transformation options (optional, defaults to {})
 * @returns {Object} Transform result object
 */
function transformFileSync(filename, opts);

Usage Examples:

const babel = require("babel-core");

// Transform file synchronously
try {
  const result = babel.transformFileSync("src/input.js", {
    presets: ["es2015"],
    sourceMaps: "inline"
  });
  console.log(result.code);
} catch (err) {
  console.error("Transform error:", err);
}

// With default options
const result = babel.transformFileSync("src/input.js");
console.log(result.code);

Common Configuration Options

Core Options

// Transform options object structure:
const transformOptions = {
  // Output control
  ast: Boolean,           // Include AST in result (default: true)
  code: Boolean,          // Generate code (default: true)
  comments: Boolean,      // Output comments (default: true)
  compact: Boolean,       // Remove whitespace (default: "auto")
  
  // Source handling
  filename: String,       // Filename for errors/source maps (default: "unknown")
  filenameRelative: String, // Filename relative to sourceRoot
  sourceMaps: Boolean,    // Source map generation (true, false, "inline", "both")
  sourceMapTarget: String, // Source map file property
  sourceRoot: String,     // Source map sourceRoot property
  sourceType: String,     // Parse as "script" or "module"
  
  // Configuration files
  babelrc: Boolean,       // Use .babelrc files (default: true)
  extends: String,        // Path to .babelrc file to extend
  
  // File filtering  
  ignore: Array,          // Files to ignore (strings, RegExp, or Functions)
  only: Array,            // Only transform these files (strings, RegExp, or Functions)
  
  // Plugins and presets
  plugins: Array,         // List of plugins to load and use
  presets: Array,         // List of presets to load and use
  
  // Advanced options
  env: Object,            // Environment-specific options
  retainLines: Boolean,   // Retain line numbers (default: false)
  minified: Boolean,      // Generate minified output (default: false)
  
  // Parser and generator options
  parserOpts: Object,     // Options passed to babylon parser
  generatorOpts: Object   // Options passed to babel-generator
};

Plugin Configuration

// Plugin as function
{
  plugins: [
    function(babel) {
      return {
        visitor: {
          Identifier(path) {
            path.node.name = path.node.name.split("").reverse().join("");
          }
        }
      };
    }
  ]
}

// Plugin with options
{
  plugins: [
    ["transform-es2015-arrow-functions", { spec: true }]
  ]
}

// Preset configuration
{
  presets: [
    ["es2015", { modules: false }],
    "react"
  ]
}

Error Handling

Transform functions can throw various types of errors:

try {
  const result = babel.transform(invalidCode, options);
} catch (err) {
  if (err._babel) {
    // Babel-specific error
    console.error("Babel error:", err.message);
    console.error("Code frame:", err.codeFrame);
    console.error("Location:", err.loc);
  } else {
    // Other error
    console.error("General error:", err.message);
  }
}

Common error scenarios:

  • Syntax errors: Invalid JavaScript syntax in input code
  • Plugin errors: Plugin execution failures or invalid plugin configuration
  • File system errors: File not found or permission issues (transformFile/transformFileSync)
  • Configuration errors: Invalid options or preset/plugin resolution failures