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.
npm install --save-dev babel-plugin-transform-export-extensions// As a Babel plugin (most common usage)
// No direct import required - configured in .babelrc or Babel configFor programmatic usage:
const plugin = require("babel-plugin-transform-export-extensions");{
"plugins": ["transform-export-extensions"]
}babel --plugins transform-export-extensions script.jsconst babel = require("babel-core");
const result = babel.transform(code, {
plugins: ["transform-export-extensions"]
});This plugin follows the standard Babel plugin architecture:
ExportNamedDeclaration nodes during Babel's AST traversalbabel-plugin-syntax-export-extensionsThe 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) { /* ... */ }
}
};
}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 };Transforms export v from 'module' syntax into ES2015-compatible code.
Input:
export MyComponent from "./MyComponent";Output:
import _MyComponent from "./MyComponent";
export { _MyComponent as MyComponent };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;
};
}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;// babel.config.js
module.exports = {
plugins: [
"transform-export-extensions"
]
};// .babelrc
{
"plugins": [
"external-helpers",
"transform-export-extensions"
]
}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 };This plugin requires the following dependencies:
The plugin transforms these specific export extension patterns:
export * as identifier from "module"export identifier from "module"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:
export { name }, export default value)export * from "module")export { name } from "module")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.