CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--plugin-transform-export-namespace-from

Babel plugin that transforms modern JavaScript export namespace syntax into ES2015-compatible code

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

@babel/plugin-transform-export-namespace-from

@babel/plugin-transform-export-namespace-from is a Babel plugin that transforms modern JavaScript export namespace syntax (export * as name from 'module') into ES2015-compatible code. The plugin enables developers to use the export namespace from syntax in their code while maintaining compatibility with older JavaScript environments.

Package Information

  • Package Name: @babel/plugin-transform-export-namespace-from
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-transform-export-namespace-from

Core Imports

Default import for use as a Babel plugin:

import plugin from "@babel/plugin-transform-export-namespace-from";

CommonJS:

const plugin = require("@babel/plugin-transform-export-namespace-from");

Basic Usage

In Babel Configuration

Add the plugin to your Babel configuration:

{
  "plugins": ["@babel/plugin-transform-export-namespace-from"]
}

Or with options:

module.exports = {
  plugins: [
    ["@babel/plugin-transform-export-namespace-from", {}]
  ]
};

Transformation Examples

The plugin transforms export namespace declarations:

Input:

export * as foo from "bar";
export * as default from "foo";
export * as "some exports" from "baz";

Output:

import * as _foo from "bar";
export { _foo as foo };
import * as _default from "foo";
export { _default as default };
import * as _someExports from "baz";
export { _someExports as "some exports" };

Architecture

The plugin follows standard Babel plugin architecture:

  • Plugin Factory: Main export is a plugin factory function created using @babel/helper-plugin-utils
  • AST Visitor Pattern: Uses visitor pattern to traverse and transform AST nodes
  • Scope Analysis: Generates unique identifiers to prevent naming conflicts
  • Node Transformation: Converts single export namespace declarations into separate import + export statements

Capabilities

Plugin Export

The main and only export is a Babel plugin factory function.

/**
 * Babel plugin factory function that returns a plugin configuration object
 * Asserts minimum Babel version 7 using REQUIRED_VERSION global function
 * Conditionally includes parser options based on BABEL_8_BREAKING environment variable
 * @param api - Babel plugin API providing version assertion and utilities
 * @returns Plugin configuration object with name, visitor, and optional parser options
 */
export default function(api: PluginAPI): PluginObj;

interface PluginAPI {
  /** Assert that Babel version meets the minimum required version */
  assertVersion(version: number | string): void;
}

interface PluginObj {
  name: string;
  manipulateOptions?: (opts: any, parser: any) => void;
  visitor: {
    [key: string]: (path: NodePath<any>) => void;
  };
}

Plugin Configuration Object

The plugin function returns a configuration object with the following properties:

interface PluginConfiguration {
  /** Plugin identifier name */
  name: "transform-export-namespace-from";
  
  /** Optional parser configuration function (undefined when BABEL_8_BREAKING is true) */
  manipulateOptions?: (opts: any, parser: ParserConfig) => void;
  
  /** AST visitor methods for transformation */
  visitor: {
    ExportNamedDeclaration(path: NodePath<ExportNamedDeclaration>): void;
  };
}

interface ParserConfig {
  plugins: string[];
}

Visitor Method

The core transformation logic is implemented in the visitor method:

/**
 * Transforms ExportNamedDeclaration nodes containing ExportNamespaceSpecifier
 * Handles complex cases including ExportDefaultSpecifier and mixed specifier scenarios
 * Converts export namespace declarations into separate import and export statements
 * Generates unique identifiers and preserves remaining specifiers when present
 * @param path - Babel AST node path for ExportNamedDeclaration
 */
ExportNamedDeclaration(path: NodePath<ExportNamedDeclaration>): void;

Types

Babel Core Types

The plugin uses Babel's core AST types for transformation:

// From @babel/core
import { types as t } from "@babel/core";

// Core Babel types used in the plugin
interface NodePath<T> {
  node: T;
  scope: Scope;
  replaceWithMultiple(nodes: any[]): any[];
}

interface Scope {
  generateUidIdentifier(name: string): Identifier;
  registerDeclaration(path: any): void;
}

interface ExportNamedDeclaration {
  specifiers: (ExportSpecifier | ExportNamespaceSpecifier | ExportDefaultSpecifier)[];
  source: StringLiteral | null;
}

interface ExportNamespaceSpecifier {
  type: "ExportNamespaceSpecifier";
  exported: Identifier | StringLiteral;
}

interface ExportDefaultSpecifier {
  type: "ExportDefaultSpecifier";
  exported: Identifier;
}

interface ExportSpecifier {
  type: "ExportSpecifier";
  local: Identifier;
  exported: Identifier | StringLiteral;
}

interface ImportDeclaration {
  specifiers: ImportSpecifier[];
  source: StringLiteral;
}

interface ImportNamespaceSpecifier {
  type: "ImportNamespaceSpecifier";
  local: Identifier;
}

interface ImportSpecifier {
  type: "ImportSpecifier";
  local: Identifier;
  imported: Identifier | StringLiteral;
}

interface Identifier {
  type: "Identifier";
  name: string;
}

interface StringLiteral {
  type: "StringLiteral";
  value: string;
}

Transformation Logic

The plugin performs the following transformations:

  1. Detection: Identifies ExportNamedDeclaration nodes with ExportNamespaceSpecifier
  2. Default Export Handling: When ExportDefaultSpecifier is present at index 0, it's processed separately first
  3. Unique ID Generation: Creates unique identifiers using scope analysis to prevent conflicts based on exported name
  4. Node Creation: Generates new ImportDeclaration with namespace import and ExportNamedDeclaration with named export
  5. Remaining Specifiers: If other specifiers remain after processing namespace specifier, the original node is preserved with remaining specifiers
  6. AST Replacement: Replaces original nodes with multiple transformed nodes and registers new import declaration in scope

Dependencies

Runtime Dependencies

// Required for plugin factory creation
import { declare } from "@babel/helper-plugin-utils";

// Global function available in Babel build environment
declare function REQUIRED_VERSION(version: number): number | string;
declare function REQUIRED_VERSION(version: string): string;

Peer Dependencies

  • @babel/core: ^7.0.0-0 (provides transformation API and AST types)

Node.js Compatibility

  • Minimum Version: Node.js 6.9.0
  • Babel Version: Requires Babel 7.0+ (plugin asserts minimum version during initialization)
  • Babel 8 Support: Conditional behavior based on BABEL_8_BREAKING environment variable - when true, manipulateOptions is undefined and parser plugin configuration is skipped
  • Module Type: ESM with CommonJS compatibility through exports configuration

docs

index.md

tile.json