@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.
npm install --save-dev @babel/plugin-transform-export-namespace-fromDefault 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");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", {}]
]
};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" };The plugin follows standard Babel plugin architecture:
@babel/helper-plugin-utilsThe 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;
};
}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[];
}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;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;
}The plugin performs the following transformations:
ExportNamedDeclaration nodes with ExportNamespaceSpecifierExportDefaultSpecifier is present at index 0, it's processed separately firstImportDeclaration with namespace import and ExportNamedDeclaration with named export// 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;BABEL_8_BREAKING environment variable - when true, manipulateOptions is undefined and parser plugin configuration is skipped