CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--plugin-transform-modules-amd

This plugin transforms ES2015 modules to AMD

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

@babel/plugin-transform-modules-amd

This plugin transforms ES2015 modules to AMD (Asynchronous Module Definition) format. It converts ES6 import/export statements to AMD define() calls and provides comprehensive module transformation capabilities including dynamic import support, module interoperability, and configurable transformation options.

Package Information

  • Package Name: @babel/plugin-transform-modules-amd
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-transform-modules-amd

Core Imports

// Plugin registration (CommonJS)
const transformModulesAmd = require("@babel/plugin-transform-modules-amd");

// Plugin registration (ES modules)
import transformModulesAmd from "@babel/plugin-transform-modules-amd";

Basic Usage

Add to your Babel configuration:

// babel.config.js
module.exports = {
  plugins: [
    ["@babel/plugin-transform-modules-amd", {
      // Optional configuration
      allowTopLevelThis: false,
      strict: true,
      noInterop: false
    }]
  ]
};

Input ES6 module:

import { helper } from "./utils";
export const result = helper("data");

Output AMD module:

define(["exports", "./utils"], function (_exports, _utils) {
  "use strict";
  
  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.result = void 0;
  
  const result = _exports.result = (0, _utils.helper)("data");
});

Capabilities

Plugin Configuration

The main export is a Babel plugin factory function that accepts configuration options.

interface Options {
  /** Allow top-level `this` references (default: false) */
  allowTopLevelThis?: boolean;
  /** Import interop strategy for cross-module compatibility */
  importInterop?: "none" | "babel" | "node" | ((source: string, filename?: string) => "none" | "babel" | "node");
  /** Enable loose transformation mode (default: false) */
  loose?: boolean;
  /** Disable module interop transformations (default: false) */
  noInterop?: boolean;
  /** Enable strict mode in output (default: false) */
  strict?: boolean;
  /** Alternative name for strict mode */
  strictMode?: boolean;
}

interface BabelPlugin {
  name: string;
  pre?(): void;
  visitor: {
    [key: string]: any;
  };
}

/**
 * Babel plugin factory function
 * @param api - Babel API object with assertVersion method
 * @param options - Plugin configuration options
 * @returns Babel plugin object with transformation logic
 */
declare function plugin(api: any, options?: Options): BabelPlugin;

export default plugin;
export { Options };

Module Transformation

Transforms ES6 module syntax to AMD format:

  • Import statements → AMD dependency declarations in define() function
  • Export statements → module.exports assignments within define() callback
  • Dynamic imports → Promise-wrapped AMD require() calls
  • Module detection → Wraps non-modules in anonymous define() if dynamic imports are present

Dynamic Import Support

Converts ES6 dynamic import() expressions to AMD-compatible Promise-based require() calls:

Input:

import("./module").then(mod => mod.default());

Output:

new Promise((resolve, reject) =>
  require(["./module"], imported => resolve(imported), reject)
);

Interoperability Features

  • Module interop: Handles compatibility between ES6 and CommonJS modules
  • Namespace imports: Preserves namespace object structure in AMD format
  • Re-exports: Transforms export-from statements to proper AMD dependencies
  • Mixed module systems: Ensures proper integration with existing AMD, CommonJS, and ES6 modules

Configuration Options

allowTopLevelThis (boolean, default: false)

  • Allows references to this at the top level of modules
  • Useful for UMD builds where this refers to the global object

importInterop ("babel" | "node" | "none")

  • Controls how imports from CommonJS modules are handled
  • "babel": Use Babel's interop helpers (default)
  • "node": Use Node.js-style interop
  • "none": No interop transformation

loose (boolean, default: false)

  • Enables loose transformation mode for smaller output
  • Affects how module metadata and re-exports are handled

noInterop (boolean, default: false)

  • Disables all module interoperability transformations
  • Results in direct property access without helper functions

strict/strictMode (boolean, default: false)

  • Enables strict mode in the generated AMD modules
  • Adds "use strict"; directive to define() callback functions

Plugin Architecture

The plugin operates as a standard Babel plugin with:

  • Pre-hook: Sets plugin metadata flag for coordination with other module plugins
  • AST Visitors:
    • CallExpression|ImportExpression: Handles dynamic import transformations
    • Program.exit: Main module transformation logic
  • Helper Integration: Uses @babel/helper-module-transforms for core transformation logic
  • Template System: Uses AST templates for generating AMD wrapper code

Error Handling

The plugin includes validation for:

  • Babel version compatibility (requires Babel 7+)
  • Proper module syntax detection
  • Dynamic import expression validation
  • Configuration option validation

Common transformation scenarios are handled gracefully:

  • Non-module files (wrapped in anonymous define if needed)
  • Mixed import/export patterns
  • Circular dependency detection
  • Hoisted function declarations

docs

index.md

tile.json