Extract Export Babel Plugin
raw JSON → 0.1.0 verified Sat Apr 25 auth: no javascript maintenance
A Babel plugin that extracts a specific ESM export and its dependencies from a file, based on a fork of NextJS's SSG transform plugin. Current version 0.1.0, single release. It operates by performing static analysis with Babel to remove unused exports and import statements, outputting only the targeted export and its prerequisites. Differentiates from tree-shaking tools by allowing per-export extraction at build time, useful for code splitting or dynamic imports. Note: The package has not been updated since initial release and may have limited compatibility with newer Babel versions.
Common errors
error Error: Cannot find module 'babel-plugin-extract-export' ↓
cause Package not installed or node_modules missing.
fix
Run 'yarn add babel-plugin-extract-export' or 'npm install babel-plugin-extract-export'.
error TypeError: plugin is not a function ↓
cause Passing the plugin incorrectly to Babel's plugins array; likely double-wrapped in array.
fix
Use [[plugin, options]] syntax correctly: 'plugins: [[extractExport, { exportName: 'Avatar' }]]'.
error Cannot read property 'replace' of undefined at ... PluginPass.ExportNamedDeclaration ↓
cause Plugin encountered a file without any named exports matching the specified exportName, or the file is not parseable.
fix
Ensure the file has an exported variable or function with the exact name given in exportName option.
Warnings
breaking Version 0.1.0 is the only release and may break with Babel versions other than 7.x. No tests for Babel 8. ↓
fix Ensure Babel 7 compatibility; upgrade to future releases if available.
deprecated The plugin is in maintenance mode with no recent updates. It may be deprecated or superseded by NextJS's official transforms. ↓
fix Consider using NextJS's SSG transform directly or a more maintained alternative like babel-plugin-transform-imports.
gotcha Plugin only extracts a single named export (via 'exportName' option). Multiple exports or default exports are not supported. ↓
fix Use separate passes or a different plugin if you need to extract multiple exports.
gotcha Requires @babel/plugin-syntax-typescript with { isTSX: true } when extracting from TypeScript JSX files; otherwise parsing may fail silently. ↓
fix Always include the syntax plugin with appropriate configuration for TSX files.
Install
npm install babel-plugin-extract-export yarn add babel-plugin-extract-export pnpm add babel-plugin-extract-export Imports
- default
const extractExport = require('babel-plugin-extract-export') - default wrong
import * as extractExport from 'babel-plugin-extract-export'correctimport extractExport from 'babel-plugin-extract-export' - default wrong
plugins: [[extractExport, { exportName: 'MyExport' }]]correctplugins: [extractExport, { exportName: 'MyExport' }]
Quickstart
const babel = require('@babel/core');
const syntaxTypeScript = require('@babel/plugin-syntax-typescript');
const extractExport = require('babel-plugin-extract-export');
const source = `
import { Image } from 'system';
export const Avatar = () => <Image />;
type BoxProps = { children: any };
export const Box = (props: BoxProps) => <div {...props} />;
`;
const result = babel.transformSync(source, {
configFile: false,
plugins: [
[syntaxTypeScript, { isTSX: true }],
[extractExport, { exportName: 'Avatar' }],
],
});
console.log(result.code);
// Expected output:
// import { Image } from 'system';
// export const Avatar = () => <Image />;