Comparing version 0.1.0 to 0.1.1
@@ -7,2 +7,4 @@ var recast = require( 'recast' ), | ||
toAmd: function ( source, options ) { | ||
options = options || {}; | ||
var parsed = parse( source, options ); | ||
@@ -13,2 +15,4 @@ return generators.amd( parsed, options ); | ||
toCjs: function ( source, options ) { | ||
options = options || {}; | ||
var parsed = parse( source, options ); | ||
@@ -15,0 +19,0 @@ return generators.cjs( parsed, options ); |
@@ -7,9 +7,21 @@ var recast = require( 'recast' ); | ||
generated += 'define([' + imports.concat( 'exports' ).map( quote ).join( ',' ) + '], function (' + imports.map( getImportName ).concat( 'exports' ).join( ',' ) + ') {\n\n'; | ||
generated += 'define([' + | ||
( options.defaultOnly ? imports : imports.concat( 'exports' ) ).map( quote ).join( ',' ) + | ||
'], function (' + | ||
( options.defaultOnly ? imports.map( getImportName ) : imports.map( getImportName ).concat( 'exports' ) ).join( ',' ) + | ||
') {\n\n'; | ||
if ( options.defaultOnly ) { | ||
generated += 'var __export;\n\n'; | ||
} | ||
generated += recast.print( parsed.ast ).code; | ||
if ( options.defaultOnly ) { | ||
generated += '\nreturn __export;'; | ||
} | ||
generated += '\n\n});'; | ||
return generated; | ||
return generated.trim(); | ||
}; | ||
@@ -16,0 +28,0 @@ |
@@ -11,5 +11,13 @@ var recast = require( 'recast' ); | ||
if ( options.defaultOnly ) { | ||
generated += 'var __export;\n\n'; | ||
} | ||
generated += recast.print( parsed.ast ).code; | ||
return generated; | ||
if ( options.defaultOnly ) { | ||
generated += '\nmodule.exports = __export;'; | ||
} | ||
return generated.trim(); | ||
}; |
var recast = require( 'recast' ); | ||
module.exports = function ( source ) { | ||
var ast, body, numImports, importDeclarations, exportDeclarations, imports = [], exports = []; | ||
module.exports = function ( source, options ) { | ||
var ast, body, numImports, exportCount = 0, importDeclarations, exportDeclarations, imports = [], exports = []; | ||
@@ -39,2 +39,6 @@ ast = recast.parse( source.trim() ); | ||
if ( options.defaultOnly && node.kind !== 'default' ) { | ||
throw new Error( 'A named import was used in defaultOnly mode' ); | ||
} | ||
source = node.source.value; | ||
@@ -44,3 +48,9 @@ index = numImports - ( imports.push( source ) ); | ||
node.specifiers.forEach( function ( specifier ) { | ||
varDeclarations.push( 'var ' + specifier.id.name + ' = __imports_' + index + '.' + ( node.kind === 'default' ? 'default' : specifier.id.name) ); | ||
var declaration = 'var ' + specifier.id.name + ' = __imports_' + index; | ||
if ( !options.defaultOnly ) { | ||
declaration += '.' + ( node.kind === 'default' ? 'default' : specifier.id.name ); | ||
} | ||
varDeclarations.push( declaration ); | ||
}); | ||
@@ -55,9 +65,13 @@ | ||
index = numExports - ( exports.push( 'hmm' ) ); | ||
index = numExports - exportCount++; | ||
if ( node.declaration ) { | ||
declarations.push( 'exports.default = ' + recast.print( body.splice( nodeIndex + 1, 1 )[0] ).code ); | ||
declarations.push( ( options.defaultOnly ? '__export' : 'exports.default' ) + ' = ' + recast.print( body.splice( nodeIndex + 1, 1 )[0] ).code ); | ||
} | ||
else { | ||
if ( options.defaultOnly ) { | ||
throw new Error( 'A named export was used in defaultOnly mode' ); | ||
} | ||
node.specifiers.forEach( function ( specifier ) { | ||
@@ -64,0 +78,0 @@ declarations.push( 'exports.' + specifier.id.name + ' = ' + specifier.id.name + ';' ); |
{ | ||
"name": "esperanto", | ||
"description": "An easier way to convert ES6 modules to AMD and CommonJS", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Rich Harris", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
4590
125