Comparing version 0.5.10 to 0.6.0
@@ -58,2 +58,4 @@ (function (global, factory) { | ||
var envDepth = 0; | ||
estraverse__default.traverse( ast, { | ||
@@ -72,2 +74,8 @@ enter: function ( node ) { | ||
case 'FunctionDeclaration': | ||
envDepth++; | ||
// fallthrough | ||
case 'ArrowFunctionExpression': | ||
if ( node.id ) { | ||
@@ -117,2 +125,8 @@ addToScope( node ); | ||
break; | ||
case 'ThisExpression': | ||
if (envDepth === 0) { | ||
node._topLevel = true; | ||
} | ||
break; | ||
} | ||
@@ -124,3 +138,11 @@ }, | ||
case 'FunctionDeclaration': | ||
envDepth--; | ||
// fallthrough | ||
case 'ArrowFunctionExpression': | ||
scope = scope.parent; | ||
break; | ||
@@ -698,7 +720,3 @@ | ||
body.trim() | ||
.prepend( "'use strict';\n\n" ) | ||
.indent() | ||
.prepend( '(function () {\n\n' ) | ||
.append( '\n\n}).call(global);' ); | ||
body.prepend( "'use strict';\n\n" ).trimLines() | ||
@@ -708,41 +726,54 @@ return packageResult( body, options, 'toCjs' ); | ||
function standaloneUmdIntro ( options, indentStr ) { | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var intro = | ||
(("(function (factory) {\ | ||
\n !(typeof exports === 'object' && typeof module !== 'undefined') &&\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + "factory) :\ | ||
\n factory()\ | ||
\n}(function () { 'use strict';\ | ||
\n\ | ||
\n"); | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function defaultUmdIntro ( options, indentStr ) { | ||
var intro, amdName, needsGlobal, amdDeps, cjsDeps, globalDeps, args, cjsDefine, globalDefine, nonAMDDefine; | ||
var hasExports = options.hasExports; | ||
amdName = options.amdName ? (("'" + (options.amdName)) + "', ") : ''; | ||
needsGlobal = options.hasImports || options.hasExports; | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var amdDeps = options.importPaths.length > 0 ? | ||
'[' + options.importPaths.map( quote ).join( ', ' ) + '], ' : | ||
''; | ||
var cjsDeps = options.importPaths.map( req ).join( ', ' ); | ||
var globalDeps = options.importNames.map( globalify ).join( ', ' ); | ||
var args = options.args.join( ', ' ); | ||
amdDeps = options.importPaths.map( quote ).join( ', ' ); | ||
cjsDeps = options.importPaths.map( req ).join( ', ' ); | ||
globalDeps = options.importNames.map( globalify ).join( ', ' ); | ||
args = ( options.args || options.importNames ).join( ', ' ); | ||
var cjsExport = | ||
(hasExports ? 'module.exports = ' : '') + (("factory(" + cjsDeps) + ")"); | ||
cjsDefine = options.hasExports ? | ||
(("module.exports = factory(" + cjsDeps) + ")") : | ||
(("factory(" + cjsDeps) + ")"); | ||
var globalExport = | ||
(hasExports ? (("global." + (options.name)) + " = ") : '') + (("factory(" + globalDeps) + ")"); | ||
globalDefine = options.hasExports ? | ||
(("global." + (options.name)) + (" = factory(" + globalDeps) + ")") : | ||
(("factory(" + globalDeps) + ")"); | ||
nonAMDDefine = cjsDefine === globalDefine ? globalDefine : | ||
(("typeof exports === 'object' ? " + cjsDefine) + (" :\n\t" + globalDefine) + ""); | ||
intro = | ||
(("(function (" + (needsGlobal ? 'global, ' : '')) + ("factory) {\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + (amdDeps ? '[' + amdDeps + '], ' : '')) + ("factory) :\ | ||
\n " + nonAMDDefine) + ("\ | ||
\n}(" + (needsGlobal ? 'this, ' : '')) + ("function (" + args) + ") { 'use strict';\ | ||
var intro = | ||
(("(function (global, factory) {\ | ||
\n typeof exports === 'object' && typeof module !== 'undefined' ? " + cjsExport) + (" :\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + amdDeps) + ("factory) :\ | ||
\n " + globalExport) + ("\ | ||
\n}(this, function (" + args) + ") { 'use strict';\ | ||
\n\ | ||
\n").replace( /\t/g, indentStr ); | ||
\n"); | ||
return intro; | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function umd__umd ( mod, body, options ) { | ||
var importNames = [], | ||
importPaths = [], | ||
intro, | ||
i; | ||
var importNames = []; | ||
var importPaths = []; | ||
@@ -753,36 +784,39 @@ if ( !options.name ) { | ||
// ensure empty imports are at the end | ||
reorderImports( mod.imports ); | ||
var hasImports = mod.imports.length > 0; | ||
var hasExports = mod.exports.length > 0; | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
// ensure empty imports are at the end | ||
reorderImports( mod.imports ); | ||
if ( x.name ) { | ||
importNames[i] = x.name; | ||
} | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
body.remove( x.start, x.next ); | ||
}); | ||
if ( x.name ) { | ||
importNames[i] = x.name; | ||
} | ||
transformExportDeclaration( mod.exports[0], body ); | ||
body.remove( x.start, x.next ); | ||
}); | ||
intro = defaultUmdIntro({ | ||
hasImports: mod.imports.length > 0, | ||
hasExports: mod.exports.length > 0, | ||
transformExportDeclaration( mod.exports[0], body ); | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
intro = defaultUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
args: importNames, | ||
}, body.getIndentString() ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, mod.body.indentStr ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n}));' ); | ||
body.trim() | ||
.prepend( "'use strict';\n\n" ) | ||
.trim() | ||
.indent() | ||
.prepend( intro ) | ||
.append( '\n\n}));' ); | ||
return packageResult( body, options, 'toUmd' ); | ||
@@ -993,2 +1027,7 @@ } | ||
rewriteIdentifiers( body, node, identifierReplacements, scope ); | ||
// Replace top-level this with undefined ES6 8.1.1.5.4 | ||
if ( node.type === 'ThisExpression' && node._topLevel ) { | ||
body.replace( node.start, node.end, 'undefined' ); | ||
} | ||
}, | ||
@@ -1116,5 +1155,5 @@ | ||
body.trim().indent({ | ||
exclude: mod.ast._templateLiteralRanges | ||
}).prepend( options.intro ).trim().append( options.outro ); | ||
if ( options.intro && options.outro ) { | ||
body.indent().prepend( options.intro ).trimLines().append( options.outro ); | ||
} | ||
;$D$1 = void 0} | ||
@@ -1159,3 +1198,3 @@ | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, body.indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
@@ -1170,5 +1209,2 @@ transformBody( mod, body, { | ||
var intro = '(function () {\n\n\t\'use strict\';\n\n'; | ||
var outro = '\n\n}).call(global);'; | ||
function strictMode_cjs__cjs ( mod, body, options ) { | ||
@@ -1192,7 +1228,7 @@ var importBlock; | ||
transformBody( mod, body, { | ||
intro: intro.replace( /\t/g, body.indentStr ), | ||
header: importBlock, | ||
outro: outro | ||
}); | ||
body.prepend( "'use strict';\n\n" ).trimLines() | ||
return packageResult( body, options, 'toCjs' ); | ||
@@ -1202,44 +1238,37 @@ } | ||
function strictUmdIntro ( options, indentStr ) { | ||
var intro, amdName, needsGlobal, defaultsBlock = '', amdDeps, cjsDeps, globalDeps, args, cjsDefine, globalDefine, nonAMDDefine; | ||
var hasExports = options.hasExports; | ||
amdName = options.amdName ? (("'" + (options.amdName)) + "', ") : ''; | ||
needsGlobal = options.hasImports || options.hasExports; | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var amdDeps = options.hasExports || options.importPaths.length > 0 ? | ||
'[' + | ||
( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths ).map( quote ).join( ', ' ) + | ||
'], ' : | ||
''; | ||
var cjsDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths.map( req ) ).join( ', ' ); | ||
var globalDeps = ( options.hasExports ? [ (("(global." + (options.name)) + " = {})") ] : [] ) | ||
.concat( options.importNames.map( globalify ) ).join( ', ' ); | ||
var args = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importNames ).join( ', ' ); | ||
amdDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths ).map( quote ).join( ', ' ); | ||
cjsDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths.map( req ) ).join( ', ' ); | ||
globalDeps = ( options.hasExports ? [ options.name ] : [] ).concat( options.importNames ).map( globalify ).join( ', ' ); | ||
args = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importNames ).join( ', ' ); | ||
var defaultsBlock = ''; | ||
if ( options.externalDefaults && options.externalDefaults.length > 0 ) { | ||
defaultsBlock = options.externalDefaults.map( function(name ) | ||
{return (("\tvar " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");")} | ||
).join( '\n' ) + '\n\n'; | ||
).join('\n') + '\n\n'; | ||
} | ||
cjsDefine =(("factory(" + cjsDeps) + ")"); | ||
globalDefine = options.hasExports ? | ||
(("(global." + (options.name)) + (" = {}, factory(" + globalDeps) + "))") : | ||
(("factory(" + globalDeps) + ")"); | ||
nonAMDDefine = cjsDefine === globalDefine ? globalDefine : | ||
(("typeof exports === 'object' ? " + cjsDefine) + (" :\n\t" + globalDefine) + ""); | ||
intro = | ||
(("(function (" + (needsGlobal ? 'global, ' : '')) + ("factory) {\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + (amdDeps ? '[' + amdDeps + '], ' : '')) + ("factory) :\ | ||
\n " + nonAMDDefine) + ("\ | ||
\n}(" + (needsGlobal ? 'this, ' : '')) + ("function (" + args) + (") { 'use strict';\ | ||
var intro = | ||
(("(function (global, factory) {\ | ||
\n typeof exports === 'object' && typeof module !== 'undefined' ? factory(" + cjsDeps) + (") :\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + amdDeps) + ("factory) :\ | ||
\n factory(" + globalDeps) + (")\ | ||
\n}(this, function (" + args) + (") { 'use strict';\ | ||
\n\ | ||
\n" + defaultsBlock) + "").replace( /\t/g, indentStr ); | ||
\n" + defaultsBlock) + "") | ||
return intro; | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function strictMode_umd__umd ( mod, body, options ) {var $D$3; | ||
var importPaths, | ||
importNames, | ||
intro; | ||
function strictMode_umd__umd ( mod, body, options ) { | ||
if ( !options.name ) { | ||
@@ -1251,15 +1280,22 @@ throw new Error( 'You must supply a `name` option for UMD modules' ); | ||
importPaths = ($D$3 = getImportSummary( mod ))[0], importNames = $D$3[1], $D$3; | ||
var importPaths = (importNames = getImportSummary( mod ))[0], importNames = importNames[1]; | ||
intro = strictUmdIntro({ | ||
hasImports: mod.imports.length > 0, | ||
hasExports: mod.exports.length > 0, | ||
var hasImports = mod.imports.length > 0; | ||
var hasExports = mod.exports.length > 0; | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
intro = strictUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
}, body.getIndentString() ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, body.indentStr ); | ||
transformBody( mod, body, { | ||
@@ -1271,3 +1307,3 @@ intro: intro, | ||
return packageResult( body, options, 'toUmd' ); | ||
;$D$3 = void 0} | ||
} | ||
@@ -1289,19 +1325,14 @@ var strictMode = { | ||
function defaultsMode_amd__amd ( bundle, body, options ) { | ||
var intro, | ||
indentStr, | ||
defaultName; | ||
indentStr = body.getIndentString(); | ||
if ( defaultName = bundle.entryModule.identifierReplacements.default ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + defaultName) + ";") ); | ||
var defaultName = bundle.entryModule.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nreturn " + defaultName) + ";") ); | ||
} | ||
intro = defaultsMode_amd__introTemplate({ | ||
var intro = defaultsMode_amd__introTemplate({ | ||
amdName: options.amdName ? (("'" + (options.amdName)) + "', ") : '', | ||
amdDeps: bundle.externalModules.length ? '[' + bundle.externalModules.map( quoteId ).join( ', ' ) + '], ' : '', | ||
names: bundle.externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ] + '__default'} ).join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n});' ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
@@ -1315,13 +1346,5 @@ } | ||
function defaultsMode_cjs__cjs ( bundle, body, options ) { | ||
var importBlock, | ||
x, | ||
intro, | ||
indentStr, | ||
defaultName; | ||
indentStr = body.getIndentString(); | ||
importBlock = bundle.externalModules.map( function(x ) { | ||
var importBlock = bundle.externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ]; | ||
return indentStr + (("var " + name) + ("__default = require('" + (x.id)) + "');"); | ||
return (("var " + name) + ("__default = require('" + (x.id)) + "');"); | ||
}).join( '\n' ); | ||
@@ -1333,9 +1356,9 @@ | ||
if ( defaultName = bundle.entryModule.identifierReplacements.default ) { | ||
body.append( (("\n\n" + indentStr) + ("module.exports = " + defaultName) + ";") ); | ||
var defaultName = bundle.entryModule.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nmodule.exports = " + defaultName) + ";") ); | ||
} | ||
intro = '(function () {\n\n' + indentStr + "'use strict';\n\n"; | ||
body.prepend("'use strict';\n\n").trimLines(); | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
@@ -1350,27 +1373,32 @@ } | ||
var entry = bundle.entryModule; | ||
var indentStr = body.getIndentString(); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
var hasImports = bundle.externalModules.length > 0; | ||
var hasExports = entry.exports.length > 0; | ||
var intro = defaultUmdIntro({ | ||
hasImports: bundle.externalModules.length > 0, | ||
hasExports: entry.exports.length > 0, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
args: importNames.map( function(name ) {return name + '__default'} ), | ||
var defaultName = entry.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nreturn " + defaultName) + ";") ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, indentStr ); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
body.prepend( intro ).trim(); | ||
var defaultName; | ||
if ( ( defaultName = entry.identifierReplacements.default ) ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + defaultName) + ";") ); | ||
intro = defaultUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
args: importNames.map( function(name ) {return name + '__default'} ), | ||
}, body.getIndentString() ); | ||
} | ||
body.append('\n\n}));'); | ||
body.indent().prepend( intro ).trimLines().append('\n\n}));'); | ||
@@ -1406,23 +1434,19 @@ return packageResult( body, options, 'toUmd', true ); | ||
function getExportBlock ( entry, indentStr ) { | ||
function getExportBlock ( entry ) { | ||
var name = entry.identifierReplacements.default; | ||
return indentStr + (("exports['default'] = " + name) + ";"); | ||
return (("exports['default'] = " + name) + ";"); | ||
} | ||
var builders_strictMode_amd__introTemplate; | ||
var builders_strictMode_amd__introTemplate = template( 'define(<%= amdName %><%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_amd__amd ( bundle, body, options ) { | ||
var externalDefaults = getExternalDefaults( bundle ), | ||
defaultsBlock, | ||
entry = bundle.entryModule, | ||
importIds = bundle.externalModules.map( getId ), | ||
importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ), | ||
intro, | ||
indentStr; | ||
var externalDefaults = getExternalDefaults( bundle ); | ||
var entry = bundle.entryModule; | ||
indentStr = body.getIndentString(); | ||
var importIds = bundle.externalModules.map( getId ); | ||
var importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ); | ||
if ( externalDefaults.length ) { | ||
defaultsBlock = externalDefaults.map( function(name ) { | ||
return indentStr + (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
var defaultsBlock = externalDefaults.map( function(name ) { | ||
return (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
}).join( '\n' ); | ||
@@ -1438,33 +1462,26 @@ | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
} | ||
intro = builders_strictMode_amd__introTemplate({ | ||
var intro = builders_strictMode_amd__introTemplate({ | ||
amdName: options.amdName ? (("'" + (options.amdName)) + "', ") : '', | ||
amdDeps: importIds.length ? '[' + importIds.map( quote ).join( ', ' ) + '], ' : '', | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n});' ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
} | ||
builders_strictMode_amd__introTemplate = template( 'define(<%= amdName %><%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_cjs__cjs ( bundle, body, options ) { | ||
var externalDefaults = getExternalDefaults( bundle ), | ||
importBlock, | ||
entry = bundle.entryModule, | ||
intro, | ||
indentStr; | ||
var externalDefaults = getExternalDefaults( bundle ); | ||
var entry = bundle.entryModule; | ||
indentStr = body.getIndentString(); | ||
importBlock = bundle.externalModules.map( function(x ) { | ||
var importBlock = bundle.externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ], | ||
statement = (("" + indentStr) + ("var " + name) + (" = require('" + (x.id)) + "');"); | ||
statement = (("var " + name) + (" = require('" + (x.id)) + "');"); | ||
if ( ~externalDefaults.indexOf( name ) ) { | ||
statement += (("\n" + indentStr) + ("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
statement += (("\nvar " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
} | ||
@@ -1480,8 +1497,7 @@ | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
intro = '(function () {\n\n' + indentStr + "'use strict';\n\n"; | ||
body.prepend("'use strict';\n\n").trimLines(); | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
@@ -1496,26 +1512,31 @@ } | ||
var entry = bundle.entryModule; | ||
var indentStr = body.getIndentString(); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
var hasImports = bundle.externalModules.length > 0; | ||
var hasExports = entry.exports.length > 0; | ||
var intro = strictUmdIntro({ | ||
hasImports: bundle.externalModules.length > 0, | ||
hasExports: entry.exports.length > 0, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
externalDefaults: getExternalDefaults( bundle ), | ||
if ( hasExports && entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, indentStr ); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
body.prepend( intro ).trim(); | ||
if ( entry.exports.length && entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
intro = strictUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
externalDefaults: getExternalDefaults( bundle ), | ||
amdName: options.amdName, | ||
name: options.name, | ||
}, body.getIndentString() ); | ||
} | ||
body.append('\n\n}));'); | ||
body.indent().prepend( intro ).trimLines().append('\n\n}));'); | ||
@@ -1522,0 +1543,0 @@ return packageResult( body, options, 'toUmd', true ); |
@@ -49,2 +49,4 @@ (function () { | ||
var envDepth = 0; | ||
estraverse__default.traverse( ast, { | ||
@@ -63,2 +65,8 @@ enter: function ( node ) { | ||
case 'FunctionDeclaration': | ||
envDepth++; | ||
// fallthrough | ||
case 'ArrowFunctionExpression': | ||
if ( node.id ) { | ||
@@ -108,2 +116,8 @@ addToScope( node ); | ||
break; | ||
case 'ThisExpression': | ||
if (envDepth === 0) { | ||
node._topLevel = true; | ||
} | ||
break; | ||
} | ||
@@ -115,3 +129,11 @@ }, | ||
case 'FunctionDeclaration': | ||
envDepth--; | ||
// fallthrough | ||
case 'ArrowFunctionExpression': | ||
scope = scope.parent; | ||
break; | ||
@@ -1020,2 +1042,7 @@ | ||
rewriteIdentifiers( body, node, identifierReplacements, scope ); | ||
// Replace top-level this with undefined ES6 8.1.1.5.4 | ||
if ( node.type === 'ThisExpression' && node._topLevel ) { | ||
body.replace( node.start, node.end, 'undefined' ); | ||
} | ||
}, | ||
@@ -1119,2 +1146,3 @@ | ||
// exports available, using Object.defineProperty | ||
var indentStr = body.getIndentString(); | ||
if ( mod._exportsNamespace ) { | ||
@@ -1127,7 +1155,7 @@ var prefix = bundle.uniqueNames[ mod.id ], | ||
if ( x.hasDeclaration ) { | ||
namespaceExports.push( body.indentStr + (("get " + (x.name)) + (" () { return " + (identifierReplacements[x.name])) + "; }") ); | ||
namespaceExports.push( indentStr + (("get " + (x.name)) + (" () { return " + (identifierReplacements[x.name])) + "; }") ); | ||
} | ||
else if ( x.isDefault ) { | ||
namespaceExports.push( body.indentStr + (("get default () { return " + (identifierReplacements.default)) + "; }") ); | ||
namespaceExports.push( indentStr + (("get default () { return " + (identifierReplacements.default)) + "; }") ); | ||
} | ||
@@ -1137,3 +1165,3 @@ | ||
x.specifiers.forEach( function(s ) { | ||
namespaceExports.push( body.indentStr + (("get " + (s.name)) + (" () { return " + (s.name)) + "; }") ); | ||
namespaceExports.push( indentStr + (("get " + (s.name)) + (" () { return " + (s.name)) + "; }") ); | ||
}); | ||
@@ -1207,3 +1235,3 @@ } | ||
bundle.body = body.indent(); | ||
bundle.body = body; | ||
} | ||
@@ -1328,3 +1356,3 @@ | ||
var transform = options.transform; | ||
var transformed = transform(source); | ||
var transformed = transform( source, modulePath ); | ||
if ( !transformed || | ||
@@ -1632,7 +1660,3 @@ ( typeof transformed !== 'string' && | ||
body.trim() | ||
.prepend( "'use strict';\n\n" ) | ||
.indent() | ||
.prepend( '(function () {\n\n' ) | ||
.append( '\n\n}).call(global);' ); | ||
body.prepend( "'use strict';\n\n" ).trimLines() | ||
@@ -1642,41 +1666,54 @@ return packageResult( body, options, 'toCjs' ); | ||
function standaloneUmdIntro ( options, indentStr ) { | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var intro = | ||
(("(function (factory) {\ | ||
\n !(typeof exports === 'object' && typeof module !== 'undefined') &&\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + "factory) :\ | ||
\n factory()\ | ||
\n}(function () { 'use strict';\ | ||
\n\ | ||
\n"); | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function defaultUmdIntro ( options, indentStr ) { | ||
var intro, amdName, needsGlobal, amdDeps, cjsDeps, globalDeps, args, cjsDefine, globalDefine, nonAMDDefine; | ||
var hasExports = options.hasExports; | ||
amdName = options.amdName ? (("'" + (options.amdName)) + "', ") : ''; | ||
needsGlobal = options.hasImports || options.hasExports; | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var amdDeps = options.importPaths.length > 0 ? | ||
'[' + options.importPaths.map( quote ).join( ', ' ) + '], ' : | ||
''; | ||
var cjsDeps = options.importPaths.map( req ).join( ', ' ); | ||
var globalDeps = options.importNames.map( globalify ).join( ', ' ); | ||
var args = options.args.join( ', ' ); | ||
amdDeps = options.importPaths.map( quote ).join( ', ' ); | ||
cjsDeps = options.importPaths.map( req ).join( ', ' ); | ||
globalDeps = options.importNames.map( globalify ).join( ', ' ); | ||
args = ( options.args || options.importNames ).join( ', ' ); | ||
var cjsExport = | ||
(hasExports ? 'module.exports = ' : '') + (("factory(" + cjsDeps) + ")"); | ||
cjsDefine = options.hasExports ? | ||
(("module.exports = factory(" + cjsDeps) + ")") : | ||
(("factory(" + cjsDeps) + ")"); | ||
var globalExport = | ||
(hasExports ? (("global." + (options.name)) + " = ") : '') + (("factory(" + globalDeps) + ")"); | ||
globalDefine = options.hasExports ? | ||
(("global." + (options.name)) + (" = factory(" + globalDeps) + ")") : | ||
(("factory(" + globalDeps) + ")"); | ||
nonAMDDefine = cjsDefine === globalDefine ? globalDefine : | ||
(("typeof exports === 'object' ? " + cjsDefine) + (" :\n\t" + globalDefine) + ""); | ||
intro = | ||
(("(function (" + (needsGlobal ? 'global, ' : '')) + ("factory) {\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + (amdDeps ? '[' + amdDeps + '], ' : '')) + ("factory) :\ | ||
\n " + nonAMDDefine) + ("\ | ||
\n}(" + (needsGlobal ? 'this, ' : '')) + ("function (" + args) + ") { 'use strict';\ | ||
var intro = | ||
(("(function (global, factory) {\ | ||
\n typeof exports === 'object' && typeof module !== 'undefined' ? " + cjsExport) + (" :\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + amdDeps) + ("factory) :\ | ||
\n " + globalExport) + ("\ | ||
\n}(this, function (" + args) + ") { 'use strict';\ | ||
\n\ | ||
\n").replace( /\t/g, indentStr ); | ||
\n"); | ||
return intro; | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function umd__umd ( mod, body, options ) { | ||
var importNames = [], | ||
importPaths = [], | ||
intro, | ||
i; | ||
var importNames = []; | ||
var importPaths = []; | ||
@@ -1687,36 +1724,39 @@ if ( !options.name ) { | ||
// ensure empty imports are at the end | ||
reorderImports( mod.imports ); | ||
var hasImports = mod.imports.length > 0; | ||
var hasExports = mod.exports.length > 0; | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
// ensure empty imports are at the end | ||
reorderImports( mod.imports ); | ||
if ( x.name ) { | ||
importNames[i] = x.name; | ||
} | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
body.remove( x.start, x.next ); | ||
}); | ||
if ( x.name ) { | ||
importNames[i] = x.name; | ||
} | ||
transformExportDeclaration( mod.exports[0], body ); | ||
body.remove( x.start, x.next ); | ||
}); | ||
intro = defaultUmdIntro({ | ||
hasImports: mod.imports.length > 0, | ||
hasExports: mod.exports.length > 0, | ||
transformExportDeclaration( mod.exports[0], body ); | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
intro = defaultUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
args: importNames, | ||
}, body.getIndentString() ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, mod.body.indentStr ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n}));' ); | ||
body.trim() | ||
.prepend( "'use strict';\n\n" ) | ||
.trim() | ||
.indent() | ||
.prepend( intro ) | ||
.append( '\n\n}));' ); | ||
return packageResult( body, options, 'toUmd' ); | ||
@@ -1874,5 +1914,5 @@ } | ||
body.trim().indent({ | ||
exclude: mod.ast._templateLiteralRanges | ||
}).prepend( options.intro ).trim().append( options.outro ); | ||
if ( options.intro && options.outro ) { | ||
body.indent().prepend( options.intro ).trimLines().append( options.outro ); | ||
} | ||
;$D$3 = void 0} | ||
@@ -1917,3 +1957,3 @@ | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, body.indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
@@ -1928,5 +1968,2 @@ utils_transformBody__transformBody( mod, body, { | ||
var intro = '(function () {\n\n\t\'use strict\';\n\n'; | ||
var outro = '\n\n}).call(global);'; | ||
function strictMode_cjs__cjs ( mod, body, options ) { | ||
@@ -1950,7 +1987,7 @@ var importBlock; | ||
utils_transformBody__transformBody( mod, body, { | ||
intro: intro.replace( /\t/g, body.indentStr ), | ||
header: importBlock, | ||
outro: outro | ||
}); | ||
body.prepend( "'use strict';\n\n" ).trimLines() | ||
return packageResult( body, options, 'toCjs' ); | ||
@@ -1960,44 +1997,37 @@ } | ||
function strictUmdIntro ( options, indentStr ) { | ||
var intro, amdName, needsGlobal, defaultsBlock = '', amdDeps, cjsDeps, globalDeps, args, cjsDefine, globalDefine, nonAMDDefine; | ||
var hasExports = options.hasExports; | ||
amdName = options.amdName ? (("'" + (options.amdName)) + "', ") : ''; | ||
needsGlobal = options.hasImports || options.hasExports; | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var amdDeps = options.hasExports || options.importPaths.length > 0 ? | ||
'[' + | ||
( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths ).map( quote ).join( ', ' ) + | ||
'], ' : | ||
''; | ||
var cjsDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths.map( req ) ).join( ', ' ); | ||
var globalDeps = ( options.hasExports ? [ (("(global." + (options.name)) + " = {})") ] : [] ) | ||
.concat( options.importNames.map( globalify ) ).join( ', ' ); | ||
var args = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importNames ).join( ', ' ); | ||
amdDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths ).map( quote ).join( ', ' ); | ||
cjsDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths.map( req ) ).join( ', ' ); | ||
globalDeps = ( options.hasExports ? [ options.name ] : [] ).concat( options.importNames ).map( globalify ).join( ', ' ); | ||
args = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importNames ).join( ', ' ); | ||
var defaultsBlock = ''; | ||
if ( options.externalDefaults && options.externalDefaults.length > 0 ) { | ||
defaultsBlock = options.externalDefaults.map( function(name ) | ||
{return (("\tvar " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");")} | ||
).join( '\n' ) + '\n\n'; | ||
).join('\n') + '\n\n'; | ||
} | ||
cjsDefine =(("factory(" + cjsDeps) + ")"); | ||
globalDefine = options.hasExports ? | ||
(("(global." + (options.name)) + (" = {}, factory(" + globalDeps) + "))") : | ||
(("factory(" + globalDeps) + ")"); | ||
nonAMDDefine = cjsDefine === globalDefine ? globalDefine : | ||
(("typeof exports === 'object' ? " + cjsDefine) + (" :\n\t" + globalDefine) + ""); | ||
intro = | ||
(("(function (" + (needsGlobal ? 'global, ' : '')) + ("factory) {\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + (amdDeps ? '[' + amdDeps + '], ' : '')) + ("factory) :\ | ||
\n " + nonAMDDefine) + ("\ | ||
\n}(" + (needsGlobal ? 'this, ' : '')) + ("function (" + args) + (") { 'use strict';\ | ||
var intro = | ||
(("(function (global, factory) {\ | ||
\n typeof exports === 'object' && typeof module !== 'undefined' ? factory(" + cjsDeps) + (") :\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + amdDeps) + ("factory) :\ | ||
\n factory(" + globalDeps) + (")\ | ||
\n}(this, function (" + args) + (") { 'use strict';\ | ||
\n\ | ||
\n" + defaultsBlock) + "").replace( /\t/g, indentStr ); | ||
\n" + defaultsBlock) + "") | ||
return intro; | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function strictMode_umd__umd ( mod, body, options ) {var $D$5; | ||
var importPaths, | ||
importNames, | ||
intro; | ||
function strictMode_umd__umd ( mod, body, options ) { | ||
if ( !options.name ) { | ||
@@ -2009,15 +2039,22 @@ throw new Error( 'You must supply a `name` option for UMD modules' ); | ||
importPaths = ($D$5 = getImportSummary( mod ))[0], importNames = $D$5[1], $D$5; | ||
var importPaths = (importNames = getImportSummary( mod ))[0], importNames = importNames[1]; | ||
intro = strictUmdIntro({ | ||
hasImports: mod.imports.length > 0, | ||
hasExports: mod.exports.length > 0, | ||
var hasImports = mod.imports.length > 0; | ||
var hasExports = mod.exports.length > 0; | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
intro = strictUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
}, body.getIndentString() ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, body.indentStr ); | ||
utils_transformBody__transformBody( mod, body, { | ||
@@ -2029,3 +2066,3 @@ intro: intro, | ||
return packageResult( body, options, 'toUmd' ); | ||
;$D$5 = void 0} | ||
} | ||
@@ -2047,19 +2084,14 @@ var strictMode = { | ||
function defaultsMode_amd__amd ( bundle, body, options ) { | ||
var intro, | ||
indentStr, | ||
defaultName; | ||
indentStr = body.getIndentString(); | ||
if ( defaultName = bundle.entryModule.identifierReplacements.default ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + defaultName) + ";") ); | ||
var defaultName = bundle.entryModule.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nreturn " + defaultName) + ";") ); | ||
} | ||
intro = defaultsMode_amd__introTemplate({ | ||
var intro = defaultsMode_amd__introTemplate({ | ||
amdName: options.amdName ? (("'" + (options.amdName)) + "', ") : '', | ||
amdDeps: bundle.externalModules.length ? '[' + bundle.externalModules.map( quoteId ).join( ', ' ) + '], ' : '', | ||
names: bundle.externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ] + '__default'} ).join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n});' ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
@@ -2073,13 +2105,5 @@ } | ||
function defaultsMode_cjs__cjs ( bundle, body, options ) { | ||
var importBlock, | ||
x, | ||
intro, | ||
indentStr, | ||
defaultName; | ||
indentStr = body.getIndentString(); | ||
importBlock = bundle.externalModules.map( function(x ) { | ||
var importBlock = bundle.externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ]; | ||
return indentStr + (("var " + name) + ("__default = require('" + (x.id)) + "');"); | ||
return (("var " + name) + ("__default = require('" + (x.id)) + "');"); | ||
}).join( '\n' ); | ||
@@ -2091,9 +2115,9 @@ | ||
if ( defaultName = bundle.entryModule.identifierReplacements.default ) { | ||
body.append( (("\n\n" + indentStr) + ("module.exports = " + defaultName) + ";") ); | ||
var defaultName = bundle.entryModule.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nmodule.exports = " + defaultName) + ";") ); | ||
} | ||
intro = '(function () {\n\n' + indentStr + "'use strict';\n\n"; | ||
body.prepend("'use strict';\n\n").trimLines(); | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
@@ -2108,27 +2132,32 @@ } | ||
var entry = bundle.entryModule; | ||
var indentStr = body.getIndentString(); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
var hasImports = bundle.externalModules.length > 0; | ||
var hasExports = entry.exports.length > 0; | ||
var intro = defaultUmdIntro({ | ||
hasImports: bundle.externalModules.length > 0, | ||
hasExports: entry.exports.length > 0, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
args: importNames.map( function(name ) {return name + '__default'} ), | ||
var defaultName = entry.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nreturn " + defaultName) + ";") ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, indentStr ); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
body.prepend( intro ).trim(); | ||
var defaultName; | ||
if ( ( defaultName = entry.identifierReplacements.default ) ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + defaultName) + ";") ); | ||
intro = defaultUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
args: importNames.map( function(name ) {return name + '__default'} ), | ||
}, body.getIndentString() ); | ||
} | ||
body.append('\n\n}));'); | ||
body.indent().prepend( intro ).trimLines().append('\n\n}));'); | ||
@@ -2164,23 +2193,19 @@ return packageResult( body, options, 'toUmd', true ); | ||
function getExportBlock ( entry, indentStr ) { | ||
function getExportBlock ( entry ) { | ||
var name = entry.identifierReplacements.default; | ||
return indentStr + (("exports['default'] = " + name) + ";"); | ||
return (("exports['default'] = " + name) + ";"); | ||
} | ||
var builders_strictMode_amd__introTemplate; | ||
var builders_strictMode_amd__introTemplate = template( 'define(<%= amdName %><%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_amd__amd ( bundle, body, options ) { | ||
var externalDefaults = getExternalDefaults( bundle ), | ||
defaultsBlock, | ||
entry = bundle.entryModule, | ||
importIds = bundle.externalModules.map( getId ), | ||
importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ), | ||
intro, | ||
indentStr; | ||
var externalDefaults = getExternalDefaults( bundle ); | ||
var entry = bundle.entryModule; | ||
indentStr = body.getIndentString(); | ||
var importIds = bundle.externalModules.map( getId ); | ||
var importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ); | ||
if ( externalDefaults.length ) { | ||
defaultsBlock = externalDefaults.map( function(name ) { | ||
return indentStr + (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
var defaultsBlock = externalDefaults.map( function(name ) { | ||
return (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
}).join( '\n' ); | ||
@@ -2196,33 +2221,26 @@ | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
} | ||
intro = builders_strictMode_amd__introTemplate({ | ||
var intro = builders_strictMode_amd__introTemplate({ | ||
amdName: options.amdName ? (("'" + (options.amdName)) + "', ") : '', | ||
amdDeps: importIds.length ? '[' + importIds.map( quote ).join( ', ' ) + '], ' : '', | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n});' ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
} | ||
builders_strictMode_amd__introTemplate = template( 'define(<%= amdName %><%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_cjs__cjs ( bundle, body, options ) { | ||
var externalDefaults = getExternalDefaults( bundle ), | ||
importBlock, | ||
entry = bundle.entryModule, | ||
intro, | ||
indentStr; | ||
var externalDefaults = getExternalDefaults( bundle ); | ||
var entry = bundle.entryModule; | ||
indentStr = body.getIndentString(); | ||
importBlock = bundle.externalModules.map( function(x ) { | ||
var importBlock = bundle.externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ], | ||
statement = (("" + indentStr) + ("var " + name) + (" = require('" + (x.id)) + "');"); | ||
statement = (("var " + name) + (" = require('" + (x.id)) + "');"); | ||
if ( ~externalDefaults.indexOf( name ) ) { | ||
statement += (("\n" + indentStr) + ("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
statement += (("\nvar " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
} | ||
@@ -2238,8 +2256,7 @@ | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
intro = '(function () {\n\n' + indentStr + "'use strict';\n\n"; | ||
body.prepend("'use strict';\n\n").trimLines(); | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
@@ -2254,26 +2271,31 @@ } | ||
var entry = bundle.entryModule; | ||
var indentStr = body.getIndentString(); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
var hasImports = bundle.externalModules.length > 0; | ||
var hasExports = entry.exports.length > 0; | ||
var intro = strictUmdIntro({ | ||
hasImports: bundle.externalModules.length > 0, | ||
hasExports: entry.exports.length > 0, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
externalDefaults: getExternalDefaults( bundle ), | ||
if ( hasExports && entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, indentStr ); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
body.prepend( intro ).trim(); | ||
if ( entry.exports.length && entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
intro = strictUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
externalDefaults: getExternalDefaults( bundle ), | ||
amdName: options.amdName, | ||
name: options.name, | ||
}, body.getIndentString() ); | ||
} | ||
body.append('\n\n}));'); | ||
body.indent().prepend( intro ).trimLines().append('\n\n}));'); | ||
@@ -2280,0 +2302,0 @@ return packageResult( body, options, 'toUmd', true ); |
436
esperanto.js
@@ -49,2 +49,4 @@ (function () { | ||
var envDepth = 0; | ||
estraverse__default.traverse( ast, { | ||
@@ -63,2 +65,8 @@ enter: function ( node ) { | ||
case 'FunctionDeclaration': | ||
envDepth++; | ||
// fallthrough | ||
case 'ArrowFunctionExpression': | ||
if ( node.id ) { | ||
@@ -108,2 +116,8 @@ addToScope( node ); | ||
break; | ||
case 'ThisExpression': | ||
if (envDepth === 0) { | ||
node._topLevel = true; | ||
} | ||
break; | ||
} | ||
@@ -115,3 +129,11 @@ }, | ||
case 'FunctionDeclaration': | ||
envDepth--; | ||
// fallthrough | ||
case 'ArrowFunctionExpression': | ||
scope = scope.parent; | ||
break; | ||
@@ -1020,2 +1042,7 @@ | ||
rewriteIdentifiers( body, node, identifierReplacements, scope ); | ||
// Replace top-level this with undefined ES6 8.1.1.5.4 | ||
if ( node.type === 'ThisExpression' && node._topLevel ) { | ||
body.replace( node.start, node.end, 'undefined' ); | ||
} | ||
}, | ||
@@ -1119,2 +1146,3 @@ | ||
// exports available, using Object.defineProperty | ||
var indentStr = body.getIndentString(); | ||
if ( mod._exportsNamespace ) { | ||
@@ -1127,7 +1155,7 @@ var prefix = bundle.uniqueNames[ mod.id ], | ||
if ( x.hasDeclaration ) { | ||
namespaceExports.push( body.indentStr + (("get " + (x.name)) + (" () { return " + (identifierReplacements[x.name])) + "; }") ); | ||
namespaceExports.push( indentStr + (("get " + (x.name)) + (" () { return " + (identifierReplacements[x.name])) + "; }") ); | ||
} | ||
else if ( x.isDefault ) { | ||
namespaceExports.push( body.indentStr + (("get default () { return " + (identifierReplacements.default)) + "; }") ); | ||
namespaceExports.push( indentStr + (("get default () { return " + (identifierReplacements.default)) + "; }") ); | ||
} | ||
@@ -1137,3 +1165,3 @@ | ||
x.specifiers.forEach( function(s ) { | ||
namespaceExports.push( body.indentStr + (("get " + (s.name)) + (" () { return " + (s.name)) + "; }") ); | ||
namespaceExports.push( indentStr + (("get " + (s.name)) + (" () { return " + (s.name)) + "; }") ); | ||
}); | ||
@@ -1207,3 +1235,3 @@ } | ||
bundle.body = body.indent(); | ||
bundle.body = body; | ||
} | ||
@@ -1328,3 +1356,3 @@ | ||
var transform = options.transform; | ||
var transformed = transform(source); | ||
var transformed = transform( source, modulePath ); | ||
if ( !transformed || | ||
@@ -1632,7 +1660,3 @@ ( typeof transformed !== 'string' && | ||
body.trim() | ||
.prepend( "'use strict';\n\n" ) | ||
.indent() | ||
.prepend( '(function () {\n\n' ) | ||
.append( '\n\n}).call(global);' ); | ||
body.prepend( "'use strict';\n\n" ).trimLines() | ||
@@ -1642,41 +1666,54 @@ return packageResult( body, options, 'toCjs' ); | ||
function standaloneUmdIntro ( options, indentStr ) { | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var intro = | ||
(("(function (factory) {\ | ||
\n !(typeof exports === 'object' && typeof module !== 'undefined') &&\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + "factory) :\ | ||
\n factory()\ | ||
\n}(function () { 'use strict';\ | ||
\n\ | ||
\n"); | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function defaultUmdIntro ( options, indentStr ) { | ||
var intro, amdName, needsGlobal, amdDeps, cjsDeps, globalDeps, args, cjsDefine, globalDefine, nonAMDDefine; | ||
var hasExports = options.hasExports; | ||
amdName = options.amdName ? (("'" + (options.amdName)) + "', ") : ''; | ||
needsGlobal = options.hasImports || options.hasExports; | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var amdDeps = options.importPaths.length > 0 ? | ||
'[' + options.importPaths.map( quote ).join( ', ' ) + '], ' : | ||
''; | ||
var cjsDeps = options.importPaths.map( req ).join( ', ' ); | ||
var globalDeps = options.importNames.map( globalify ).join( ', ' ); | ||
var args = options.args.join( ', ' ); | ||
amdDeps = options.importPaths.map( quote ).join( ', ' ); | ||
cjsDeps = options.importPaths.map( req ).join( ', ' ); | ||
globalDeps = options.importNames.map( globalify ).join( ', ' ); | ||
args = ( options.args || options.importNames ).join( ', ' ); | ||
var cjsExport = | ||
(hasExports ? 'module.exports = ' : '') + (("factory(" + cjsDeps) + ")"); | ||
cjsDefine = options.hasExports ? | ||
(("module.exports = factory(" + cjsDeps) + ")") : | ||
(("factory(" + cjsDeps) + ")"); | ||
var globalExport = | ||
(hasExports ? (("global." + (options.name)) + " = ") : '') + (("factory(" + globalDeps) + ")"); | ||
globalDefine = options.hasExports ? | ||
(("global." + (options.name)) + (" = factory(" + globalDeps) + ")") : | ||
(("factory(" + globalDeps) + ")"); | ||
nonAMDDefine = cjsDefine === globalDefine ? globalDefine : | ||
(("typeof exports === 'object' ? " + cjsDefine) + (" :\n\t" + globalDefine) + ""); | ||
intro = | ||
(("(function (" + (needsGlobal ? 'global, ' : '')) + ("factory) {\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + (amdDeps ? '[' + amdDeps + '], ' : '')) + ("factory) :\ | ||
\n " + nonAMDDefine) + ("\ | ||
\n}(" + (needsGlobal ? 'this, ' : '')) + ("function (" + args) + ") { 'use strict';\ | ||
var intro = | ||
(("(function (global, factory) {\ | ||
\n typeof exports === 'object' && typeof module !== 'undefined' ? " + cjsExport) + (" :\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + amdDeps) + ("factory) :\ | ||
\n " + globalExport) + ("\ | ||
\n}(this, function (" + args) + ") { 'use strict';\ | ||
\n\ | ||
\n").replace( /\t/g, indentStr ); | ||
\n"); | ||
return intro; | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function umd__umd ( mod, body, options ) { | ||
var importNames = [], | ||
importPaths = [], | ||
intro, | ||
i; | ||
var importNames = []; | ||
var importPaths = []; | ||
@@ -1687,36 +1724,39 @@ if ( !options.name ) { | ||
// ensure empty imports are at the end | ||
reorderImports( mod.imports ); | ||
var hasImports = mod.imports.length > 0; | ||
var hasExports = mod.exports.length > 0; | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
// ensure empty imports are at the end | ||
reorderImports( mod.imports ); | ||
if ( x.name ) { | ||
importNames[i] = x.name; | ||
} | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
body.remove( x.start, x.next ); | ||
}); | ||
if ( x.name ) { | ||
importNames[i] = x.name; | ||
} | ||
transformExportDeclaration( mod.exports[0], body ); | ||
body.remove( x.start, x.next ); | ||
}); | ||
intro = defaultUmdIntro({ | ||
hasImports: mod.imports.length > 0, | ||
hasExports: mod.exports.length > 0, | ||
transformExportDeclaration( mod.exports[0], body ); | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
intro = defaultUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
args: importNames, | ||
}, body.getIndentString() ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, mod.body.indentStr ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n}));' ); | ||
body.trim() | ||
.prepend( "'use strict';\n\n" ) | ||
.trim() | ||
.indent() | ||
.prepend( intro ) | ||
.append( '\n\n}));' ); | ||
return packageResult( body, options, 'toUmd' ); | ||
@@ -1874,5 +1914,5 @@ } | ||
body.trim().indent({ | ||
exclude: mod.ast._templateLiteralRanges | ||
}).prepend( options.intro ).trim().append( options.outro ); | ||
if ( options.intro && options.outro ) { | ||
body.indent().prepend( options.intro ).trimLines().append( options.outro ); | ||
} | ||
;$D$3 = void 0} | ||
@@ -1917,3 +1957,3 @@ | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, body.indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
@@ -1928,5 +1968,2 @@ utils_transformBody__transformBody( mod, body, { | ||
var intro = '(function () {\n\n\t\'use strict\';\n\n'; | ||
var outro = '\n\n}).call(global);'; | ||
function strictMode_cjs__cjs ( mod, body, options ) { | ||
@@ -1950,7 +1987,7 @@ var importBlock; | ||
utils_transformBody__transformBody( mod, body, { | ||
intro: intro.replace( /\t/g, body.indentStr ), | ||
header: importBlock, | ||
outro: outro | ||
}); | ||
body.prepend( "'use strict';\n\n" ).trimLines() | ||
return packageResult( body, options, 'toCjs' ); | ||
@@ -1960,44 +1997,37 @@ } | ||
function strictUmdIntro ( options, indentStr ) { | ||
var intro, amdName, needsGlobal, defaultsBlock = '', amdDeps, cjsDeps, globalDeps, args, cjsDefine, globalDefine, nonAMDDefine; | ||
var hasExports = options.hasExports; | ||
amdName = options.amdName ? (("'" + (options.amdName)) + "', ") : ''; | ||
needsGlobal = options.hasImports || options.hasExports; | ||
var amdName = options.amdName ? | ||
"'" + options.amdName + "', " : | ||
''; | ||
var amdDeps = options.hasExports || options.importPaths.length > 0 ? | ||
'[' + | ||
( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths ).map( quote ).join( ', ' ) + | ||
'], ' : | ||
''; | ||
var cjsDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths.map( req ) ).join( ', ' ); | ||
var globalDeps = ( options.hasExports ? [ (("(global." + (options.name)) + " = {})") ] : [] ) | ||
.concat( options.importNames.map( globalify ) ).join( ', ' ); | ||
var args = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importNames ).join( ', ' ); | ||
amdDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths ).map( quote ).join( ', ' ); | ||
cjsDeps = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importPaths.map( req ) ).join( ', ' ); | ||
globalDeps = ( options.hasExports ? [ options.name ] : [] ).concat( options.importNames ).map( globalify ).join( ', ' ); | ||
args = ( options.hasExports ? [ 'exports' ] : [] ).concat( options.importNames ).join( ', ' ); | ||
var defaultsBlock = ''; | ||
if ( options.externalDefaults && options.externalDefaults.length > 0 ) { | ||
defaultsBlock = options.externalDefaults.map( function(name ) | ||
{return (("\tvar " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");")} | ||
).join( '\n' ) + '\n\n'; | ||
).join('\n') + '\n\n'; | ||
} | ||
cjsDefine =(("factory(" + cjsDeps) + ")"); | ||
globalDefine = options.hasExports ? | ||
(("(global." + (options.name)) + (" = {}, factory(" + globalDeps) + "))") : | ||
(("factory(" + globalDeps) + ")"); | ||
nonAMDDefine = cjsDefine === globalDefine ? globalDefine : | ||
(("typeof exports === 'object' ? " + cjsDefine) + (" :\n\t" + globalDefine) + ""); | ||
intro = | ||
(("(function (" + (needsGlobal ? 'global, ' : '')) + ("factory) {\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + (amdDeps ? '[' + amdDeps + '], ' : '')) + ("factory) :\ | ||
\n " + nonAMDDefine) + ("\ | ||
\n}(" + (needsGlobal ? 'this, ' : '')) + ("function (" + args) + (") { 'use strict';\ | ||
var intro = | ||
(("(function (global, factory) {\ | ||
\n typeof exports === 'object' && typeof module !== 'undefined' ? factory(" + cjsDeps) + (") :\ | ||
\n typeof define === 'function' && define.amd ? define(" + amdName) + ("" + amdDeps) + ("factory) :\ | ||
\n factory(" + globalDeps) + (")\ | ||
\n}(this, function (" + args) + (") { 'use strict';\ | ||
\n\ | ||
\n" + defaultsBlock) + "").replace( /\t/g, indentStr ); | ||
\n" + defaultsBlock) + "") | ||
return intro; | ||
return intro.replace( /\t/g, indentStr ); | ||
} | ||
function strictMode_umd__umd ( mod, body, options ) {var $D$5; | ||
var importPaths, | ||
importNames, | ||
intro; | ||
function strictMode_umd__umd ( mod, body, options ) { | ||
if ( !options.name ) { | ||
@@ -2009,15 +2039,22 @@ throw new Error( 'You must supply a `name` option for UMD modules' ); | ||
importPaths = ($D$5 = getImportSummary( mod ))[0], importNames = $D$5[1], $D$5; | ||
var importPaths = (importNames = getImportSummary( mod ))[0], importNames = importNames[1]; | ||
intro = strictUmdIntro({ | ||
hasImports: mod.imports.length > 0, | ||
hasExports: mod.exports.length > 0, | ||
var hasImports = mod.imports.length > 0; | ||
var hasExports = mod.exports.length > 0; | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
intro = strictUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
}, body.getIndentString() ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, body.indentStr ); | ||
utils_transformBody__transformBody( mod, body, { | ||
@@ -2029,3 +2066,3 @@ intro: intro, | ||
return packageResult( body, options, 'toUmd' ); | ||
;$D$5 = void 0} | ||
} | ||
@@ -2047,19 +2084,14 @@ var strictMode = { | ||
function defaultsMode_amd__amd ( bundle, body, options ) { | ||
var intro, | ||
indentStr, | ||
defaultName; | ||
indentStr = body.getIndentString(); | ||
if ( defaultName = bundle.entryModule.identifierReplacements.default ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + defaultName) + ";") ); | ||
var defaultName = bundle.entryModule.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nreturn " + defaultName) + ";") ); | ||
} | ||
intro = defaultsMode_amd__introTemplate({ | ||
var intro = defaultsMode_amd__introTemplate({ | ||
amdName: options.amdName ? (("'" + (options.amdName)) + "', ") : '', | ||
amdDeps: bundle.externalModules.length ? '[' + bundle.externalModules.map( quoteId ).join( ', ' ) + '], ' : '', | ||
names: bundle.externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ] + '__default'} ).join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n});' ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
@@ -2073,13 +2105,5 @@ } | ||
function defaultsMode_cjs__cjs ( bundle, body, options ) { | ||
var importBlock, | ||
x, | ||
intro, | ||
indentStr, | ||
defaultName; | ||
indentStr = body.getIndentString(); | ||
importBlock = bundle.externalModules.map( function(x ) { | ||
var importBlock = bundle.externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ]; | ||
return indentStr + (("var " + name) + ("__default = require('" + (x.id)) + "');"); | ||
return (("var " + name) + ("__default = require('" + (x.id)) + "');"); | ||
}).join( '\n' ); | ||
@@ -2091,9 +2115,9 @@ | ||
if ( defaultName = bundle.entryModule.identifierReplacements.default ) { | ||
body.append( (("\n\n" + indentStr) + ("module.exports = " + defaultName) + ";") ); | ||
var defaultName = bundle.entryModule.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nmodule.exports = " + defaultName) + ";") ); | ||
} | ||
intro = '(function () {\n\n' + indentStr + "'use strict';\n\n"; | ||
body.prepend("'use strict';\n\n").trimLines(); | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
@@ -2108,27 +2132,32 @@ } | ||
var entry = bundle.entryModule; | ||
var indentStr = body.getIndentString(); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
var hasImports = bundle.externalModules.length > 0; | ||
var hasExports = entry.exports.length > 0; | ||
var intro = defaultUmdIntro({ | ||
hasImports: bundle.externalModules.length > 0, | ||
hasExports: entry.exports.length > 0, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
args: importNames.map( function(name ) {return name + '__default'} ), | ||
var defaultName = entry.identifierReplacements.default; | ||
if ( defaultName ) { | ||
body.append( (("\n\nreturn " + defaultName) + ";") ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, indentStr ); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
body.prepend( intro ).trim(); | ||
var defaultName; | ||
if ( ( defaultName = entry.identifierReplacements.default ) ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + defaultName) + ";") ); | ||
intro = defaultUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
amdName: options.amdName, | ||
name: options.name, | ||
args: importNames.map( function(name ) {return name + '__default'} ), | ||
}, body.getIndentString() ); | ||
} | ||
body.append('\n\n}));'); | ||
body.indent().prepend( intro ).trimLines().append('\n\n}));'); | ||
@@ -2164,23 +2193,19 @@ return packageResult( body, options, 'toUmd', true ); | ||
function getExportBlock ( entry, indentStr ) { | ||
function getExportBlock ( entry ) { | ||
var name = entry.identifierReplacements.default; | ||
return indentStr + (("exports['default'] = " + name) + ";"); | ||
return (("exports['default'] = " + name) + ";"); | ||
} | ||
var builders_strictMode_amd__introTemplate; | ||
var builders_strictMode_amd__introTemplate = template( 'define(<%= amdName %><%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_amd__amd ( bundle, body, options ) { | ||
var externalDefaults = getExternalDefaults( bundle ), | ||
defaultsBlock, | ||
entry = bundle.entryModule, | ||
importIds = bundle.externalModules.map( getId ), | ||
importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ), | ||
intro, | ||
indentStr; | ||
var externalDefaults = getExternalDefaults( bundle ); | ||
var entry = bundle.entryModule; | ||
indentStr = body.getIndentString(); | ||
var importIds = bundle.externalModules.map( getId ); | ||
var importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ); | ||
if ( externalDefaults.length ) { | ||
defaultsBlock = externalDefaults.map( function(name ) { | ||
return indentStr + (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
var defaultsBlock = externalDefaults.map( function(name ) { | ||
return (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
}).join( '\n' ); | ||
@@ -2196,33 +2221,26 @@ | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
} | ||
intro = builders_strictMode_amd__introTemplate({ | ||
var intro = builders_strictMode_amd__introTemplate({ | ||
amdName: options.amdName ? (("'" + (options.amdName)) + "', ") : '', | ||
amdDeps: importIds.length ? '[' + importIds.map( quote ).join( ', ' ) + '], ' : '', | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
}).replace( /\t/g, body.getIndentString() ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
body.indent().prepend( intro ).trimLines().append( '\n\n});' ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
} | ||
builders_strictMode_amd__introTemplate = template( 'define(<%= amdName %><%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_cjs__cjs ( bundle, body, options ) { | ||
var externalDefaults = getExternalDefaults( bundle ), | ||
importBlock, | ||
entry = bundle.entryModule, | ||
intro, | ||
indentStr; | ||
var externalDefaults = getExternalDefaults( bundle ); | ||
var entry = bundle.entryModule; | ||
indentStr = body.getIndentString(); | ||
importBlock = bundle.externalModules.map( function(x ) { | ||
var importBlock = bundle.externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ], | ||
statement = (("" + indentStr) + ("var " + name) + (" = require('" + (x.id)) + "');"); | ||
statement = (("var " + name) + (" = require('" + (x.id)) + "');"); | ||
if ( ~externalDefaults.indexOf( name ) ) { | ||
statement += (("\n" + indentStr) + ("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
statement += (("\nvar " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
} | ||
@@ -2238,8 +2256,7 @@ | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
intro = '(function () {\n\n' + indentStr + "'use strict';\n\n"; | ||
body.prepend("'use strict';\n\n").trimLines(); | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
@@ -2254,26 +2271,31 @@ } | ||
var entry = bundle.entryModule; | ||
var indentStr = body.getIndentString(); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
var hasImports = bundle.externalModules.length > 0; | ||
var hasExports = entry.exports.length > 0; | ||
var intro = strictUmdIntro({ | ||
hasImports: bundle.externalModules.length > 0, | ||
hasExports: entry.exports.length > 0, | ||
var intro; | ||
if (!hasImports && !hasExports) { | ||
intro = standaloneUmdIntro({ | ||
amdName: options.amdName, | ||
}, body.getIndentString() ); | ||
} else { | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
externalDefaults: getExternalDefaults( bundle ), | ||
if ( hasExports && entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry ) ); | ||
} | ||
amdName: options.amdName, | ||
name: options.name | ||
}, indentStr ); | ||
var importPaths = bundle.externalModules.map( getId ); | ||
var importNames = importPaths.map( function(path ) {return bundle.uniqueNames[ path ]} ); | ||
body.prepend( intro ).trim(); | ||
if ( entry.exports.length && entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( entry, indentStr ) ); | ||
intro = strictUmdIntro({ | ||
hasExports: hasExports, | ||
importPaths: importPaths, | ||
importNames: importNames, | ||
externalDefaults: getExternalDefaults( bundle ), | ||
amdName: options.amdName, | ||
name: options.name, | ||
}, body.getIndentString() ); | ||
} | ||
body.append('\n\n}));'); | ||
body.indent().prepend( intro ).trimLines().append('\n\n}));'); | ||
@@ -2280,0 +2302,0 @@ return packageResult( body, options, 'toUmd', true ); |
{ | ||
"name": "esperanto", | ||
"description": "An easier way to convert ES6 modules to AMD and CommonJS", | ||
"version": "0.5.10", | ||
"version": "0.6.0", | ||
"author": "Rich Harris", | ||
@@ -11,3 +11,3 @@ "repository": "https://github.com/esperantojs/esperanto", | ||
"estraverse": "^1.9.0", | ||
"magic-string": "^0.2.5", | ||
"magic-string": "^0.3.0", | ||
"minimist": "^1.1.0", | ||
@@ -28,3 +28,3 @@ "sander": "^0.2.1" | ||
"gobble-uglifyjs": "^0.1.0", | ||
"magic-string": "^0.2.6", | ||
"mocha": "^2.1.0", | ||
"sander": "^0.2.1", | ||
@@ -41,5 +41,5 @@ "source-map": "^0.1.40" | ||
"build": "scripts/build.sh", | ||
"test": "scripts/test.sh", | ||
"test": "mocha", | ||
"prepublish": "npm run build" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
809295
5222
+ Addedmagic-string@0.3.1(transitive)
- Removedmagic-string@0.2.7(transitive)
Updatedmagic-string@^0.3.0