transpile

Transpiles JavaScript modules from one format to another.

Transpiles JavaScript modules from one format to another.

It supports from:

  • es6,
  • cjs,
  • amd,
  • steal

to

  • amd,
  • steal,
  • cjs.

Currently, it can not transpile to ES6 module syntax.

> npm install transpile --save-dev

transpile.to transpiles from one format to another format. transpile.able lets you know if a transpile is possible.

Formats are specified by strings like:

  • "es6" - ES6 Module syntax like import Point from "math";
  • "cjs" - CommonJS syntax like var _ = require('underscore');
  • "amd" - Asynchronous Module Definition syntax like define(['jquery'],function($){});
  • "steal" - steal syntax like steal('jquery', function($){})

Transpiles from the load's format to the specified format. If the load does not specify a format, "es6" modules are assumed. Returns the transpiled source.

Example:

var transpile = require('transpile');
var res = transpile.to({
  name: "my/module",
  source: "var foo = require('foo')",
  metadata: {format: "cjs"}
}, "amd")
 
res //-> "define("my/module", function(require, exports, module) { ... " 

A load is an object in the shape of an ES6 Load Record like:

{
  name: "moduleName",
  source: "source code",
  metadata: {format: "formatName"}
}
  • normalizeMap Object<moduleName,moduleName> - A mapping module names that will be used to replace dependency names in the transpiled result.
  • normalize function(name, currentName, address) -> String - A function that can be used to change moduleNames that are written in the transpiled result.
  • namedDefines Boolean=false - Set to true to insert named defines.
  • transpiler String=traceur - Set which ES6 transpiler to use. Valid options are traceur or 6to5 with traceur being the default.

Returns the path used to transpile from fromFormat to toFormat. If transpiling is not possible, null will be returned.

Example:

var res = transpile.able("steal","cjs");
res //-> ["steal","amd"]; 

This means that a module will be converted from "steal" to "amd" and then to "cjs".

> npm test