Babel helper functions for inserting module loads
npx @tessl/cli install tessl/npm-babel--helper-module-imports@7.27.0@babel/helper-module-imports provides essential helper functions for Babel plugins and transforms to programmatically insert import statements and module loads into JavaScript/TypeScript code during compilation. It offers a comprehensive ImportInjector class and convenience functions that handle the complexities of module import injection, including proper scoping, deduplication, and compatibility with both ES6 modules and CommonJS.
npm install @babel/helper-module-importsimport { ImportInjector, addDefault, addNamed, addNamespace, addSideEffect, isModule } from "@babel/helper-module-imports";For TypeScript users who need access to the ImportOptions type, it's typically available through the ImportInjector import or by importing from the internal modules (though this is not recommended for public API usage):
// Recommended: Use the ImportInjector which provides the same interface
import { ImportInjector } from "@babel/helper-module-imports";
// The ImportOptions type is used as Partial<ImportOptions> in function parameters
// and is typically inferred by TypeScript, so explicit imports are rarely neededFor CommonJS:
const { ImportInjector, addDefault, addNamed, addNamespace, addSideEffect, isModule } = require("@babel/helper-module-imports");import { addDefault, addNamed, isModule } from "@babel/helper-module-imports";
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
// Add a default import
const identifier = addDefault(path, "lodash", { nameHint: "lodash" });
// Add a named import
const namedIdentifier = addNamed(path, "debounce", "lodash", { nameHint: "debounce" });
// Check if the current file is a module
if (isModule(programPath)) {
// Use ES6 imports
} else {
// Use CommonJS requires
}@babel/helper-module-imports is built around several key components:
addDefault, addNamed, addNamespace, addSideEffect) for simple import injection operationsHigh-level functions for quickly adding imports without managing state. Perfect for simple Babel plugin operations where you need to inject a single import.
function addDefault(
path: NodePath,
importedSource: string,
opts?: Partial<ImportOptions>
): t.Identifier | t.MemberExpression | t.SequenceExpression;
function addNamed(
path: NodePath,
name: string,
importedSource: string,
opts?: Partial<ImportOptions>
): t.Identifier | t.MemberExpression | t.SequenceExpression;
function addNamespace(
path: NodePath,
importedSource: string,
opts?: Partial<ImportOptions>
): t.Identifier | t.MemberExpression | t.SequenceExpression;
function addSideEffect(
path: NodePath,
importedSource: string,
opts?: Partial<ImportOptions>
): t.Identifier | t.MemberExpression | t.SequenceExpression;Stateful class for managing multiple import operations with shared configuration. Ideal for complex Babel plugins that need to inject multiple imports with consistent settings.
class ImportInjector {
constructor(
path: NodePath,
importedSource?: string,
opts?: Partial<ImportOptions>
);
addDefault(importedSourceIn: string, opts: Partial<ImportOptions>): t.Identifier | t.MemberExpression | t.SequenceExpression;
addNamed(importName: string, importedSourceIn: string, opts: Partial<ImportOptions>): t.Identifier | t.MemberExpression | t.SequenceExpression;
addNamespace(importedSourceIn: string, opts: Partial<ImportOptions>): t.Identifier | t.MemberExpression | t.SequenceExpression;
addSideEffect(importedSourceIn: string, opts: Partial<ImportOptions>): t.Identifier | t.MemberExpression | t.SequenceExpression;
}Helper functions for module detection and compatibility checking.
function isModule(path: NodePath<t.Program>): boolean;interface ImportOptions {
/** The module being referenced */
importedSource: string | null;
/** Type of module: 'es6' or 'commonjs' (default: 'commonjs') */
importedType: "es6" | "commonjs";
/** Interop behavior for CommonJS modules */
importedInterop: "babel" | "node" | "compiled" | "uncompiled";
/** CommonJS interop in the loading environment */
importingInterop: "babel" | "node";
/** Force imported values to be live references */
ensureLiveReference: boolean;
/** Force calls to exclude context */
ensureNoContext: boolean;
/** Whether import should be loaded before or after existing imports */
importPosition: "before" | "after";
/** Hint for variable name generation */
nameHint?: string;
/** Control import statement ordering */
blockHoist?: number;
}The library throws errors in the following scenarios:
importPosition: "after" in non-module filesuncompiled interop)