CtrlK
BlogDocsLog inGet started
Tessl Logo

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

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

Pending
This version of the tile failed moderation
Malicious code detected in tile.json: This appears to be a typosquatting/supply chain attack. The package name 'tessl/npm-babel--helper-module-transforms' mimics the legitimate Babel package '@babel/helper-module-transforms' (note the double dash and different namespace). The 'describes' field references the real package 'pkg:npm/%40babel/helper-module-transforms@7.28.3' but with a slightly different version (7.28.1 vs 7.28.3), suggesting this is designed to confuse users into installing a malicious impersonator of a widely-used Babel dependency.
Overview
Eval results
Files

module-transformation.mddocs/

Module Transformation

Core functionality for rewriting ES6 module statements and preparing module initialization code. This is the primary API for transforming modules from ES6 syntax to target module formats.

Capabilities

Main Transformation Function

Performs all generic ES6 module rewriting needed to handle initial module processing.

/**
 * Perform all of the generic ES6 module rewriting needed to handle initial
 * module processing. This function will rewrite the majority of the given
 * program to reference the modules described by the returned metadata,
 * and returns a list of statements for use when initializing the module.
 */
function rewriteModuleStatementsAndPrepareHeader(
  path: NodePath<Program>,
  options: RewriteModuleStatementsAndPrepareHeaderOptions
): { meta: ModuleMetadata; headers: Statement[] };

interface RewriteModuleStatementsAndPrepareHeaderOptions {
  /** Name for the exported object (e.g., "exports" or "module.exports") */
  exportName?: string;
  /** Whether to use strict mode handling */
  strict: boolean;
  /** Allow `this` in top-level scope */
  allowTopLevelThis?: boolean;
  /** Add "use strict" directive */
  strictMode: boolean;
  /** Use loose transformation mode (deprecated) */
  loose?: boolean;
  /** Import interoperability strategy */
  importInterop?: ImportInterop;
  /** Disable interop helpers (deprecated, use importInterop: "none") */
  noInterop?: boolean;
  /** Lazy loading configuration */
  lazy?: Lazy;
  /** Function to determine wrapper payload for imports */
  getWrapperPayload?: (
    source: string,
    metadata: SourceModuleMetadata,
    importNodes: Node[]
  ) => unknown;
  /** Function to wrap import references */
  wrapReference?: (ref: Expression, payload: unknown) => Expression | null;
  /** Only handle ES namespace imports */
  esNamespaceOnly?: boolean;
  /** Source filename for module name generation */
  filename: string | undefined;
  /** Use constant re-exports (performance optimization) */
  constantReexports?: boolean | void;
  /** Make module metadata enumerable */
  enumerableModuleMeta?: boolean | void;
  /** Skip incomplete namespace import detection */
  noIncompleteNsImportDetection?: boolean | void;
}

type ImportInterop = 
  | "none"       // No interop helpers
  | "babel"      // Babel-style interop
  | "node"       // Node.js-style interop
  | ((source: string, filename?: string) => "none" | "babel" | "node");

type Lazy = boolean | string[] | ((source: string) => boolean);

Usage Example:

import { rewriteModuleStatementsAndPrepareHeader, isModule } from "@babel/helper-module-transforms";
import type { NodePath, Program } from "@babel/core";

function transformToCommonJS(programPath: NodePath<Program>) {
  if (!isModule(programPath)) {
    throw new Error("Cannot process module statements in a script");
  }

  // Transform the module with CommonJS-compatible settings
  const result = rewriteModuleStatementsAndPrepareHeader(programPath, {
    strict: false,
    strictMode: true,
    allowTopLevelThis: false,
    importInterop: "babel",
    filename: programPath.hub.file?.opts.filename,
    constantReexports: false,
    enumerableModuleMeta: true
  });

  // The result contains transformed metadata and initialization statements
  return {
    moduleMetadata: result.meta,
    initializationCode: result.headers
  };
}

Statement Hoisting

Ensures module initialization statements are hoisted above user code.

/**
 * Flag a set of statements as hoisted above all else so that module init
 * statements all run before user code.
 */
function ensureStatementsHoisted(statements: Statement[]): void;

Usage Example:

import { ensureStatementsHoisted } from "@babel/helper-module-transforms";

// After generating module initialization statements
const initStatements = generateModuleInitStatements();
ensureStatementsHoisted(initStatements);
// Now these statements will be executed before any user code

Interop Wrapping

Wraps import expressions with appropriate interop helpers based on module type.

/**
 * Given an expression for a standard import object, like "require('foo')",
 * wrap it in a call to the interop helpers based on the type.
 */
function wrapInterop(
  programPath: NodePath<Program>,
  expr: Expression,
  type: InteropType
): CallExpression | null;

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 imports

Usage Example:

import { wrapInterop } from "@babel/helper-module-transforms";
import { types as t } from "@babel/core";

// Wrap a require() call with appropriate interop helper
const requireCall = t.callExpression(t.identifier("require"), [t.stringLiteral("some-module")]);
const wrappedCall = wrapInterop(programPath, requireCall, "default");
// Results in: _interopRequireDefault(require("some-module"))

// Some interop types return null (no wrapping needed)
const noWrapCall = wrapInterop(programPath, requireCall, "none");
// Results in: null (use original expression)

Namespace Initialization

Creates runtime initialization statements for imported namespaces.

/**
 * Create the runtime initialization statements for a given requested source.
 * These will initialize all of the runtime import/export logic that
 * can't be handled statically by the statements created by
 * buildExportInitializationStatements().
 */
function buildNamespaceInitStatements(
  metadata: ModuleMetadata,
  sourceMetadata: SourceModuleMetadata,
  constantReexports?: boolean | void,
  wrapReference?: (ref: Identifier, payload: unknown) => Expression | null
): Statement[];

Internal Note: This function generates the runtime code needed to properly initialize namespace objects and handle re-exports that cannot be statically determined.

AST Rewriting

Utility for rewriting this expressions in module scope.

/**
 * Rewrite all `this` references in the top-level scope to be `void 0` (undefined).
 * This is necessary because modules have undefined `this` in the top level.
 */
function rewriteThis(programPath: NodePath): void;

Usage Example:

import { rewriteThis } from "@babel/helper-module-transforms";

// Rewrite `this` to `undefined` in module scope
rewriteThis(programPath);

Install with Tessl CLI

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

docs

dynamic-imports.md

index.md

lazy-loading.md

module-metadata.md

module-names.md

module-transformation.md

tile.json