Comparing version 0.4.10 to 0.5.0
1541
esperanto.js
@@ -11,3 +11,9 @@ (function () { | ||
var annotateAst__Scope = function ( options ) { | ||
/* | ||
This module traverse a module's AST, attaching scope information | ||
to nodes as it goes, which is later used to determine which | ||
identifiers need to be rewritten to avoid collisions | ||
*/ | ||
var Scope = function ( options ) { | ||
options = options || {}; | ||
@@ -19,3 +25,3 @@ | ||
annotateAst__Scope.prototype = { | ||
Scope.prototype = { | ||
add: function ( name ) { | ||
@@ -25,3 +31,7 @@ this.names.push( name ); | ||
contains: function ( name ) { | ||
contains: function ( name, ignoreTopLevel ) { | ||
if ( ignoreTopLevel && !this.parent ) { | ||
return false; | ||
} | ||
if ( ~this.names.indexOf( name ) ) { | ||
@@ -32,3 +42,3 @@ return true; | ||
if ( this.parent ) { | ||
return this.parent.contains( name ); | ||
return this.parent.contains( name, ignoreTopLevel ); | ||
} | ||
@@ -40,4 +50,4 @@ | ||
function annotateAst__annotateAst ( ast ) { | ||
var scope = new annotateAst__Scope(), blockScope = new annotateAst__Scope(); | ||
function annotateAst ( ast ) { | ||
var scope = new Scope(), blockScope = new Scope(), declared = {}, templateLiteralRanges = []; | ||
@@ -54,8 +64,9 @@ estraverse__default.traverse( ast, { | ||
if ( annotateAst__createsScope( node ) ) { | ||
if ( createsScope( node ) ) { | ||
if ( node.id ) { | ||
scope.add( node.id.name ); | ||
declared[ node.id.name ] = true; | ||
} | ||
scope = node._scope = new annotateAst__Scope({ | ||
scope = node._scope = new Scope({ | ||
parent: scope, | ||
@@ -66,4 +77,4 @@ params: node.params.map( function(x ) {return x.name} ) // TODO rest params? | ||
else if ( annotateAst__createsBlockScope( node ) ) { | ||
blockScope = node._blockScope = new annotateAst__Scope({ | ||
else if ( createsBlockScope( node ) ) { | ||
blockScope = node._blockScope = new Scope({ | ||
parent: blockScope | ||
@@ -73,8 +84,10 @@ }); | ||
if ( annotateAst__declaresVar( node ) ) { | ||
if ( declaresVar( node ) ) { | ||
scope.add( node.id.name ); | ||
declared[ node.id.name ] = true; | ||
} | ||
else if ( annotateAst__declaresLet( node ) ) { | ||
else if ( declaresLet( node ) ) { | ||
blockScope.add( node.id.name ); | ||
declared[ node.id.name ] = true; | ||
} | ||
@@ -90,9 +103,15 @@ | ||
} | ||
// make a note of template literals - we want to prevent multiline | ||
// strings from being indented with everything else | ||
if ( node.type === 'TemplateLiteral' ) { | ||
templateLiteralRanges.push([ node.start, node.end ]); | ||
} | ||
}, | ||
leave: function ( node ) { | ||
if ( annotateAst__createsScope( node ) ) { | ||
if ( createsScope( node ) ) { | ||
scope = scope.parent; | ||
} | ||
else if ( annotateAst__createsBlockScope( node ) ) { | ||
else if ( createsBlockScope( node ) ) { | ||
blockScope = blockScope.parent; | ||
@@ -105,14 +124,15 @@ } | ||
ast._blockScope = blockScope; | ||
ast._declared = declared; | ||
ast._templateLiteralRanges = templateLiteralRanges; | ||
} | ||
var annotateAst__default = annotateAst__annotateAst; | ||
function annotateAst__createsScope ( node ) { | ||
function createsScope ( node ) { | ||
return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration'; | ||
} | ||
function annotateAst__createsBlockScope ( node ) { | ||
function createsBlockScope ( node ) { | ||
return node.type === 'BlockStatement'; | ||
} | ||
function annotateAst__declaresVar ( node ) { | ||
function declaresVar ( node ) { | ||
// TODO const? (function taken care of already) | ||
@@ -126,8 +146,15 @@ return ( | ||
function annotateAst__declaresLet ( node ) { | ||
function declaresLet ( node ) { | ||
return false; // TODO | ||
} | ||
function findImportsAndExports__findImportsAndExports ( mod, source, ast, imports, exports ) { | ||
var previousDeclaration; | ||
/** | ||
* Inspects a module and discovers/categorises import & export declarations | ||
* @param {object} mod - the module object | ||
* @param {string} source - the module's original source code | ||
* @param {object} ast - the result of parsing `source` with acorn | ||
* @returns {array} - [ imports, exports ] | ||
*/ | ||
function findImportsAndExports ( mod, source, ast ) { | ||
var imports = [], exports = [], previousDeclaration; | ||
@@ -146,3 +173,3 @@ ast.body.forEach( function(node ) { | ||
if ( node.type === 'ImportDeclaration' ) { | ||
declaration = findImportsAndExports__processImport( node ); | ||
declaration = processImport( node ); | ||
imports.push( declaration ); | ||
@@ -152,3 +179,3 @@ } | ||
else if ( node.type === 'ExportDeclaration' ) { | ||
declaration = findImportsAndExports__processExport( node, source ); | ||
declaration = processExport( node, source ); | ||
exports.push( declaration ); | ||
@@ -166,3 +193,3 @@ | ||
// `export { foo } from './bar'; | ||
passthrough = findImportsAndExports__processImport( node, true ); | ||
passthrough = processImport( node, true ); | ||
imports.push( passthrough ); | ||
@@ -184,6 +211,13 @@ | ||
} | ||
return [ imports, exports ]; | ||
} | ||
var findImportsAndExports__default = findImportsAndExports__findImportsAndExports; | ||
function findImportsAndExports__processImport ( node, passthrough ) { | ||
/** | ||
* Generates a representation of an import declaration | ||
* @param {object} node - the original AST node | ||
* @param {boolean} passthrough - `true` if this is an `export { foo } from 'bar'`-style declaration | ||
* @returns {object} | ||
*/ | ||
function processImport ( node, passthrough ) { | ||
var result = { | ||
@@ -226,3 +260,9 @@ node: node, | ||
function findImportsAndExports__processExport ( node, source ) { | ||
/** | ||
* Generates a representation of an export declaration | ||
* @param {object} node - the original AST node | ||
* @param {string} source - the original source code | ||
* @returns {object} | ||
*/ | ||
function processExport ( node, source ) { | ||
var result, d; | ||
@@ -312,5 +352,13 @@ | ||
var sanitize__reserved = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield'.split( ' ' ); | ||
var hasOwnProp = Object.prototype.hasOwnProperty; | ||
var hasOwnProp__default = hasOwnProp; | ||
function sanitize__sanitize ( name ) { | ||
var reserved = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield'.split( ' ' ); | ||
/** | ||
* Generates a sanitized (i.e. valid identifier) name from a module ID | ||
* @param {string} id - a module ID, or part thereof | ||
* @returns {string} | ||
*/ | ||
function sanitize ( name ) { | ||
name = name.replace( /[^a-zA-Z0-9_$]/g, '_' ); | ||
@@ -321,3 +369,3 @@ if ( /[^a-zA-Z_$]/.test( name[0] ) ) { | ||
if ( ~sanitize__reserved.indexOf( name ) ) { | ||
if ( ~reserved.indexOf( name ) ) { | ||
name = '_' + name; | ||
@@ -328,11 +376,6 @@ } | ||
} | ||
var sanitize__default = sanitize__sanitize; | ||
function getModuleNameHelper__moduleNameHelper ( userFn, varNames ) { | ||
var nameById = {}, usedNames = {}, getModuleName; | ||
function getModuleNameHelper ( userFn ) {var usedNames = arguments[1];if(usedNames === void 0)usedNames = {}; | ||
var nameById = {}, getModuleName; | ||
if ( varNames ) { | ||
varNames.forEach( function(n ) {return usedNames[n] = true} ); | ||
} | ||
getModuleName = function(x ) { | ||
@@ -344,3 +387,3 @@ var moduleId, parts, i, prefix = '', name, candidate, specifier; | ||
// use existing value | ||
if ( nameById.hasOwnProperty( moduleId ) ) { | ||
if ( hasOwnProp.call( nameById, moduleId ) ) { | ||
return nameById[ moduleId ]; | ||
@@ -351,5 +394,5 @@ } | ||
if ( userFn && ( name = userFn( moduleId ) ) ) { | ||
name = sanitize__default( name ); | ||
name = sanitize( name ); | ||
if ( usedNames.hasOwnProperty( name ) ) { | ||
if ( hasOwnProp.call( usedNames, name ) ) { | ||
// TODO write a test for this | ||
@@ -374,5 +417,5 @@ throw new Error( 'Naming collision: module ' + moduleId + ' cannot be called ' + name ); | ||
while ( i-- ) { | ||
candidate = prefix + sanitize__default( parts.slice( i ).join( '__' ) ); | ||
candidate = prefix + sanitize( parts.slice( i ).join( '__' ) ); | ||
if ( !usedNames.hasOwnProperty( candidate ) ) { | ||
if ( !hasOwnProp.call( usedNames, candidate ) ) { | ||
name = candidate; | ||
@@ -395,9 +438,7 @@ break; | ||
} | ||
var getModuleNameHelper__default = getModuleNameHelper__moduleNameHelper; | ||
function getStandaloneModule__getStandaloneModule ( options ) { | ||
var mod, varNames; | ||
function getStandaloneModule ( options ) {var $D$0; | ||
var mod, imports, exports; | ||
mod = { | ||
source: options.source, | ||
body: new MagicString__default( options.source ), | ||
@@ -407,21 +448,26 @@ ast: acorn__default.parse( options.source, { | ||
locations: true | ||
}), | ||
imports: [], | ||
exports: [] | ||
}) | ||
}; | ||
if ( options.strict ) { | ||
annotateAst__default( mod.ast ); | ||
varNames = mod.ast._scope.names.concat( mod.ast._blockScope.names ); | ||
annotateAst( mod.ast ); | ||
} | ||
mod.getName = getModuleNameHelper__default( options.getModuleName, varNames ); | ||
mod.getName = getModuleNameHelper( options.getModuleName, mod.ast._declared ); | ||
findImportsAndExports__default( mod, mod.source, mod.ast, mod.imports, mod.exports, options.getModuleName ); | ||
imports = ($D$0 = findImportsAndExports( mod, options.source, mod.ast ))[0], exports = $D$0[1], $D$0; | ||
mod.imports = imports; | ||
mod.exports = exports; | ||
return mod; | ||
} | ||
var getStandaloneModule__default = getStandaloneModule__getStandaloneModule; | ||
;$D$0 = void 0} | ||
function resolve__resolve ( importPath, importerPath ) { | ||
/** | ||
* Resolves an importPath relative to the module that is importing it | ||
* @param {string} importPath - the (possibly relative) path of an imported module | ||
* @param {string} importerPath - the (relative to `base`) path of the importing module | ||
* @returns {string} | ||
*/ | ||
function resolve ( importPath, importerPath ) { | ||
var resolved, importerParts, importParts; | ||
@@ -450,5 +496,4 @@ | ||
} | ||
var resolve__default = resolve__resolve; | ||
function sortModules__sortModules ( entry, moduleLookup ) { | ||
function sortModules ( entry, moduleLookup ) { | ||
var seen = {}, | ||
@@ -460,3 +505,3 @@ ordered = []; | ||
// already included | ||
if ( !mod || seen.hasOwnProperty( mod.id ) ) { | ||
if ( !mod || hasOwnProp.call( seen, mod.id ) ) { | ||
return; | ||
@@ -468,3 +513,3 @@ } | ||
mod.imports.forEach( function(x ) { | ||
var id = resolve__default( x.path, mod.file ); | ||
var id = resolve( x.path, mod.file ); | ||
visit( moduleLookup[ id ] ); | ||
@@ -480,5 +525,4 @@ }); | ||
} | ||
var sortModules__default = sortModules__sortModules; | ||
function resolveChains__resolveChains ( modules, moduleLookup ) { | ||
function resolveChains ( modules, moduleLookup ) { | ||
var chains = {}; | ||
@@ -488,7 +532,7 @@ | ||
modules.forEach( function(mod ) { | ||
var origin = {}, namespaceExporter; | ||
var origin = {}; | ||
mod.imports.forEach( function(x ) { | ||
x.specifiers.forEach( function(s ) { | ||
var moduleId = resolve__default( x.path, mod.file ); | ||
var moduleId = resolve( x.path, mod.file ); | ||
@@ -498,3 +542,3 @@ if ( s.batch ) { | ||
// it needs to export an object full of getters | ||
if ( moduleLookup.hasOwnProperty( moduleId ) ) { | ||
if ( hasOwnProp.call( moduleLookup, moduleId ) ) { | ||
moduleLookup[ moduleId ]._exportsNamespace = true; | ||
@@ -514,3 +558,3 @@ } | ||
x.specifiers.forEach( function(s ) { | ||
if ( origin.hasOwnProperty( s.name ) ) { | ||
if ( hasOwnProp.call( origin, s.name ) ) { | ||
chains[ mod.id + '@' + s.name ] = origin[ s.name ]; | ||
@@ -524,5 +568,4 @@ } | ||
} | ||
var resolveChains__default = resolveChains__resolveChains; | ||
function getUniqueNames__getUniqueNames ( modules, userNames ) { | ||
function getUniqueNames ( modules, externalModules, userNames ) { | ||
var names = {}, used = {}; | ||
@@ -541,6 +584,6 @@ | ||
mod.imports.forEach( function(x ) { | ||
var id = resolve__default( x.path, mod.file ); | ||
var id = resolve( x.path, mod.file ); | ||
x.id = id; | ||
if ( x.default && !names.hasOwnProperty( id ) && !used.hasOwnProperty( x.name ) ) { | ||
if ( x.default && !hasOwnProp.call( names, id ) && !hasOwnProp.call( used, x.name ) ) { | ||
names[ id ] = x.name; | ||
@@ -554,7 +597,7 @@ used[ x.name ] = true; | ||
// introducing conflicts | ||
modules.forEach( function(mod ) { | ||
modules.concat( externalModules ).forEach( function(mod ) { | ||
var parts, i, name; | ||
// is this already named? | ||
if ( names.hasOwnProperty( mod.id ) ) { | ||
if ( hasOwnProp.call( names, mod.id ) ) { | ||
return; | ||
@@ -567,5 +610,5 @@ } | ||
while ( i-- ) { | ||
name = sanitize__default( parts.slice( i ).join( '_' ) ); | ||
name = sanitize( parts.slice( i ).join( '_' ) ); | ||
if ( !used.hasOwnProperty( name ) ) { | ||
if ( !hasOwnProp.call( used, name ) ) { | ||
break; | ||
@@ -575,3 +618,3 @@ } | ||
while ( used.hasOwnProperty( name ) ) { | ||
while ( hasOwnProp.call( used, name ) ) { | ||
name = '_' + name; | ||
@@ -586,7 +629,279 @@ } | ||
} | ||
var getUniqueNames__default = getUniqueNames__getUniqueNames; | ||
function disallowIllegalReassignment__disallowIllegalReassignment ( node, names, scope ) { | ||
var assignee, name, replacement, message; | ||
function getUnscopedNames ( mod ) { | ||
var unscoped = [], importedNames, scope; | ||
function imported ( name ) { | ||
if (!importedNames) { | ||
importedNames = {}; | ||
mod.imports.forEach(function(i ) { | ||
!i.passthrough && i.specifiers.forEach(function(s ) { | ||
importedNames[ s.batch ? s.name : s.as ] = true; | ||
}); | ||
}); | ||
} | ||
return hasOwnProp.call( importedNames, name ); | ||
} | ||
estraverse__default.traverse( mod.ast, { | ||
enter: function ( node ) { | ||
// we're only interested in references, not property names etc | ||
if ( node._skip ) return this.skip(); | ||
if ( node._scope ) { | ||
scope = node._scope; | ||
} | ||
if ( node.type === 'Identifier' && | ||
!scope.contains( node.name ) && | ||
!imported( node.name ) && | ||
!~unscoped.indexOf( node.name ) ) { | ||
unscoped.push( node.name ); | ||
} | ||
}, | ||
leave: function ( node ) { | ||
if ( node.type === 'Program' ) { | ||
return; | ||
} | ||
if ( node._scope ) { | ||
scope = scope.parent; | ||
} | ||
} | ||
}); | ||
return unscoped; | ||
} | ||
function getRenamedImports ( mod ) { | ||
var renamed = []; | ||
mod.imports.forEach( function(x ) { | ||
if ( x.specifiers ) { | ||
x.specifiers.forEach( function(s ) { | ||
if ( s.name !== s.as && !~renamed.indexOf( s.name ) ) { | ||
renamed.push( s.name ); | ||
} | ||
}); | ||
} | ||
}); | ||
return renamed; | ||
} | ||
function topLevelScopeConflicts ( bundle ) { | ||
var conflicts = {}, inBundle = {}; | ||
bundle.modules.forEach( function(mod ) { | ||
var names = | ||
// all top defined identifiers are in top scope | ||
mod.ast._scope.names | ||
// all unattributed identifiers could collide with top scope | ||
.concat( getUnscopedNames( mod ) ) | ||
.concat( getRenamedImports( mod ) ); | ||
if ( mod._exportsNamespace ) { | ||
conflicts[ bundle.uniqueNames[ mod.id ] ] = true; | ||
} | ||
// merge this module's top scope with bundle top scope | ||
names.forEach( function(name ) { | ||
if ( hasOwnProp.call( inBundle, name ) ) { | ||
conflicts[ name ] = true; | ||
} else { | ||
inBundle[ name ] = true; | ||
} | ||
}); | ||
}); | ||
return conflicts; | ||
} | ||
function getIdentifierReplacements ( bundle ) { | ||
var conflicts, identifiers = {}; | ||
// first, discover conflicts | ||
conflicts = topLevelScopeConflicts( bundle ); | ||
bundle.modules.forEach( function(mod ) { | ||
var prefix, moduleIdentifiers, x; | ||
prefix = bundle.uniqueNames[ mod.id ]; | ||
identifiers[ mod.id ] = moduleIdentifiers = {}; | ||
function addName ( n ) { | ||
moduleIdentifiers[n] = { | ||
name: hasOwnProp.call( conflicts, n ) ? | ||
prefix + '__' + n : | ||
n | ||
}; | ||
} | ||
mod.ast._scope.names.forEach( addName ); | ||
mod.ast._blockScope.names.forEach( addName ); | ||
mod.imports.forEach( function(x ) { | ||
var external; | ||
if ( x.passthrough ) { | ||
return; | ||
} | ||
external = hasOwnProp.call( bundle.externalModuleLookup, x.id ); | ||
x.specifiers.forEach( function(s ) { | ||
var moduleId, mod, moduleName, specifierName, name, replacement, hash, isChained, separatorIndex; | ||
moduleId = x.id; | ||
mod = bundle.moduleLookup[ moduleId ]; | ||
if ( s.batch ) { | ||
name = s.name; | ||
replacement = bundle.uniqueNames[ moduleId ]; | ||
} else { | ||
name = s.as; | ||
specifierName = s.name; | ||
// If this is a chained import, get the origin | ||
hash = moduleId + '@' + specifierName; | ||
while ( hasOwnProp.call( bundle.chains, hash ) ) { | ||
hash = bundle.chains[ hash ]; | ||
isChained = true; | ||
} | ||
if ( isChained ) { | ||
separatorIndex = hash.indexOf( '@' ); | ||
moduleId = hash.substr( 0, separatorIndex ); | ||
specifierName = hash.substring( separatorIndex + 1 ); | ||
} | ||
moduleName = bundle.uniqueNames[ moduleId ]; | ||
if ( specifierName === 'default' ) { | ||
// if it's an external module, always use __default | ||
if ( bundle.externalModuleLookup[ moduleId ] ) { | ||
replacement = moduleName + '__default'; | ||
} | ||
else if ( mod && mod.defaultExport && mod.defaultExport.declaration && mod.defaultExport.name ) { | ||
replacement = hasOwnProp.call( conflicts, mod.defaultExport.name ) ? | ||
moduleName + '__' + mod.defaultExport.name : | ||
mod.defaultExport.name; | ||
} | ||
else { | ||
replacement = hasOwnProp.call( conflicts, moduleName ) || otherModulesDeclare( bundle.moduleLookup[ moduleId ], moduleName ) ? | ||
moduleName + '__default' : | ||
moduleName; | ||
} | ||
} else if ( !external ) { | ||
replacement = hasOwnProp.call( conflicts, specifierName ) ? | ||
moduleName + '__' + specifierName : | ||
specifierName; | ||
} else { | ||
replacement = moduleName + '.' + specifierName; | ||
} | ||
} | ||
moduleIdentifiers[ name ] = { | ||
name: replacement, | ||
readOnly: true | ||
}; | ||
}); | ||
}); | ||
// TODO is this necessary? Or only necessary in with default | ||
// exports that are expressions? | ||
if ( x = mod.defaultExport ) { | ||
if ( x.declaration && x.name ) { | ||
moduleIdentifiers.default = { | ||
name: hasOwnProp.call( conflicts, x.name ) || otherModulesDeclare( null, prefix ) ? | ||
prefix + '__' + x.name : | ||
x.name | ||
}; | ||
} else { | ||
moduleIdentifiers.default = { | ||
name: hasOwnProp.call( conflicts, prefix ) || otherModulesDeclare( null, prefix ) ? | ||
prefix + '__default' : | ||
prefix | ||
}; | ||
} | ||
} | ||
}); | ||
function otherModulesDeclare ( mod, replacement ) { | ||
var i, otherMod; | ||
i = bundle.modules.length; | ||
while ( i-- ) { | ||
otherMod = bundle.modules[i]; | ||
if ( mod === otherMod ) { | ||
continue; | ||
} | ||
if ( hasOwnProp.call( otherMod.ast._declared, replacement ) ) { | ||
return true; | ||
} | ||
} | ||
} | ||
return identifiers; | ||
} | ||
function resolveExports ( bundle ) { | ||
var bundleExports = {}; | ||
bundle.entryModule.exports.forEach( function(x ) { | ||
var name; | ||
if ( x.specifiers ) { | ||
x.specifiers.forEach( function(s ) { | ||
var hash = bundle.entryModule.id + '@' + s.name, | ||
split, | ||
moduleId, | ||
name; | ||
while ( bundle.chains[ hash ] ) { | ||
hash = bundle.chains[ hash ]; | ||
} | ||
split = hash.split( '@' ); | ||
moduleId = split[0]; | ||
name = split[1]; | ||
addExport( moduleId, name, s.name ); | ||
// if ( !bundleExports[ moduleId ] ) { | ||
// bundleExports[ moduleId ] = {}; | ||
// } | ||
// bundleExports[ moduleId ][ name ] = s.name; | ||
}); | ||
} | ||
else if ( !x.default && ( name = x.name ) ) { | ||
addExport( bundle.entry, name, name ); | ||
} | ||
}); | ||
function addExport ( moduleId, name, as ) { | ||
if ( !bundleExports[ moduleId ] ) { | ||
bundleExports[ moduleId ] = {}; | ||
} | ||
bundleExports[ moduleId ][ name ] = as; | ||
} | ||
return bundleExports; | ||
} | ||
function disallowIllegalReassignment ( node, names, scope ) { | ||
var assignee, name, message; | ||
if ( node.type === 'AssignmentExpression' ) { | ||
@@ -612,11 +927,9 @@ assignee = node.left; | ||
name = assignee.name; | ||
replacement = names.hasOwnProperty( name ) && names[ name ]; | ||
if ( !!replacement && !scope.contains( name ) ) { | ||
if ( hasOwnProp.call( names, name ) && names[ name ].readOnly && !scope.contains( name ) ) { | ||
throw new Error( message + '`' + name + '`' ); | ||
} | ||
} | ||
var disallowIllegalReassignment__default = disallowIllegalReassignment__disallowIllegalReassignment; | ||
function rewriteIdentifiers__rewriteIdentifiers ( body, node, toRewrite, scope ) { | ||
function rewriteIdentifiers ( body, node, identifierReplacements, scope ) { | ||
var name, replacement; | ||
@@ -626,5 +939,5 @@ | ||
name = node.name; | ||
replacement = toRewrite.hasOwnProperty( name ) && toRewrite[ name ]; | ||
replacement = hasOwnProp.call( identifierReplacements, name ) && identifierReplacements[ name ].name; | ||
if ( replacement && !scope.contains( name ) ) { | ||
if ( replacement && !scope.contains( name, true ) ) { | ||
// rewrite | ||
@@ -635,89 +948,53 @@ body.replace( node.start, node.end, replacement ); | ||
} | ||
var rewriteIdentifiers__default = rewriteIdentifiers__rewriteIdentifiers; | ||
function gatherImports__gatherImports ( imports, externalModuleLookup, importedBindings, toRewrite, chains, uniqueNames ) { | ||
var replacements = {}; | ||
function rewriteExportAssignments ( body, node, exports, scope, alreadyExported, isTopLevelNode, capturedUpdates ) { | ||
var assignee, name, exportAs; | ||
imports.forEach( function(x ) { | ||
var external; | ||
if ( node.type === 'AssignmentExpression' ) { | ||
assignee = node.left; | ||
} else if ( node.type === 'UpdateExpression' ) { | ||
assignee = node.argument; | ||
} else { | ||
return; // not an assignment | ||
} | ||
if ( externalModuleLookup.hasOwnProperty( x.path ) ) { | ||
external = true; | ||
if ( assignee.type !== 'Identifier' ) { | ||
return; | ||
} | ||
name = assignee.name; | ||
if ( exports && ( exportAs = exports[ name ] ) ) { | ||
if ( !!capturedUpdates ) { | ||
capturedUpdates.push({ | ||
name: name, | ||
exportAs: exportAs | ||
}); | ||
return; | ||
} | ||
x.specifiers.forEach( function(s ) { | ||
var moduleId, moduleName, specifierName, name, replacement, hash, isChained, separatorIndex; | ||
// special case - increment/decrement operators | ||
if ( node.operator === '++' || node.operator === '--' ) { | ||
body.replace( node.end, node.end, ((", exports." + exportAs) + (" = " + name) + "") ); | ||
} else { | ||
body.replace( node.start, node.start, (("exports." + exportAs) + " = ") ); | ||
} | ||
moduleId = x.id; | ||
if ( s.batch ) { | ||
name = s.name; | ||
replacement = uniqueNames[ moduleId ]; | ||
} else { | ||
name = s.as; | ||
specifierName = s.name; | ||
// If this is a chained import, get the origin | ||
hash = moduleId + '@' + specifierName; | ||
while ( chains.hasOwnProperty( hash ) ) { | ||
hash = chains[ hash ]; | ||
isChained = true; | ||
} | ||
if ( isChained ) { | ||
separatorIndex = hash.indexOf( '@' ); | ||
moduleId = hash.substr( 0, separatorIndex ); | ||
specifierName = hash.substring( separatorIndex + 1 ); | ||
} | ||
moduleName = uniqueNames[ moduleId ]; | ||
if ( !external || specifierName === 'default' ) { | ||
replacement = moduleName + '__' + specifierName; | ||
} else { | ||
replacement = moduleName + '.' + specifierName; | ||
} | ||
} | ||
importedBindings[ name ] = replacement; | ||
if ( !x.passthrough ) { | ||
toRewrite[ name ] = replacement; | ||
} | ||
}); | ||
}); | ||
return replacements; | ||
// keep track of what we've already exported - we don't need to | ||
// export it again later | ||
if ( isTopLevelNode ) { | ||
alreadyExported[ name ] = true; | ||
} | ||
} | ||
} | ||
var gatherImports__default = gatherImports__gatherImports; | ||
function transformBody__transformBody ( bundle, mod, body, prefix ) { | ||
var scope, | ||
scopeNames, | ||
blockScope, | ||
importedBindings = {}, | ||
toRewrite = {}, | ||
readOnly = {}, | ||
exportNames = [], | ||
alreadyExported = {}, | ||
shouldExportEarly = {}, | ||
defaultValue, | ||
indentExclusionRanges = []; | ||
function traverseAst ( ast, body, identifierReplacements, exportNames, alreadyExported ) { | ||
var scope, blockScope, capturedUpdates; | ||
scope = mod.ast._scope; | ||
blockScope = mod.ast._blockScope; | ||
scope = ast._scope; | ||
blockScope = ast._blockScope; | ||
gatherImports__default( mod.imports, bundle.externalModuleLookup, importedBindings, toRewrite, bundle.chains, bundle.uniqueNames ); | ||
Object.keys( toRewrite ).forEach( function(k ) {return readOnly[k] = toRewrite[k]} ); | ||
capturedUpdates = null; | ||
scope.names.forEach( function(n ) {return toRewrite[n] = prefix + '__' + n} ); | ||
// remove top-level names from scope. TODO is there a cleaner way to do this? | ||
scopeNames = scope.names; | ||
scope.names = []; | ||
//gatherExports( mod.exports, toRewrite, prefix ); | ||
//exportNames = getExportNames( mod.exports ); | ||
estraverse__default.traverse( mod.ast, { | ||
// scope is now the global scope | ||
estraverse__default.traverse( ast, { | ||
enter: function ( node, parent ) { | ||
@@ -733,20 +1010,32 @@ // we're only interested in references, not property names etc | ||
// Special case: if you have a variable declaration that updates existing | ||
// bindings as a side-effect, e.g. `var a = b++`, where `b` is an exported | ||
// value, we can't simply append `exports.b = b` to the update (as we | ||
// normally would) because that would be syntactically invalid. Instead, | ||
// we capture the change and update the export (and any others) after the | ||
// variable declaration | ||
if ( node.type === 'VariableDeclaration' ) { | ||
var previous = capturedUpdates; | ||
capturedUpdates = []; | ||
capturedUpdates.previous = previous; | ||
} | ||
// Catch illegal reassignments | ||
disallowIllegalReassignment__default( node, readOnly, scope ); | ||
disallowIllegalReassignment( node, identifierReplacements, scope ); | ||
// Rewrite assignments to exports | ||
transformBody__rewriteExportAssignments( body, node, exportNames, scope, alreadyExported, ~mod.ast.body.indexOf( parent ) ); | ||
rewriteExportAssignments( body, node, exportNames, scope, alreadyExported, ~ast.body.indexOf( parent ), capturedUpdates ); | ||
// Rewrite import and export identifiers | ||
rewriteIdentifiers__default( body, node, toRewrite, scope ); | ||
// Add multi-line strings to exclusion ranges | ||
if ( node.type === 'TemplateLiteral' ) { | ||
indentExclusionRanges.push([ node.start, node.end ]); | ||
} | ||
// Replace identifiers | ||
rewriteIdentifiers( body, node, identifierReplacements, scope ); | ||
}, | ||
leave: function ( node ) { | ||
if ( node.type === 'Program' ) { | ||
return; | ||
// Special case - see above | ||
if ( node.type === 'VariableDeclaration' ) { | ||
if ( capturedUpdates.length ) { | ||
body.replace( node.end, node.end, capturedUpdates.map( function(c ) {return ((" exports." + (c.name)) + (" = " + (c.exportAs)) + ";")} ).join( '' ) ); | ||
} | ||
capturedUpdates = capturedUpdates.previous; | ||
} | ||
@@ -761,5 +1050,17 @@ | ||
}); | ||
} | ||
scope.names = scopeNames; | ||
function transformBody__transformBody ( bundle, mod, body ) { | ||
var identifierReplacements, | ||
exportNames, | ||
alreadyExported = {}, | ||
shouldExportEarly = {}, | ||
exportBlock; | ||
identifierReplacements = bundle.identifierReplacements[ mod.id ]; | ||
exportNames = bundle.exports[ mod.id ]; | ||
traverseAst( mod.ast, body, identifierReplacements, exportNames, alreadyExported ); | ||
// remove imports | ||
@@ -777,3 +1078,3 @@ mod.imports.forEach( function(x ) { | ||
if ( x.default ) { | ||
if ( x.node.declaration && x.node.declaration.id && ( name = x.node.declaration.id.name ) ) { | ||
if ( x.type === 'namedFunction' || x.type === 'namedClass' ) { | ||
// if you have a default export like | ||
@@ -789,15 +1090,19 @@ // | ||
// as the `foo` reference may be used elsewhere | ||
defaultValue = body.slice( x.valueStart, x.end ); // in case rewrites occured inside the function body | ||
// remove the `export default `, keep the rest | ||
body.remove( x.start, x.valueStart ); | ||
body.replace( x.end, x.end, '\nvar ' + prefix + '__default = ' + prefix + '__' + name + ';' ); | ||
} else { | ||
// TODO this is a bit convoluted... | ||
if ( x.node.declaration && ( name = x.node.declaration.name ) ) { | ||
defaultValue = prefix + '__' + name; | ||
body.replace( x.start, x.end, 'var ' + prefix + '__default = ' + defaultValue + ';' ); | ||
} | ||
else if ( x.node.declaration && ( name = x.node.declaration.name ) ) { | ||
if ( name === identifierReplacements.default.name ) { | ||
body.remove( x.start, x.end ); | ||
} else { | ||
body.replace( x.start, x.valueStart, 'var ' + prefix + '__default = ' ); | ||
body.replace( x.start, x.end, (("var " + (identifierReplacements.default.name)) + (" = " + (identifierReplacements[name].name)) + ";") ); | ||
} | ||
} | ||
else { | ||
body.replace( x.start, x.valueStart, (("var " + (identifierReplacements.default.name)) + " = ") ); | ||
} | ||
return; | ||
@@ -807,4 +1112,4 @@ } | ||
if ( x.declaration ) { | ||
if ( x.node.declaration.type === 'FunctionDeclaration' ) { | ||
shouldExportEarly[ x.node.declaration.id.name ] = true; // TODO what about `function foo () {}; export { foo }`? | ||
if ( x.type === 'namedFunction' ) { | ||
shouldExportEarly[ x.name ] = true; // TODO what about `function foo () {}; export { foo }`? | ||
} | ||
@@ -819,3 +1124,4 @@ | ||
if ( mod._exportsNamespace ) { | ||
var namespaceExportBlock = 'var ' + prefix + ' = {\n', | ||
var prefix = bundle.uniqueNames[ mod.id ], | ||
namespaceExportBlock = (("var " + prefix) + " = {\n"), | ||
namespaceExports = []; | ||
@@ -825,7 +1131,7 @@ | ||
if ( x.declaration ) { | ||
namespaceExports.push( body.indentStr + 'get ' + x.name + ' () { return ' + prefix + '__' + x.name + '; }' ); | ||
namespaceExports.push( body.indentStr + (("get " + (x.name)) + (" () { return " + (identifierReplacements[x.name].name)) + "; }") ); | ||
} | ||
else if ( x.default ) { | ||
namespaceExports.push( body.indentStr + 'get default () { return ' + prefix + '__default; }' ); | ||
namespaceExports.push( body.indentStr + (("get default () { return " + (identifierReplacements.default.name)) + "; }") ); | ||
} | ||
@@ -835,3 +1141,3 @@ | ||
x.specifiers.forEach( function(s ) { | ||
namespaceExports.push( body.indentStr + 'get ' + s.name + ' () { return ' + s.name + '; }' ); | ||
namespaceExports.push( body.indentStr + (("get " + (s.name)) + (" () { return " + (s.name)) + "; }") ); | ||
}); | ||
@@ -846,64 +1152,68 @@ } | ||
body.trim().indent({ exclude: indentExclusionRanges }); | ||
} | ||
var transformBody__default = transformBody__transformBody; | ||
if ( exportNames ) { | ||
exportBlock = []; | ||
function transformBody__rewriteExportAssignments ( body, node, exports, scope, alreadyExported, isTopLevelNode ) { | ||
var assignee, name; | ||
Object.keys( exportNames ).forEach( function(name ) { | ||
var exportAs; | ||
if ( node.type === 'AssignmentExpression' ) { | ||
assignee = node.left; | ||
} else if ( node.type === 'UpdateExpression' ) { | ||
assignee = node.argument; | ||
} else { | ||
return; // not an assignment | ||
} | ||
if ( !alreadyExported[ name ] ) { | ||
exportAs = exportNames[ name ]; | ||
if ( assignee.type !== 'Identifier' ) { | ||
return; | ||
} | ||
exportBlock.push( (("exports." + exportAs) + (" = " + (identifierReplacements[name].name)) + ";") ); | ||
} | ||
}); | ||
name = assignee.name; | ||
if ( ~exports.indexOf( name ) ) { | ||
// special case - increment/decrement operators | ||
if ( node.operator === '++' || node.operator === '--' ) { | ||
body.replace( node.end, node.end, ((", exports." + name) + (" = " + name) + "") ); | ||
} else { | ||
body.replace( node.start, node.start, (("exports." + name) + " = ") ); | ||
if ( exportBlock.length ) { | ||
body.trim().append( '\n\n' + exportBlock.join( '\n' ) ); | ||
} | ||
} | ||
// keep track of what we've already exported - we don't need to | ||
// export it again later | ||
if ( isTopLevelNode ) { | ||
alreadyExported[ name ] = true; | ||
} | ||
} | ||
return body.trim(); | ||
} | ||
function combine__combine ( bundle ) { | ||
var body = new MagicString__default.Bundle({ | ||
function combine ( bundle ) { | ||
var body; | ||
body = new MagicString__default.Bundle({ | ||
separator: '\n\n' | ||
}); | ||
bundle.identifierReplacements = getIdentifierReplacements( bundle ); | ||
bundle.exports = resolveExports( bundle ); | ||
bundle.modules.forEach( function(mod ) { | ||
var modBody = mod.body.clone(), | ||
prefix = bundle.uniqueNames[ mod.id ]; | ||
// verify that this module doesn't import non-exported identifiers | ||
mod.imports.forEach( function(x ) { | ||
var importedModule = bundle.moduleLookup[ x.id ]; | ||
annotateAst__default( mod.ast ); | ||
transformBody__default( bundle, mod, modBody, prefix ); | ||
if ( !importedModule ) { | ||
return; | ||
} | ||
x.specifiers.forEach( function(s ) { | ||
if ( s.batch ) { | ||
return; | ||
} | ||
if ( !importedModule.doesExport[ s.name ] ) { | ||
throw new Error( 'Module ' + importedModule.id + ' does not export ' + s.name + ' (imported by ' + mod.id + ')' ); | ||
} | ||
}); | ||
}); | ||
body.addSource({ | ||
filename: path__default.resolve( bundle.base, mod.file ), | ||
content: modBody | ||
content: transformBody__transformBody( bundle, mod, mod.body.clone() ), | ||
indentExclusionRanges: mod.ast._templateLiteralRanges | ||
}); | ||
}); | ||
bundle.body = body; | ||
bundle.body = body.indent(); | ||
} | ||
var combine__default = combine__combine; | ||
function getModule__getStandaloneModule ( mod ) { | ||
function getModule ( mod ) {var $D$1; | ||
var imports, exports; | ||
mod.body = new MagicString__default( mod.source ); | ||
mod.imports = []; | ||
mod.exports = []; | ||
@@ -915,2 +1225,4 @@ try { | ||
}); | ||
annotateAst( mod.ast ); | ||
} catch ( err ) { | ||
@@ -925,11 +1237,36 @@ // If there's a parse error, attach file info | ||
findImportsAndExports__default( mod, mod.source, mod.ast, mod.imports, mod.exports ); | ||
imports = ($D$1 = findImportsAndExports( mod, mod.source, mod.ast ))[0], exports = $D$1[1], $D$1; | ||
mod.imports = imports; | ||
mod.exports = exports; | ||
// collect exports by name, for quick lookup when verifying | ||
// that this module exports a given identifier | ||
mod.doesExport = {}; | ||
exports.forEach( function(x ) { | ||
if ( x.default ) { | ||
mod.doesExport.default = true; | ||
} | ||
else if ( x.name ) { | ||
mod.doesExport[ x.name ] = true; | ||
} | ||
else if ( x.specifiers ) { | ||
x.specifiers.forEach( function(s ) { | ||
mod.doesExport[ s.name ] = true; | ||
}); | ||
} | ||
else { | ||
throw new Error( 'Unexpected export type' ); | ||
} | ||
}); | ||
return mod; | ||
} | ||
var getModule__default = getModule__getStandaloneModule; | ||
;$D$1 = void 0} | ||
var getBundle__Promise = sander__default.Promise; | ||
var Promise = sander__default.Promise; | ||
function getBundle__getBundle ( options ) { | ||
function getBundle ( options ) { | ||
var entry = options.entry.replace( /\.js$/, '' ), | ||
@@ -949,27 +1286,26 @@ modules = [], | ||
return fetchModule( entry ) | ||
.then( function() { | ||
var entryModule, bundle; | ||
return fetchModule( entry ).then( function() { | ||
var entryModule, bundle; | ||
entryModule = moduleLookup[ entry ]; | ||
modules = sortModules__default( entryModule, moduleLookup ); // TODO is this necessary? surely it's already sorted because of the fetch order? or do we need to prevent parallel reads? | ||
entryModule = moduleLookup[ entry ]; | ||
modules = sortModules( entryModule, moduleLookup ); // TODO is this necessary? surely it's already sorted because of the fetch order? or do we need to prevent parallel reads? | ||
bundle = { | ||
entry: entry, | ||
entryModule: entryModule, | ||
base: base, | ||
modules: modules, | ||
moduleLookup: moduleLookup, | ||
externalModules: externalModules, | ||
externalModuleLookup: externalModuleLookup, | ||
skip: skip, | ||
names: names, | ||
uniqueNames: getUniqueNames__default( modules, options.names ), | ||
chains: resolveChains__default( modules, moduleLookup ) | ||
}; | ||
bundle = { | ||
entry: entry, | ||
entryModule: entryModule, | ||
base: base, | ||
modules: modules, | ||
moduleLookup: moduleLookup, | ||
externalModules: externalModules, | ||
externalModuleLookup: externalModuleLookup, | ||
skip: skip, | ||
names: names, | ||
uniqueNames: getUniqueNames( modules, externalModules, options.names ), | ||
chains: resolveChains( modules, moduleLookup ) | ||
}; | ||
combine__default( bundle ); | ||
combine( bundle ); | ||
return bundle; | ||
}); | ||
return bundle; | ||
}); | ||
@@ -981,3 +1317,3 @@ function fetchModule ( moduleId ) { | ||
if ( !promiseById.hasOwnProperty( moduleId ) ) { | ||
if ( !hasOwnProp.call( promiseById, moduleId ) ) { | ||
promiseById[ moduleId ] = sander__default.readFile( modulePath ).catch( function ( err ) { | ||
@@ -993,3 +1329,3 @@ if ( err.code === 'ENOENT' ) { | ||
module = getModule__default({ | ||
module = getModule({ | ||
source: source, | ||
@@ -1007,3 +1343,3 @@ id: moduleId, | ||
importId = resolve__default( x.path, module.file ); | ||
importId = resolve( x.path, module.file ); | ||
@@ -1016,3 +1352,3 @@ // Some modules can be skipped | ||
// short-circuit cycles | ||
if ( promiseById[ importId ] ) { | ||
if ( hasOwnProp.call( promiseById, importId ) ) { | ||
return; | ||
@@ -1024,3 +1360,3 @@ } | ||
return getBundle__Promise.all( promises ); | ||
return Promise.all( promises ); | ||
}).catch( function ( err ) { | ||
@@ -1035,3 +1371,3 @@ var externalModule; | ||
// Most likely an external module | ||
if ( !externalModuleLookup.hasOwnProperty( moduleId ) ) { | ||
if ( !hasOwnProp.call( externalModuleLookup, moduleId ) ) { | ||
externalModule = { | ||
@@ -1053,5 +1389,4 @@ id: moduleId | ||
} | ||
var getBundle__default = getBundle__getBundle; | ||
function transformExportDeclaration__transformExportDeclaration ( declaration, body ) { | ||
function transformExportDeclaration ( declaration, body ) { | ||
var exportedValue; | ||
@@ -1097,7 +1432,6 @@ | ||
} | ||
var transformExportDeclaration__default = transformExportDeclaration__transformExportDeclaration; | ||
var packageResult__warned = {}; | ||
var warned = {}; | ||
function packageResult__packageResult ( body, options, methodName, isBundle ) { | ||
function packageResult ( body, options, methodName, isBundle ) { | ||
var code, map; | ||
@@ -1116,3 +1450,3 @@ | ||
file: options.sourceMapFile, | ||
source: !isBundle ? packageResult__getRelativePath( options.sourceMapFile, options.sourceMapSource ) : null | ||
source: !isBundle ? getRelativePath( options.sourceMapFile, options.sourceMapSource ) : null | ||
}); | ||
@@ -1134,5 +1468,5 @@ | ||
toString: function () { | ||
if ( !packageResult__warned[ methodName ] ) { | ||
if ( !warned[ methodName ] ) { | ||
console.log( 'Warning: esperanto.' + methodName + '() returns an object with a \'code\' property. You should use this instead of using the returned value directly' ); | ||
packageResult__warned[ methodName ] = true; | ||
warned[ methodName ] = true; | ||
} | ||
@@ -1144,5 +1478,4 @@ | ||
} | ||
var packageResult__default = packageResult__packageResult; | ||
function packageResult__getRelativePath ( from, to ) { | ||
function getRelativePath ( from, to ) { | ||
var fromParts, toParts, i; | ||
@@ -1171,4 +1504,47 @@ | ||
var amd__template = 'define(__IMPORT_PATHS__function (__IMPORT_NAMES__) {\n\n'; | ||
function reorderImports ( imports ) { | ||
var i; | ||
// ensure empty imports are at the end | ||
i = imports.length; | ||
while ( i-- ) { | ||
if ( !imports[i].specifiers.length ) { | ||
imports.splice( imports.length - 1, 0, imports.splice( i, 1 )[0] ); | ||
} | ||
} | ||
} | ||
/** | ||
* Creates a template function from a template string. The template | ||
may have `<%= someVar %>` interpolators, and the returned function | ||
should be called with a data object e.g. `{ someVar: 'someData' }` | ||
* @param {string} str - the template string | ||
* @returns {function} | ||
*/ | ||
function template ( str ) { | ||
return function ( data ) { | ||
return str.replace( /<%=\s*([^\s]+)\s*%>/g, function ( match, $1 ) { | ||
return $1 in data ? data[ $1 ] : match; | ||
}); | ||
}; | ||
} | ||
function getId ( m ) { | ||
return m.id; | ||
} | ||
function quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
function mappers__req ( path ) { | ||
return 'require(\'' + path + '\')'; | ||
} | ||
function globalify ( name ) { | ||
return 'global.' + name; | ||
} | ||
var amd__introTemplate = template( 'define(<%= IMPORT_PATHS %>function (<%= IMPORT_NAMES %>) {\n\n' ); | ||
function amd__amd ( mod, body, options ) { | ||
@@ -1181,8 +1557,3 @@ var importNames = [], | ||
// ensure empty imports are at the end | ||
i = mod.imports.length; | ||
while ( i-- ) { | ||
if ( !mod.imports[i].specifiers.length ) { | ||
mod.imports.splice( mod.imports.length - 1, 0, mod.imports.splice( i, 1 )[0] ); | ||
} | ||
} | ||
reorderImports( mod.imports ); | ||
@@ -1203,3 +1574,3 @@ // gather imports, and remove import declarations | ||
transformExportDeclaration__default( mod.exports[0], body ); | ||
transformExportDeclaration( mod.exports[0], body ); | ||
@@ -1210,16 +1581,12 @@ body.trim(); | ||
intro = amd__template | ||
.replace( '__IMPORT_PATHS__', importPaths.length ? '[' + importPaths.map( amd__quote ).join( ', ' ) + '], ' : '' ) | ||
.replace( '__IMPORT_NAMES__', importNames.join( ', ' ) ); | ||
intro = amd__introTemplate({ | ||
IMPORT_PATHS: importPaths.length ? '[' + importPaths.map( quote ).join( ', ' ) + '], ' : '', | ||
IMPORT_NAMES: importNames.join( ', ' ) | ||
}); | ||
body.indent().prepend( intro ).append( '\n\n});' ); | ||
return packageResult__default( body, options, 'toAmd' ); | ||
return packageResult( body, options, 'toAmd' ); | ||
} | ||
var amd__default = amd__amd; | ||
function amd__quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
function cjs__cjs ( mod, body, options ) { | ||
@@ -1269,15 +1636,5 @@ var replacement, exportDeclaration; | ||
return packageResult__default( body, options, 'toCjs' ); | ||
return packageResult( body, options, 'toCjs' ); | ||
} | ||
var cjs__default = cjs__cjs; | ||
function template__template ( str ) { | ||
return function ( data ) { | ||
return str.replace( /<%=\s*([^\s]+)\s*%>/g, function ( match, $1 ) { | ||
return $1 in data ? data[ $1 ] : match; | ||
}); | ||
}; | ||
} | ||
var template__default = template__template; | ||
var umd__introTemplate; | ||
@@ -1296,8 +1653,3 @@ | ||
// ensure empty imports are at the end | ||
i = mod.imports.length; | ||
while ( i-- ) { | ||
if ( !mod.imports[i].specifiers.length ) { | ||
mod.imports.splice( mod.imports.length - 1, 0, mod.imports.splice( i, 1 )[0] ); | ||
} | ||
} | ||
reorderImports( mod.imports ); | ||
@@ -1318,3 +1670,3 @@ // gather imports, and remove import declarations | ||
transformExportDeclaration__default( mod.exports[0], body ); | ||
transformExportDeclaration( mod.exports[0], body ); | ||
@@ -1326,5 +1678,5 @@ body.trim(); | ||
intro = umd__introTemplate({ | ||
AMD_DEPS: importPaths.length ? '[' + importPaths.map( umd__quote ).join( ', ' ) + '], ' : '', | ||
CJS_DEPS: importPaths.map( umd__req ).join( ', ' ), | ||
GLOBAL_DEPS: importNames.map( umd__globalify ).join( ', ' ), | ||
AMD_DEPS: importPaths.length ? '[' + importPaths.map( quote ).join( ', ' ) + '], ' : '', | ||
CJS_DEPS: importPaths.map( mappers__req ).join( ', ' ), | ||
GLOBAL_DEPS: importNames.map( globalify ).join( ', ' ), | ||
IMPORT_NAMES: importNames.join( ', ' ), | ||
@@ -1336,19 +1688,6 @@ NAME: options.name | ||
return packageResult__default( body, options, 'toUmd' ); | ||
return packageResult( body, options, 'toUmd' ); | ||
} | ||
var umd__default = umd__umd; | ||
function umd__quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
function umd__req ( path ) { | ||
return (("require('" + path) + "')"); | ||
} | ||
function umd__globalify ( name ) { | ||
return ("global." + name); | ||
} | ||
umd__introTemplate = template__default( ("(function (global, factory) {\ | ||
umd__introTemplate = template( ("(function (global, factory) {\ | ||
\n\ | ||
@@ -1372,22 +1711,11 @@ \n 'use strict';\ | ||
var defaultsMode__default = { | ||
amd: amd__default, | ||
cjs: cjs__default, | ||
umd: umd__default | ||
var defaultsMode = { | ||
amd: amd__amd, | ||
cjs: cjs__cjs, | ||
umd: umd__umd | ||
}; | ||
function reorderImports__reorderImports ( imports ) { | ||
var i; | ||
function gatherImports ( imports, getName ) { | ||
var importedBindings = {}, identifierReplacements = {}; | ||
// ensure empty imports are at the end | ||
i = imports.length; | ||
while ( i-- ) { | ||
if ( !imports[i].specifiers.length ) { | ||
imports.splice( imports.length - 1, 0, imports.splice( i, 1 )[0] ); | ||
} | ||
} | ||
} | ||
var reorderImports__default = reorderImports__reorderImports; | ||
function utils_gatherImports__gatherImports ( imports, getName, importedBindings, toRewrite ) { | ||
imports.forEach( function(x ) { | ||
@@ -1416,11 +1744,15 @@ x.specifiers.forEach( function(s ) { | ||
if ( !x.passthrough ) { | ||
toRewrite[ name ] = replacement; | ||
identifierReplacements[ name ] = { | ||
name: replacement, | ||
readOnly: true | ||
}; | ||
} | ||
}); | ||
}); | ||
return [ importedBindings, identifierReplacements ]; | ||
} | ||
var utils_gatherImports__default = utils_gatherImports__gatherImports; | ||
function getExportNames__getExportNames ( exports ) { | ||
var result = []; | ||
function getExportNames ( exports ) { | ||
var result = {}; | ||
@@ -1431,3 +1763,3 @@ exports.forEach( function(x ) { | ||
if ( x.declaration ) { | ||
result.push( x.name ); | ||
result[ x.name ] = x.name; | ||
return; | ||
@@ -1437,3 +1769,3 @@ } | ||
x.specifiers.forEach( function(s ) { | ||
result.push( s.name ); | ||
result[ s.name ] = s.name; | ||
}); | ||
@@ -1444,9 +1776,6 @@ }); | ||
} | ||
var getExportNames__default = getExportNames__getExportNames; | ||
function utils_transformBody__transformBody ( mod, body, options ) { | ||
var scope, | ||
blockScope, | ||
importedBindings = {}, | ||
toRewrite = {}, | ||
function utils_transformBody__transformBody ( mod, body, options ) {var $D$2; | ||
var importedBindings, | ||
identifierReplacements, | ||
exportNames = [], | ||
@@ -1457,69 +1786,9 @@ alreadyExported = {}, | ||
lateExports, | ||
defaultValue, | ||
capturedUpdates = null, | ||
indentExclusionRanges = []; | ||
defaultValue; | ||
scope = mod.ast._scope; | ||
blockScope = mod.ast._blockScope; | ||
importedBindings = ($D$2 = gatherImports( mod.imports, mod.getName ))[0], identifierReplacements = $D$2[1], $D$2; | ||
exportNames = getExportNames( mod.exports ); | ||
utils_gatherImports__default( mod.imports, mod.getName, importedBindings, toRewrite ); | ||
exportNames = getExportNames__default( mod.exports ); | ||
traverseAst( mod.ast, body, identifierReplacements, exportNames, alreadyExported ); | ||
// scope is now the global scope | ||
estraverse__default.traverse( mod.ast, { | ||
enter: function ( node, parent ) { | ||
// we're only interested in references, not property names etc | ||
if ( node._skip ) return this.skip(); | ||
if ( node._scope ) { | ||
scope = node._scope; | ||
} else if ( node._blockScope ) { | ||
blockScope = node._blockScope; | ||
} | ||
// Special case: if you have a variable declaration that updates existing | ||
// bindings as a side-effect, e.g. `var a = b++`, where `b` is an exported | ||
// value, we can't simply append `exports.b = b` to the update (as we | ||
// normally would) because that would be syntactically invalid. Instead, | ||
// we capture the change and update the export (and any others) after the | ||
// variable declaration | ||
if ( node.type === 'VariableDeclaration' ) { | ||
var previous = capturedUpdates; | ||
capturedUpdates = []; | ||
capturedUpdates.previous = previous; | ||
} | ||
// Catch illegal reassignments | ||
disallowIllegalReassignment__default( node, toRewrite, scope ); | ||
// Rewrite assignments to exports | ||
utils_transformBody__rewriteExportAssignments( body, node, exportNames, scope, alreadyExported, ~mod.ast.body.indexOf( parent ), capturedUpdates ); | ||
// Rewrite import identifiers | ||
rewriteIdentifiers__default( body, node, toRewrite, scope ); | ||
// Add multi-line strings to exclusion ranges | ||
if ( node.type === 'TemplateLiteral' ) { | ||
indentExclusionRanges.push([ node.start, node.end ]); | ||
} | ||
}, | ||
leave: function ( node ) { | ||
// Special case - see above | ||
if ( node.type === 'VariableDeclaration' ) { | ||
if ( capturedUpdates.length ) { | ||
body.replace( node.end, node.end, capturedUpdates.map( function(n ) {return ((" exports." + n) + (" = " + n) + ";")} ).join( '' ) ); | ||
} | ||
capturedUpdates = capturedUpdates.previous; | ||
} | ||
if ( node._scope ) { | ||
scope = scope.parent; | ||
} else if ( node._blockScope ) { | ||
blockScope = blockScope.parent; | ||
} | ||
} | ||
}); | ||
// Remove import statements | ||
@@ -1576,12 +1845,14 @@ mod.imports.forEach( function(x ) { | ||
exportNames.forEach( function(name ) { | ||
var chain; | ||
Object.keys( exportNames ).forEach( function(name ) { | ||
var exportAs, chain; | ||
exportAs = exportNames[ name ]; | ||
if ( chain = importedBindings[ name ] ) { | ||
// special case - a binding from another module | ||
earlyExports.push( (("Object.defineProperty(exports, '" + name) + ("', { get: function () { return " + chain) + "; }});") ); | ||
earlyExports.push( (("Object.defineProperty(exports, '" + exportAs) + ("', { get: function () { return " + chain) + "; }});") ); | ||
} else if ( shouldExportEarly[ name ] ) { | ||
earlyExports.push( (("exports." + name) + (" = " + name) + ";") ); | ||
earlyExports.push( (("exports." + exportAs) + (" = " + name) + ";") ); | ||
} else if ( !alreadyExported[ name ] ) { | ||
lateExports.push( (("exports." + name) + (" = " + name) + ";") ); | ||
lateExports.push( (("exports." + exportAs) + (" = " + name) + ";") ); | ||
} | ||
@@ -1605,42 +1876,18 @@ }); | ||
body.trim().indent({ | ||
exclude: indentExclusionRanges.length ? indentExclusionRanges : null | ||
exclude: mod.ast._templateLiteralRanges | ||
}).prepend( options.intro ).trim().append( options.outro ); | ||
} | ||
var utils_transformBody__default = utils_transformBody__transformBody; | ||
;$D$2 = void 0} | ||
function utils_transformBody__rewriteExportAssignments ( body, node, exports, scope, alreadyExported, isTopLevelNode, capturedUpdates ) { | ||
var assignee, name; | ||
function getImportSummary ( mod ) { | ||
var importPaths = [], importNames = []; | ||
if ( node.type === 'AssignmentExpression' ) { | ||
assignee = node.left; | ||
} else if ( node.type === 'UpdateExpression' ) { | ||
assignee = node.argument; | ||
} else { | ||
return; // not an assignment | ||
} | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
if ( assignee.type !== 'Identifier' ) { | ||
return; | ||
} | ||
name = assignee.name; | ||
if ( ~exports.indexOf( name ) ) { | ||
if ( !!capturedUpdates ) { | ||
capturedUpdates.push( name ); | ||
return; | ||
if ( x.specifiers.length ) { // don't add empty imports | ||
importNames[i] = mod.getName( x ); | ||
} | ||
}); | ||
// special case - increment/decrement operators | ||
if ( node.operator === '++' || node.operator === '--' ) { | ||
body.replace( node.end, node.end, ((", exports." + name) + (" = " + name) + "") ); | ||
} else { | ||
body.replace( node.start, node.start, (("exports." + name) + " = ") ); | ||
} | ||
// keep track of what we've already exported - we don't need to | ||
// export it again later | ||
if ( isTopLevelNode ) { | ||
alreadyExported[ name ] = true; | ||
} | ||
} | ||
return [ importPaths, importNames ]; | ||
} | ||
@@ -1650,22 +1897,14 @@ | ||
strictMode_amd__introTemplate = template__default( 'define(<%= paths %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
strictMode_amd__introTemplate = template( 'define(<%= paths %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function strictMode_amd__amd ( mod, body, options ) { | ||
var importPaths = [], | ||
importNames = [], | ||
intro, | ||
i; | ||
function strictMode_amd__amd ( mod, body, options ) {var $D$3; | ||
var importPaths, | ||
importNames, | ||
intro; | ||
// ensure empty imports are at the end | ||
reorderImports__default( mod.imports ); | ||
reorderImports( mod.imports ); | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
importPaths = ($D$3 = getImportSummary( mod ))[0], importNames = $D$3[1], $D$3; | ||
if ( x.specifiers.length ) { // don't add empty imports | ||
importNames[i] = mod.getName( x ); | ||
} | ||
}); | ||
if ( mod.exports.length ) { | ||
@@ -1677,7 +1916,7 @@ importPaths.unshift( 'exports' ); | ||
intro = strictMode_amd__introTemplate({ | ||
paths: importPaths.length ? '[' + importPaths.map( strictMode_amd__quote ).join( ', ' ) + '], ' : '', | ||
paths: importPaths.length ? '[' + importPaths.map( quote ).join( ', ' ) + '], ' : '', | ||
names: importNames.join( ', ' ) | ||
}).replace( /\t/g, body.indentStr ); | ||
utils_transformBody__default( mod, body, { | ||
utils_transformBody__transformBody( mod, body, { | ||
intro: intro, | ||
@@ -1687,13 +1926,8 @@ outro: '\n\n});' | ||
return packageResult__default( body, options, 'toAmd' ); | ||
} | ||
var strictMode_amd__default = strictMode_amd__amd; | ||
return packageResult( body, options, 'toAmd' ); | ||
;$D$3 = void 0} | ||
function strictMode_amd__quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
var intro = '(function () {\n\n\t\'use strict\';\n\n'; | ||
var outro = '\n\n}).call(global);'; | ||
var strictMode_cjs__intro = '(function () {\n\n\t\'use strict\';\n\n'; | ||
var strictMode_cjs__outro = '\n\n}).call(global);'; | ||
function strictMode_cjs__cjs ( mod, body, options ) { | ||
@@ -1719,19 +1953,17 @@ var importBlock; | ||
utils_transformBody__default( mod, body, { | ||
intro: strictMode_cjs__intro.replace( /\t/g, body.indentStr ), | ||
utils_transformBody__transformBody( mod, body, { | ||
intro: intro.replace( /\t/g, body.indentStr ), | ||
header: importBlock, | ||
outro: strictMode_cjs__outro | ||
outro: outro | ||
}); | ||
return packageResult__default( body, options, 'toCjs' ); | ||
return packageResult( body, options, 'toCjs' ); | ||
} | ||
var strictMode_cjs__default = strictMode_cjs__cjs; | ||
var strictMode_umd__introTemplate; | ||
function strictMode_umd__umd ( mod, body, options ) { | ||
var importPaths = [], | ||
importNames = [], | ||
intro, | ||
i; | ||
function strictMode_umd__umd ( mod, body, options ) {var $D$4; | ||
var importPaths, | ||
importNames, | ||
intro; | ||
@@ -1742,17 +1974,10 @@ if ( !options.name ) { | ||
reorderImports__default( mod.imports ); | ||
reorderImports( mod.imports ); | ||
// gather imports, and remove import declarations | ||
mod.imports.forEach( function( x, i ) { | ||
importPaths[i] = x.path; | ||
importPaths = ($D$4 = getImportSummary( mod ))[0], importNames = $D$4[1], $D$4; | ||
if ( x.specifiers.length ) { | ||
importNames[i] = mod.getName( x ); | ||
} | ||
}); | ||
intro = strictMode_umd__introTemplate({ | ||
amdDeps: [ 'exports' ].concat( importPaths ).map( strictMode_umd__quote ).join( ', ' ), | ||
cjsDeps: [ 'exports' ].concat( importPaths.map( strictMode_umd__req ) ).join( ', ' ), | ||
globals: [ ("global." + (options.name)) ].concat( importNames.map( strictMode_umd__globalify ) ).join( ', ' ), | ||
amdDeps: [ 'exports' ].concat( importPaths ).map( quote ).join( ', ' ), | ||
cjsDeps: [ 'exports' ].concat( importPaths.map( mappers__req ) ).join( ', ' ), | ||
globals: [ ("global." + (options.name)) ].concat( importNames.map( globalify ) ).join( ', ' ), | ||
names: [ 'exports' ].concat( importNames ).join( ', ' ), | ||
@@ -1762,3 +1987,3 @@ name: options.name | ||
utils_transformBody__default( mod, body, { | ||
utils_transformBody__transformBody( mod, body, { | ||
intro: intro, | ||
@@ -1768,19 +1993,6 @@ outro: '\n\n}));' | ||
return packageResult__default( body, options, 'toUmd' ); | ||
} | ||
var strictMode_umd__default = strictMode_umd__umd; | ||
return packageResult( body, options, 'toUmd' ); | ||
;$D$4 = void 0} | ||
function strictMode_umd__quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
function strictMode_umd__req ( path ) { | ||
return 'require(\'' + path + '\')'; | ||
} | ||
function strictMode_umd__globalify ( name ) { | ||
return 'global.' + name; | ||
} | ||
strictMode_umd__introTemplate = template__default( ("(function (global, factory) {\ | ||
strictMode_umd__introTemplate = template( ("(function (global, factory) {\ | ||
\n\ | ||
@@ -1807,21 +2019,28 @@ \n 'use strict';\ | ||
var strictMode__default = { | ||
amd: strictMode_amd__default, | ||
cjs: strictMode_cjs__default, | ||
umd: strictMode_umd__default | ||
var strictMode = { | ||
amd: strictMode_amd__amd, | ||
cjs: strictMode_cjs__cjs, | ||
umd: strictMode_umd__umd | ||
}; | ||
// TODO rewrite with named imports/exports | ||
var moduleBuilders__default = { | ||
defaultsMode: defaultsMode__default, | ||
strictMode: strictMode__default | ||
var moduleBuilders = { | ||
defaultsMode: defaultsMode, | ||
strictMode: strictMode | ||
}; | ||
var defaultsMode_amd__introTemplate = template__default( 'define(<%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function getExportName ( bundle ) { | ||
var x = bundle.entryModule.defaultExport; | ||
if ( x.declaration ) { | ||
return x.name; | ||
} | ||
return bundle.identifierReplacements[ bundle.entry ].default.name; | ||
} | ||
var defaultsMode_amd__introTemplate = template( 'define(<%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function defaultsMode_amd__amd ( bundle, body, options ) { | ||
var entry = bundle.entryModule, | ||
x, | ||
exportStatement, | ||
intro, | ||
var intro, | ||
indentStr; | ||
@@ -1831,5 +2050,4 @@ | ||
if ( x = entry.exports[0] ) { | ||
exportStatement = indentStr + 'return ' + bundle.uniqueNames[ bundle.entry ] + '__default;'; | ||
body.append( '\n\n' + exportStatement ); | ||
if ( bundle.entryModule.defaultExport ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + (getExportName(bundle))) + ";") ); | ||
} | ||
@@ -1839,9 +2057,8 @@ | ||
amdDeps: bundle.externalModules.length ? '[' + bundle.externalModules.map( defaultsMode_amd__quoteId ).join( ', ' ) + '], ' : '', | ||
names: bundle.externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ]} ).join( ', ' ) | ||
names: bundle.externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ] + '__default'} ).join( ', ' ) | ||
}).replace( /\t/g, indentStr ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
return packageResult__default( body, options, 'toAmd', true ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
} | ||
var defaultsMode_amd__default = defaultsMode_amd__amd; | ||
@@ -1854,5 +2071,3 @@ function defaultsMode_amd__quoteId ( m ) { | ||
var importBlock, | ||
entry = bundle.entryModule, | ||
x, | ||
exportStatement, | ||
intro, | ||
@@ -1872,5 +2087,4 @@ indentStr; | ||
if ( x = entry.exports[0] ) { | ||
exportStatement = indentStr + 'module.exports = ' + bundle.uniqueNames[ bundle.entry ] + '__default;'; | ||
body.append( '\n\n' + exportStatement ); | ||
if ( bundle.entryModule.defaultExport ) { | ||
body.append( (("\n\n" + indentStr) + ("module.exports = " + (getExportName(bundle))) + ";") ); | ||
} | ||
@@ -1881,5 +2095,4 @@ | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult__default( body, options, 'toCjs', true ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
} | ||
var defaultsMode_cjs__default = defaultsMode_cjs__cjs; | ||
@@ -1889,6 +2102,3 @@ var defaultsMode_umd__introTemplate; | ||
function defaultsMode_umd__umd ( bundle, body, options ) { | ||
var x, | ||
entry = bundle.entryModule, | ||
exportStatement, | ||
amdDeps, | ||
var amdDeps, | ||
cjsDeps, | ||
@@ -1905,5 +2115,4 @@ globals, | ||
if ( x = entry.exports[0] ) { | ||
exportStatement = indentStr + 'return ' + bundle.uniqueNames[ bundle.entry ] + '__default;'; | ||
body.append( '\n\n' + exportStatement ); | ||
if ( bundle.entryModule.defaultExport ) { | ||
body.append( (("\n\n" + indentStr) + ("return " + (getExportName(bundle))) + ";") ); | ||
} | ||
@@ -1924,5 +2133,4 @@ | ||
body.prepend( intro ).trim().append( '\n\n}));' ); | ||
return packageResult__default( body, options, 'toUmd', true ); | ||
return packageResult( body, options, 'toUmd', true ); | ||
} | ||
var defaultsMode_umd__default = defaultsMode_umd__umd; | ||
@@ -1937,3 +2145,3 @@ function defaultsMode_umd__quoteId ( m ) { | ||
defaultsMode_umd__introTemplate = template__default( ("(function (global, factory) {\ | ||
defaultsMode_umd__introTemplate = template( ("(function (global, factory) {\ | ||
\n\ | ||
@@ -1959,63 +2167,20 @@ \n 'use strict';\ | ||
var builders_defaultsMode__default = { | ||
amd: defaultsMode_amd__default, | ||
cjs: defaultsMode_cjs__default, | ||
umd: defaultsMode_umd__default | ||
var builders_defaultsMode = { | ||
amd: defaultsMode_amd__amd, | ||
cjs: defaultsMode_cjs__cjs, | ||
umd: defaultsMode_umd__umd | ||
}; | ||
var getExportBlock__outroTemplate; | ||
function getExportBlock ( bundle, entry, indentStr ) { | ||
var name; | ||
function getExportBlock__getExportBlock ( bundle, entry, indentStr ) { | ||
var exportBlock = '', statements = [], name; | ||
name = bundle.uniqueNames[ bundle.entry ]; | ||
// create an export block | ||
if ( entry.defaultExport ) { | ||
exportBlock = indentStr + 'exports[\'default\'] = ' + name + '__default;'; | ||
if ( bundle.entryModule.defaultExport.declaration ) { | ||
name = bundle.entryModule.defaultExport.name; | ||
} else { | ||
name = bundle.identifierReplacements[ bundle.entry ].default.name; | ||
} | ||
entry.exports.forEach( function(x ) { | ||
if ( x.default ) { | ||
return; | ||
} | ||
if ( x.declaration ) { | ||
statements.push( indentStr + (("__export('" + (x.name)) + ("', function () { return " + name) + ("__" + (x.name)) + "; });") ); | ||
} | ||
else { | ||
x.specifiers.forEach( function(s ) { | ||
statements.push( indentStr + (("__export('" + (s.name)) + ("', function () { return " + name) + ("__" + (s.name)) + "; });") ); | ||
}); | ||
} | ||
}); | ||
if ( statements.length ) { | ||
if ( exportBlock ) { | ||
exportBlock += '\n\n'; | ||
} | ||
exportBlock += getExportBlock__outroTemplate({ | ||
exportStatements: statements.join( '\n' ) | ||
}).replace( /\t/g, indentStr ); | ||
} | ||
return exportBlock; | ||
return indentStr + (("exports['default'] = " + name) + ";"); | ||
} | ||
var getExportBlock__default = getExportBlock__getExportBlock; | ||
getExportBlock__outroTemplate = template__default( ("\ | ||
\n\ | ||
\n (function (__export) {\ | ||
\n <%= exportStatements %>\ | ||
\n }(function (prop, get) {\ | ||
\n Object.defineProperty(exports, prop, {\ | ||
\n enumerable: true,\ | ||
\n get: get\ | ||
\n });\ | ||
\n }));\ | ||
\n\ | ||
\n") ); | ||
var builders_strictMode_amd__introTemplate; | ||
@@ -2026,6 +2191,4 @@ | ||
entry = bundle.entryModule, | ||
exportBlock, | ||
externalModules = bundle.externalModules, | ||
importPaths, | ||
importNames, | ||
importIds = bundle.externalModules.map( getId ), | ||
importNames = importIds.map( function(id ) {return bundle.uniqueNames[ id ]} ), | ||
intro, | ||
@@ -2036,8 +2199,7 @@ indentStr; | ||
defaultsBlock = externalModules.map( function(x ) { | ||
var name = bundle.uniqueNames[ x.id ]; | ||
return indentStr + (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
}).join( '\n' ); | ||
if ( importNames.length ) { | ||
defaultsBlock = importNames.map( function(name ) { | ||
return indentStr + (("var " + name) + ("__default = ('default' in " + name) + (" ? " + name) + ("['default'] : " + name) + ");"); | ||
}).join( '\n' ); | ||
if ( defaultsBlock ) { | ||
body.prepend( defaultsBlock + '\n\n' ); | ||
@@ -2047,14 +2209,12 @@ } | ||
if ( entry.exports.length ) { | ||
importPaths = [ 'exports' ].concat( externalModules.map( builders_strictMode_amd__getPath ) ); | ||
importNames = [ 'exports' ].concat( externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ]} ) ); | ||
importIds.unshift( 'exports' ); | ||
importNames.unshift( 'exports' ); | ||
exportBlock = getExportBlock__default( bundle, entry, indentStr ); | ||
body.append( '\n\n' + exportBlock ); | ||
} else { | ||
importPaths = externalModules.map( builders_strictMode_amd__getPath ); | ||
importNames = externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ]} ); | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( bundle, entry, indentStr ) ); | ||
} | ||
} | ||
intro = builders_strictMode_amd__introTemplate({ | ||
amdDeps: importPaths.length ? '[' + importPaths.map( builders_strictMode_amd__quote ).join( ', ' ) + '], ' : '', | ||
amdDeps: importIds.length ? '[' + importIds.map( quote ).join( ', ' ) + '], ' : '', | ||
names: importNames.join( ', ' ) | ||
@@ -2064,18 +2224,10 @@ }).replace( /\t/g, indentStr ); | ||
body.prepend( intro ).trim().append( '\n\n});' ); | ||
return packageResult__default( body, options, 'toAmd', true ); | ||
return packageResult( body, options, 'toAmd', true ); | ||
} | ||
var builders_strictMode_amd__default = builders_strictMode_amd__amd; | ||
function builders_strictMode_amd__quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
builders_strictMode_amd__introTemplate = template( 'define(<%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_amd__getPath ( m ) { return m.id; } | ||
builders_strictMode_amd__introTemplate = template__default( 'define(<%= amdDeps %>function (<%= names %>) {\n\n\t\'use strict\';\n\n' ); | ||
function builders_strictMode_cjs__cjs ( bundle, body, options ) { | ||
var importBlock, | ||
entry = bundle.entryModule, | ||
exportBlock, | ||
intro, | ||
@@ -2097,5 +2249,4 @@ indentStr; | ||
if ( entry.exports.length ) { | ||
exportBlock = getExportBlock__default( bundle, entry, indentStr ); | ||
body.append( '\n\n' + exportBlock ); | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( bundle, entry, indentStr ) ); | ||
} | ||
@@ -2106,5 +2257,4 @@ | ||
body.prepend( intro ).trim().append( '\n\n}).call(global);' ); | ||
return packageResult__default( body, options, 'toCjs', true ); | ||
return packageResult( body, options, 'toCjs', true ); | ||
} | ||
var builders_strictMode_cjs__default = builders_strictMode_cjs__cjs; | ||
@@ -2116,3 +2266,2 @@ var builders_strictMode_umd__introTemplate; | ||
entry = bundle.entryModule, | ||
exportBlock, | ||
importPaths, | ||
@@ -2142,17 +2291,18 @@ importNames, | ||
importPaths = bundle.externalModules.map( builders_strictMode_umd__getId ); | ||
importPaths = bundle.externalModules.map( getId ); | ||
importNames = bundle.externalModules.map( function(m ) {return bundle.uniqueNames[ m.id ]} ); | ||
if ( entry.exports.length ) { | ||
amdDeps = [ 'exports' ].concat( importPaths ).map( builders_strictMode_umd__quote ).join( ', ' ); | ||
cjsDeps = [ 'exports' ].concat( importPaths.map( builders_strictMode_umd__req ) ).join( ', ' ); | ||
globals = [ options.name ].concat( importNames ).map( builders_strictMode_umd__globalify ).join( ', ' ); | ||
amdDeps = [ 'exports' ].concat( importPaths ).map( quote ).join( ', ' ); | ||
cjsDeps = [ 'exports' ].concat( importPaths.map( mappers__req ) ).join( ', ' ); | ||
globals = [ options.name ].concat( importNames ).map( globalify ).join( ', ' ); | ||
names = [ 'exports' ].concat( importNames ).join( ', ' ); | ||
exportBlock = getExportBlock__default( bundle, entry, indentStr ); | ||
body.append( '\n\n' + exportBlock ); | ||
if ( entry.defaultExport ) { | ||
body.append( '\n\n' + getExportBlock( bundle, entry, indentStr ) ); | ||
} | ||
} else { | ||
amdDeps = importPaths.map( builders_strictMode_umd__quote ).join( ', ' ); | ||
cjsDeps = importPaths.map( builders_strictMode_umd__req ).join( ', ' ); | ||
globals = importNames.map( builders_strictMode_umd__globalify ).join( ', ' ); | ||
amdDeps = importPaths.map( quote ).join( ', ' ); | ||
cjsDeps = importPaths.map( mappers__req ).join( ', ' ); | ||
globals = importNames.map( globalify ).join( ', ' ); | ||
names = importNames.join( ', ' ); | ||
@@ -2170,21 +2320,6 @@ } | ||
body.prepend( intro ).trim().append( '\n\n}));' ); | ||
return packageResult__default( body, options, 'toUmd', true ); | ||
return packageResult( body, options, 'toUmd', true ); | ||
} | ||
var builders_strictMode_umd__default = builders_strictMode_umd__umd; | ||
function builders_strictMode_umd__getId ( m ) { return m.id; } | ||
function builders_strictMode_umd__quote ( str ) { | ||
return "'" + str + "'"; | ||
} | ||
function builders_strictMode_umd__req ( path ) { | ||
return 'require(\'' + path + '\')'; | ||
} | ||
function builders_strictMode_umd__globalify ( name ) { | ||
return 'global.' + name; | ||
} | ||
builders_strictMode_umd__introTemplate = template__default( ("(function (global, factory) {\ | ||
builders_strictMode_umd__introTemplate = template( ("(function (global, factory) {\ | ||
\n\ | ||
@@ -2211,15 +2346,15 @@ \n 'use strict';\ | ||
var builders_strictMode__default = { | ||
amd: builders_strictMode_amd__default, | ||
cjs: builders_strictMode_cjs__default, | ||
umd: builders_strictMode_umd__default | ||
var builders_strictMode = { | ||
amd: builders_strictMode_amd__amd, | ||
cjs: builders_strictMode_cjs__cjs, | ||
umd: builders_strictMode_umd__umd | ||
}; | ||
// TODO rewrite with named imports/exports | ||
var bundleBuilders__default = { | ||
defaultsMode: builders_defaultsMode__default, | ||
strictMode: builders_strictMode__default | ||
var bundleBuilders = { | ||
defaultsMode: builders_defaultsMode, | ||
strictMode: builders_strictMode | ||
}; | ||
function hasNamedImports__hasNamedImports ( mod ) { | ||
function hasNamedImports ( mod ) { | ||
var i, x; | ||
@@ -2244,5 +2379,4 @@ | ||
} | ||
var hasNamedImports__default = hasNamedImports__hasNamedImports; | ||
function hasNamedExports__hasNamedExports ( mod ) { | ||
function hasNamedExports ( mod ) { | ||
var i; | ||
@@ -2257,8 +2391,7 @@ | ||
} | ||
var hasNamedExports__default = hasNamedExports__hasNamedExports; | ||
var esperanto__deprecateMessage = 'options.defaultOnly has been deprecated, and is now standard behaviour. To use named imports/exports, pass `strict: true`.', | ||
esperanto__alreadyWarned = false; | ||
var deprecateMessage = 'options.defaultOnly has been deprecated, and is now standard behaviour. To use named imports/exports, pass `strict: true`.', | ||
alreadyWarned = false; | ||
function esperanto__transpileMethod ( format ) { | ||
function transpileMethod ( format ) { | ||
return function ( source ) {var options = arguments[1];if(options === void 0)options = {}; | ||
@@ -2269,9 +2402,9 @@ var mod, | ||
mod = getStandaloneModule__default({ source: source, getModuleName: options.getModuleName, strict: options.strict }); | ||
mod = getStandaloneModule({ source: source, getModuleName: options.getModuleName, strict: options.strict }); | ||
body = mod.body.clone(); | ||
if ( 'defaultOnly' in options && !esperanto__alreadyWarned ) { | ||
if ( 'defaultOnly' in options && !alreadyWarned ) { | ||
// TODO link to a wiki page explaining this, or something | ||
console.log( esperanto__deprecateMessage ); | ||
esperanto__alreadyWarned = true; | ||
console.log( deprecateMessage ); | ||
alreadyWarned = true; | ||
} | ||
@@ -2281,9 +2414,9 @@ | ||
// ensure there are no named imports/exports. TODO link to a wiki page... | ||
if ( hasNamedImports__default( mod ) || hasNamedExports__default( mod ) ) { | ||
if ( hasNamedImports( mod ) || hasNamedExports( mod ) ) { | ||
throw new Error( 'You must be in strict mode (pass `strict: true`) to use named imports or exports' ); | ||
} | ||
builder = moduleBuilders__default.defaultsMode[ format ]; | ||
builder = moduleBuilders.defaultsMode[ format ]; | ||
} else { | ||
builder = moduleBuilders__default.strictMode[ format ]; | ||
builder = moduleBuilders.strictMode[ format ]; | ||
} | ||
@@ -2295,9 +2428,9 @@ | ||
var esperanto__default = { | ||
toAmd: esperanto__transpileMethod( 'amd' ), | ||
toCjs: esperanto__transpileMethod( 'cjs' ), | ||
toUmd: esperanto__transpileMethod( 'umd' ), | ||
var esperanto = { | ||
toAmd: transpileMethod( 'amd' ), | ||
toCjs: transpileMethod( 'cjs' ), | ||
toUmd: transpileMethod( 'umd' ), | ||
bundle: function ( options ) { | ||
return getBundle__default( options ).then( function ( bundle ) { | ||
return getBundle( options ).then( function ( bundle ) { | ||
return { | ||
@@ -2314,6 +2447,6 @@ toAmd: function(options ) {return transpile( 'amd', options )}, | ||
if ( 'defaultOnly' in options && !esperanto__alreadyWarned ) { | ||
if ( 'defaultOnly' in options && !alreadyWarned ) { | ||
// TODO link to a wiki page explaining this, or something | ||
console.log( esperanto__deprecateMessage ); | ||
esperanto__alreadyWarned = true; | ||
console.log( deprecateMessage ); | ||
alreadyWarned = true; | ||
} | ||
@@ -2323,9 +2456,17 @@ | ||
// ensure there are no named imports/exports | ||
if ( hasNamedExports__default( bundle.entryModule ) ) { | ||
if ( hasNamedExports( bundle.entryModule ) ) { | ||
throw new Error( 'Entry module can only have named exports in strict mode (pass `strict: true`)' ); | ||
} | ||
builder = bundleBuilders__default.defaultsMode[ format ]; | ||
bundle.modules.forEach( function(mod ) { | ||
mod.imports.forEach( function(x ) { | ||
if ( bundle.externalModuleLookup[ x.id ] && !x.default ) { | ||
throw new Error( 'You can only have named external imports in strict mode (pass `strict: true`)' ); | ||
} | ||
}); | ||
}); | ||
builder = bundleBuilders.defaultsMode[ format ]; | ||
} else { | ||
builder = bundleBuilders__default.strictMode[ format ]; | ||
builder = bundleBuilders.strictMode[ format ]; | ||
} | ||
@@ -2339,5 +2480,5 @@ | ||
module.exports = esperanto__default; | ||
module.exports = esperanto; | ||
}).call(global); | ||
//# sourceMappingURL=./esperanto.js.map |
{ | ||
"name": "esperanto", | ||
"description": "An easier way to convert ES6 modules to AMD and CommonJS", | ||
"version": "0.4.10", | ||
"version": "0.5.0", | ||
"author": "Rich Harris", | ||
@@ -11,3 +11,3 @@ "repository": "https://github.com/rich-harris/esperanto", | ||
"estraverse": "^1.9.0", | ||
"magic-string": "^0.2.4", | ||
"magic-string": "^0.2.5", | ||
"minimist": "^1.1.0", | ||
@@ -14,0 +14,0 @@ "sander": "^0.2.1" |
69199
2017
Updatedmagic-string@^0.2.5