Babel plugin that transforms export namespace from syntax to ES2015 compatible code
npx @tessl/cli install tessl/npm-babel--plugin-proposal-export-namespace-from@7.18.0Babel plugin that transforms export namespace from syntax to ES2015-compatible code. It converts export * as ns from "module" statements into separate import and export declarations for broader browser compatibility.
npm install --save-dev @babel/plugin-proposal-export-namespace-fromconst plugin = require("@babel/plugin-proposal-export-namespace-from");ESM:
import plugin from "@babel/plugin-proposal-export-namespace-from";Add to your Babel configuration:
{
"plugins": ["@babel/plugin-proposal-export-namespace-from"]
}Or programmatically:
import { transform } from "@babel/core";
import exportNamespaceFrom from "@babel/plugin-proposal-export-namespace-from";
const result = transform(code, {
plugins: [exportNamespaceFrom]
});The main exported plugin function that integrates with Babel's transformation pipeline.
/**
* Creates a Babel plugin for transforming export namespace syntax
* @param api - Babel's plugin API object with utility methods
* @returns Plugin object with visitor pattern implementation
*/
declare function exportNamespaceFromPlugin(api: PluginAPI): PluginObject;
export default exportNamespaceFromPlugin;
// From @babel/core
interface PluginAPI {
version: string;
assertVersion(version: number | string): void;
types: typeof import("@babel/types");
template: any;
env: (name?: string) => string | undefined;
}
interface PluginObject {
name: string;
inherits?: any;
visitor: {
ExportNamedDeclaration(path: NodePath<ExportNamedDeclaration>): void;
};
}
// From @babel/traverse
interface NodePath<T = Node> {
node: T;
scope: Scope;
replaceWithMultiple(nodes: Node[]): NodePath[];
}
// From @babel/types
interface Node {
type: string;
}The plugin transforms the following syntax patterns:
Input:
export * as foo from "bar";Output:
import * as _foo from "bar";
export { _foo as foo };Input:
export * as default from "foo";Output:
import * as _default from "foo";
export { _default as default };Input:
export * as "some exports" from "foo";Output:
import * as _someExports from "foo";
export { _someExports as "some exports" };Input:
export * as foo, { bar } from "bar";Output:
import * as _foo from "bar";
export { _foo as foo };
export { bar } from "bar";// From @babel/types
interface ExportNamedDeclaration extends Node {
type: "ExportNamedDeclaration";
specifiers: (ExportSpecifier | ExportNamespaceSpecifier | ExportDefaultSpecifier)[];
source: StringLiteral | null;
declaration?: Declaration | null;
}
interface ExportNamespaceSpecifier extends Node {
type: "ExportNamespaceSpecifier";
exported: Identifier | StringLiteral;
}
interface ExportDefaultSpecifier extends Node {
type: "ExportDefaultSpecifier";
exported: Identifier;
}
interface ExportSpecifier extends Node {
type: "ExportSpecifier";
local: Identifier;
exported: Identifier | StringLiteral;
}
interface ImportDeclaration extends Node {
type: "ImportDeclaration";
specifiers: (ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier)[];
source: StringLiteral;
}
interface ImportNamespaceSpecifier extends Node {
type: "ImportNamespaceSpecifier";
local: Identifier;
}
// From @babel/traverse
interface Scope {
generateUidIdentifier(name?: string): Identifier;
registerDeclaration(path: NodePath): void;
}
interface Identifier extends Node {
type: "Identifier";
name: string;
}
interface StringLiteral extends Node {
type: "StringLiteral";
value: string;
}
interface Declaration extends Node {}This plugin does not accept any configuration options. It automatically detects and transforms all export namespace declarations in the source code.
declare helper for plugin creationThe plugin operates at the AST level and will only transform valid export namespace syntax. Invalid syntax will be handled by Babel's parser before reaching this plugin.