or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

dynamic-imports.mdindex.mdlazy-loading.mdmodule-metadata.mdmodule-names.mdmodule-transformation.md
tile.json

tessl/npm-babel--helper-module-transforms

Babel helper functions for implementing ES6 module transformations and interoperability between different module formats

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/helper-module-transforms@7.28.x

To install, run

npx @tessl/cli install tessl/npm-babel--helper-module-transforms@7.28.0

index.mddocs/

@babel/helper-module-transforms

@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.

Package Information

  • Package Name: @babel/helper-module-transforms
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @babel/helper-module-transforms

Core Imports

import {
  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");

Basic Usage

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
  };
}

Architecture

@babel/helper-module-transforms is built around several key components:

  • Module Statement Rewriting: Core functionality for transforming import/export statements into target module format
  • Metadata Processing: Comprehensive analysis and processing of module imports, exports, and dependencies
  • Interoperability Handling: Support for different module formats and interop strategies between ES modules and CommonJS
  • Dynamic Import Support: Utilities for transforming dynamic import expressions with various wrapping strategies
  • Lazy Loading: Support for lazy module loading to optimize bundle size and loading performance
  • Type Safety: Full TypeScript support with comprehensive type definitions for all APIs

Capabilities

Module Statement Transformation

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;
}

Module Transformation

Module Metadata and Validation

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>;
}

Module Metadata

Dynamic Import Utilities

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;

Dynamic Imports

Module Name Generation

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;
}

Module Names