@babel/helper-module-transforms provides essential helper functions and utilities for implementing ES6 module transformations in Babel. It serves as a core component that enables the transformation of modern JavaScript module syntax (import/export statements) into various module formats like CommonJS, AMD, UMD, or System.js.
npm install @babel/helper-module-transformsimport {
rewriteModuleStatementsAndPrepareHeader,
ensureStatementsHoisted,
wrapInterop,
buildNamespaceInitStatements,
buildDynamicImport,
getModuleName,
hasExports,
isSideEffectImport,
isModule,
rewriteThis
} from "@babel/helper-module-transforms";
// Import type definitions (only these types are exported from the main package)
import type {
RewriteModuleStatementsAndPrepareHeaderOptions,
PluginOptions
} from "@babel/helper-module-transforms";For CommonJS:
const {
rewriteModuleStatementsAndPrepareHeader,
ensureStatementsHoisted,
wrapInterop,
buildNamespaceInitStatements,
buildDynamicImport,
getModuleName,
hasExports,
isSideEffectImport,
isModule,
rewriteThis
} = require("@babel/helper-module-transforms");import { rewriteModuleStatementsAndPrepareHeader, isModule } from "@babel/helper-module-transforms";
import { NodePath } from "@babel/core";
import type { Program } from "@babel/types";
// Transform a module's import/export statements
function transformModule(programPath: NodePath<Program>) {
// Check if this is actually a module
if (!isModule(programPath)) {
throw new Error("Cannot process module statements in a script");
}
// Perform the main module transformation
const result = rewriteModuleStatementsAndPrepareHeader(programPath, {
strict: false,
strictMode: true,
allowTopLevelThis: false,
importInterop: "babel",
filename: "example.js"
});
return {
metadata: result.meta,
initStatements: result.headers
};
}@babel/helper-module-transforms is built around several key components:
Core functionality for rewriting ES6 module statements and preparing module initialization code. This is the primary API for transforming modules.
function rewriteModuleStatementsAndPrepareHeader(
path: NodePath<Program>,
options: RewriteModuleStatementsAndPrepareHeaderOptions
): { meta: ModuleMetadata; headers: Statement[] };
interface RewriteModuleStatementsAndPrepareHeaderOptions {
exportName?: string;
strict: boolean;
allowTopLevelThis?: boolean;
strictMode: boolean;
loose?: boolean;
importInterop?: ImportInterop;
noInterop?: boolean;
lazy?: Lazy;
getWrapperPayload?: (source: string, metadata: SourceModuleMetadata, importNodes: Node[]) => unknown;
wrapReference?: (ref: Expression, payload: unknown) => Expression | null;
esNamespaceOnly?: boolean;
filename: string | undefined;
constantReexports?: boolean | void;
enumerableModuleMeta?: boolean | void;
noIncompleteNsImportDetection?: boolean | void;
}Utilities for working with module metadata, validation, and checking module characteristics.
function hasExports(metadata: ModuleMetadata): boolean;
function isSideEffectImport(source: SourceModuleMetadata): boolean;
function isModule(path: NodePath): boolean;
interface ModuleMetadata {
exportName: string;
exportNameListName: null | string;
hasExports: boolean;
local: Map<string, LocalExportMetadata>;
source: Map<string, SourceModuleMetadata>;
stringSpecifiers: Set<string>;
}Support for transforming dynamic import expressions with various promise wrapping and deferring strategies.
function buildDynamicImport(
node: CallExpression | ImportExpression,
deferToThen: boolean,
wrapWithPromise: boolean,
builder: (specifier: Expression) => Expression
): Expression;Utilities for generating module names based on file paths and configuration options.
function getModuleName(
rootOpts: { filename?: string; filenameRelative?: string; sourceRoot?: string; },
pluginOpts: PluginOptions
): string | null;
interface PluginOptions {
moduleId?: string;
moduleIds?: boolean;
getModuleId?: (moduleName: string) => string | null | undefined;
moduleRoot?: string;
}