or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Babel Plugin Transform Export Extensions

babel-plugin-transform-export-extensions is a Babel plugin that transforms extended ES6+ export syntax into standard ES2015 export statements. It specifically handles export * as ns from 'module' and export v from 'module' syntax by converting them into separate import and export statements that are compatible with ES2015 environments.

Package Information

  • Package Name: babel-plugin-transform-export-extensions
  • Package Type: npm
  • Language: JavaScript (ES6+)
  • Installation: npm install --save-dev babel-plugin-transform-export-extensions

Core Imports

// As a Babel plugin (most common usage)
// No direct import required - configured in .babelrc or Babel config

For programmatic usage:

const plugin = require("babel-plugin-transform-export-extensions");

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["transform-export-extensions"]
}

Via CLI

babel --plugins transform-export-extensions script.js

Via Node API

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

const result = babel.transform(code, {
  plugins: ["transform-export-extensions"]
});

Architecture

This plugin follows the standard Babel plugin architecture:

  • Plugin Factory: Default export function that returns plugin configuration
  • AST Visitor: Processes ExportNamedDeclaration nodes during Babel's AST traversal
  • Syntax Integration: Inherits syntax parsing from babel-plugin-syntax-export-extensions
  • Transformation Strategy: Replaces extended export syntax with import+export pairs

Capabilities

Plugin Factory Function

The main entry point that creates the Babel plugin configuration.

/**
 * Babel plugin factory function for transforming export extensions
 * @param {Object} babel - Babel environment object
 * @param {Object} babel.types - Babel types helper (aliased as 't')
 * @returns {Object} Babel plugin configuration object
 */
function default({ types: t }) {
  return {
    inherits: require("babel-plugin-syntax-export-extensions"),
    visitor: {
      ExportNamedDeclaration(path) { /* ... */ }
    }
  };
}

Namespace Export Extension Transformation

Transforms export * as ns from 'module' syntax into ES2015-compatible code.

Input:

export * as utils from "./utils";

Output:

import * as _utils from "./utils";
export { _utils as utils };

Default Export Extension Transformation

Transforms export v from 'module' syntax into ES2015-compatible code.

Input:

export MyComponent from "./MyComponent";

Output:

import _MyComponent from "./MyComponent";
export { _MyComponent as MyComponent };

Plugin Configuration Object

The object returned by the plugin factory function.

interface BabelPluginConfig {
  /** Inherits syntax parsing capabilities from babel-plugin-syntax-export-extensions */
  inherits: typeof require("babel-plugin-syntax-export-extensions");
  
  /** AST visitor configuration */
  visitor: {
    /** Processes export declarations with namespace or default specifiers */
    ExportNamedDeclaration(path: NodePath<ExportNamedDeclaration>): void;
  };
}

AST Visitor Method

Processes export declarations during Babel's AST traversal.

/**
 * Babel AST visitor method for ExportNamedDeclaration nodes
 * @param {NodePath} path - Babel NodePath for the export declaration
 * @param {Object} path.node - The AST node being processed
 * @param {Object} path.scope - Babel scope for identifier generation
 */
ExportNamedDeclaration(path: {
  node: ExportNamedDeclaration;
  scope: Scope;
}): void;

Usage Examples

Basic Plugin Usage

// babel.config.js
module.exports = {
  plugins: [
    "transform-export-extensions"
  ]
};

With Other Plugins

// .babelrc
{
  "plugins": [
    "external-helpers",
    "transform-export-extensions"
  ]
}

Programmatic Transformation

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

const code = `
export * as utils from "./utils";
export Component from "./Component";
`;

const result = babel.transform(code, {
  plugins: ["transform-export-extensions"]
});

console.log(result.code);
// Output:
// import * as _utils from "./utils";
// export { _utils as utils };
// import _Component from "./Component";
// export { _Component as Component };

Dependencies

This plugin requires the following dependencies:

  • babel-plugin-syntax-export-extensions: Provides syntax parsing for export extensions
  • babel-runtime: Babel runtime helpers for transpiled code

Supported Syntax

The plugin transforms these specific export extension patterns:

  1. Namespace exports: export * as identifier from "module"
  2. Default exports: export identifier from "module"
  3. Compound exports: Mixed default/namespace extensions with regular exports

Compound Export Examples

Mixed default and regular exports:

// Input
export v, { x, y as w } from "mod";

// Output  
import _v from "mod";
export { _v as v };
export { x, y as w } from "mod";

Mixed default and namespace exports:

// Input
export v, * as ns from "mod";

// Output
import _v from "mod";
export { _v as v };
import * as _ns from "mod";
export { _ns as ns };

The plugin does not transform:

  • Standard ES2015 exports (export { name }, export default value)
  • Re-exports without renaming (export * from "module")
  • Named re-exports (export { name } from "module")

Error Handling

The plugin operates during Babel's transformation phase. If syntax errors occur, they will be reported by Babel's parser. The plugin itself focuses on AST transformation and does not throw custom errors during normal operation.