Helper functions for module detection and compatibility checking.
Utility function to check if a file qualifies as a module by examining its sourceType.
/**
* Check if a file qualifies as a module
* @param path - Program NodePath to examine
* @returns true if the file is a module (sourceType === "module"), false otherwise
*/
function isModule(path: NodePath<t.Program>): boolean;Usage Examples:
import { isModule } from "@babel/helper-module-imports";
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
// In a Babel plugin
export default function myPlugin() {
return {
visitor: {
Program(path: NodePath<t.Program>) {
if (isModule(path)) {
// File is an ES6 module - can use import/export
console.log("Using ES6 module syntax");
} else {
// File is a script - must use require/module.exports
console.log("Using CommonJS syntax");
}
}
}
};
}
// Conditional import injection
function addImportBasedOnModuleType(path: NodePath, source: string) {
const programPath = path.find(p => p.isProgram()) as NodePath<t.Program>;
if (isModule(programPath)) {
// Use ES6 imports
return addDefault(path, source, { importedType: "es6" });
} else {
// Use CommonJS
return addDefault(path, source, { importedType: "commonjs" });
}
}The isModule function determines module type by checking the sourceType property of the Program node:
// Internally equivalent to:
function isModule(path: NodePath<t.Program>): boolean {
return path.node.sourceType === "module";
}Module Types:
"module": File uses ES6 module syntax (import/export)
import and export statementsimportPosition: "after".mjs files, .js files with "type": "module" in package.json, TypeScript files"script": File uses script/CommonJS syntax
require() and module.exportsimportPosition: "after".js files, CommonJS modulesAll import functions automatically detect module type and adjust behavior accordingly:
import { isModule, addDefault } from "@babel/helper-module-imports";
// Manual detection
if (isModule(programPath)) {
// Explicitly use ES6
const id = addDefault(path, "lodash", { importedType: "es6" });
} else {
// Explicitly use CommonJS
const id = addDefault(path, "lodash", { importedType: "commonjs" });
}
// Automatic detection (recommended)
const id = addDefault(path, "lodash"); // Automatically chooses appropriate typeexport default function babelPlugin(opts = {}) {
return {
visitor: {
Program(path) {
const moduleType = isModule(path) ? "es6" : "commonjs";
// Store module type for use in other visitors
this.moduleType = moduleType;
this.isModule = isModule(path);
},
CallExpression(path) {
if (this.isModule) {
// Module-specific logic
} else {
// CommonJS-specific logic
}
}
}
};
}function addLibraryImport(path: NodePath, libraryName: string) {
const programPath = path.find(p => p.isProgram()) as NodePath<t.Program>;
const options = {
importedType: isModule(programPath) ? "es6" : "commonjs",
importedInterop: isModule(programPath) ? "compiled" : "babel"
} as const;
return addDefault(path, libraryName, options);
}function safeAddImport(path: NodePath, source: string, position: "before" | "after" = "before") {
const programPath = path.find(p => p.isProgram()) as NodePath<t.Program>;
// Prevent "after" position in non-modules
if (position === "after" && !isModule(programPath)) {
console.warn('Import position "after" is only supported in modules, using "before"');
position = "before";
}
return addDefault(path, source, {
importPosition: position,
importedType: isModule(programPath) ? "es6" : "commonjs"
});
}import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
// Function signature
function isModule(path: NodePath<t.Program>): boolean;
// Related types
type SourceType = "module" | "script";
interface Program extends t.Node {
sourceType: SourceType;
body: t.Statement[];
// ... other properties
}.mjs files and package.json "type": "module"<script> and <script type="module">