or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

convenience-functions.mdimport-injector.mdindex.mdutilities.md
tile.json

utilities.mddocs/

Utilities

Helper functions for module detection and compatibility checking.

Capabilities

isModule Function

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

Module Type Detection

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)

    • Supports import and export statements
    • Has module scope (not global)
    • Can use importPosition: "after"
    • Examples: .mjs files, .js files with "type": "module" in package.json, TypeScript files
  • "script": File uses script/CommonJS syntax

    • Uses require() and module.exports
    • Has global scope
    • Cannot use importPosition: "after"
    • Examples: Traditional .js files, CommonJS modules

Integration with Import Functions

All 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 type

Common Usage Patterns

Plugin Configuration

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

Conditional Interop

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

Error Prevention

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

Type Definitions

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
}

Compatibility Notes

  • Babel 7+: Fully supported
  • TypeScript: Works with all TypeScript file types
  • Node.js ESM: Correctly detects .mjs files and package.json "type": "module"
  • Browser: Works with both <script> and <script type="module">
  • Webpack: Compatible with Webpack's module resolution