Utilities for working with module metadata, validation, and checking module characteristics. These functions help analyze and validate module structure and import/export patterns.
Check if a module has exports that need special handling.
/**
* Check if the module has any exports that need handling.
*/
function hasExports(metadata: ModuleMetadata): boolean;Usage Example:
import { hasExports } from "@babel/helper-module-transforms";
const hasModuleExports = hasExports(moduleMetadata);
if (hasModuleExports) {
// Generate export initialization code
generateExportStatements();
}Determine if an import is purely for side effects.
/**
* Check if a given source is an anonymous import, e.g. "import 'foo';"
* Returns true for imports that don't import any bindings.
*/
function isSideEffectImport(source: SourceModuleMetadata): boolean;Usage Example:
import { isSideEffectImport } from "@babel/helper-module-transforms";
if (isSideEffectImport(sourceMetadata)) {
// This is a side-effect only import like "import './styles.css'"
// Handle differently - no bindings need to be created
handleSideEffectImport(sourceMetadata);
}Check if a Babel path represents a module (vs a script).
/**
* Check if the given path represents a module (has import/export statements)
* rather than a regular script.
*/
function isModule(path: NodePath): boolean;Usage Example:
import { isModule } from "@babel/helper-module-transforms";
if (isModule(programPath)) {
// This file uses ES6 modules - can transform import/export statements
transformModuleStatements(programPath);
} else {
// This is a regular script - no module transformations needed
handleScript(programPath);
}Complete metadata about a module's imports and exports.
interface ModuleMetadata {
/** Name of the export object (e.g., "exports") */
exportName: string;
/** Name of the variable containing export names list */
exportNameListName: null | string;
/** Whether the module has any exports */
hasExports: boolean;
/** Map of local binding names to their export metadata */
local: Map<string, LocalExportMetadata>;
/** Map of source module names to their metadata */
source: Map<string, SourceModuleMetadata>;
/** Set of export names that must be treated as string literals */
stringSpecifiers: Set<string>;
}Metadata about a specific imported source module.
interface SourceModuleMetadata {
/** Unique variable name for this namespace object */
name: string;
/** Source location information */
loc: SourceLocation | undefined | null;
/** Interop strategy for this module */
interop: InteropType;
/** Map of local names to import names */
imports: Map<string, string>;
/** Set of local names that reference the namespace object */
importsNamespace: Set<string>;
/** Map of export names to import names for re-exports */
reexports: Map<string, string>;
/** Set of names to re-export as namespace */
reexportNamespace: Set<string>;
/** Information about export * re-exports */
reexportAll: null | {
loc: SourceLocation | undefined | null;
};
/** Wrapper payload for lazy loading */
wrap?: unknown;
/** Whether this source is actually referenced */
referenced: boolean;
}Metadata about locally defined exports.
interface LocalExportMetadata {
/** Names this local binding is exported as */
names: Array<string>;
/** Kind of local binding */
kind: "import" | "hoisted" | "block" | "var";
}Enumeration of interoperability strategies.
type InteropType =
| "default" // Babel interop for default-only imports
| "namespace" // Babel interop for namespace or default+named imports
| "node-default" // Node.js interop for default-only imports
| "node-namespace" // Node.js interop for namespace or default+named imports
| "none"; // No interop, or named-only importsConfiguration for import interoperability strategies.
type ImportInterop =
| "none" // No interop helpers
| "babel" // Babel-style interop helpers
| "node" // Node.js-style interop helpers
| ((source: string, filename?: string) => "none" | "babel" | "node");