-
Notifications
You must be signed in to change notification settings - Fork 21
Home
Rich Harris edited this page Aug 18, 2014
·
9 revisions
Install esperanto from npm:
npm install esperanto
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() ) );
});
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 tofalse
-
addUseStrict
(boolean) - whether to add a 'use strict' pragma. Defaults totrue
-
indent
(string) - the string to insert before each line of the code inside thedefine()
block. If omitted, this will be guessed from the input code; if the result is ambiguous, esperanto will default to tabs.
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()
.
See [this page](default only mode) for an explanation of defaultOnly
.