babel/doc/modules.md
2014-11-29 11:31:34 -08:00

4.3 KiB

Modules

Usage

CLI

$ 6to5 --modules common script.js

Node

var to5 = require("6to5");
to5.transform('import "foo";', { modules: "common" });

Formats

Common (Default)

$ 6to5 --modules common

In

import "foo";

import foo from "foo";
import * as foo from "foo";

import {bar} from "foo";
import {foo as bar} from "foo";

export {test};
export var test = 5;

export default test;

Out

require("foo");

var foo = require("foo").default;
var foo = require("foo");

var bar = require("foo").bar;
var bar = require("foo").foo;

exports.test = test;
var test = 5; exports.test = test;

exports.default = test;

Common interop

$ 6to5 --modules commonInterop

In

import "foo";

import foo from "foo";
import * as foo from "foo";

import {bar} from "foo";
import {foo as bar} from "foo";

export {test};
export var test = 5;

export default test;

Out

var _interopRequire = function (obj) {
  return obj && (obj["default"] || obj);
};

require("foo");

var foo = _interopRequire(require("foo"));
var foo = require("foo");

var bar = require("foo").bar;
var bar = require("foo").foo;

exports.test = test;
var test = exports.test = 5;

exports["default"] = test;

module.exports behaviour

If there exist no other non-default exports then default exports are exported as module.exports instead of exports.default.

In

export default function foo() {

}

Out

module.exports = foo;

function foo() {

}

AMD

$ 6to5 --modules amd

In

import foo from "foo";

export function bar() {
  return foo("foobar");
}

Out

define(["exports", "foo"], function (exports, _foo) {
  exports.bar = bar;

  var foo = _foo.default;

  function bar() {
    return foo("foobar");
  }
});

You can optionally specify to include the module id (using the --amd-module-id argument):

define("filename", ["exports", "foo"], function (exports, _foo) {})

UMD

$ 6to5 --modules umd

In

import foo from "foo";

export function bar() {
  return foo("foobar");
}

Out

(function (factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "foo"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("foo"));
  }
})(function (exports) {
  exports.bar = bar;

  var foo = _foo.default;

  function bar() {
    return foo("foobar");
  }
});

Ignore

$ 6to5 --modules ignore

In

import foo from "foo";

export function bar() {
  return foo("foobar");
}

Out

function bar() {
  return foo("foobar");
}

System

$ 6to5 --modules system

In

import foo from "foo";

export function bar() {
  return foo("foobar");
}

Out

System.register("bar", ["foo"], function (_export) {
  "use strict";

  var __moduleName = "bar";

  var foo;
  function bar() {
    return foo("foobar");
  }
  return {
    setters: [function (m) {
      foo = m.default;
    }],
    execute: function () {
      _export("bar", bar);
    }
  };
});

Custom

You can alternatively specify module names instead of one of the built-in types.

$ 6to5 --modules custom-module-formatter

node_modules/custom-module-formatter/index.js

module.exports = ModuleFormatter;

function ModuleFormatter() {

}

ModuleFormatter.prototype.transform = function (ast) {
  // this is ran after all transformers have had their turn at modifying the ast
  // feel free to modify this however
};

ModuleFormatter.prototype.import = function (node, nodes) {
  // node is an ImportDeclaration
};

ModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
  // specifier is an ImportSpecifier
  // node is an ImportDeclaration
};

ModuleFormatter.prototype.export = function (node, nodes) {
  // node is an ExportDeclaration
};

ModuleFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
  // specifier is an ExportSpecifier
  // node is an ExportDeclaration
};