Skip to content
Rich Harris edited this page Aug 18, 2014 · 9 revisions

Esperanto

Getting started

Install esperanto from npm:

npm install esperanto

Usage

var fs = require( 'fs' );
var esperanto = require( 'esperanto' );

fs.readFile( 'path/to/es6/modules/foo.js', function ( err, result ) {
  if ( err ) throw err;

  fs.writeFile( 'amd/foo.js', esperanto.toAmd( result.toString() ) );
  fs.writeFile( 'cjs/foo.js', esperanto.toCjs( result.toString() ) );
});

esperanto.toAmd( code[, options] )

This method returns an AMD module:

// input
esperanto.toAmd( 'export default function foo () { alert( 'foo!' ); }' )

// output
define(function () {

  'use strict';
	
  return function foo () { alert( 'foo!' ); };

});

The second argument, if used, has three optional properties:

  • defaultOnly (boolean) - whether to run esperanto in default only mode. Defaults to false
  • addUseStrict (boolean) - whether to add a 'use strict' pragma. Defaults to true
  • indent (string) - the string to insert before each line of the code inside the define() block. If omitted, this will be guessed from the input code; if the result is ambiguous, esperanto will default to tabs.

esperanto.toCjs( string[, options] )

As above, except it generates a CommonJS (a.k.a. node) module:

// input
esperanto.toAmd( 'export default function foo () { alert( 'foo!' ); }' )

// output
module.exports = function foo () { alert( 'foo!' ); };

In this case, the addUseStrict and indent options don't apply. The defaultOnly property is the same as with esperanto.toAmd().

Using esperanto on the command line

Coming soon!

defaultOnly mode

See [this page](default only mode) for an explanation of defaultOnly.

Clone this wiki locally