diff --git a/src/Bundle/index.js b/src/Bundle/index.js index bb31a83..1e1d45e 100644 --- a/src/Bundle/index.js +++ b/src/Bundle/index.js @@ -82,6 +82,14 @@ class Bundle { generateMap ( options ) { let offsets = {}; + let names = []; + this.sources.forEach( source => { + Object.keys( source.content.nameLocations ).forEach( location => { + const name = source.content.nameLocations[ location ]; + if ( !~names.indexOf( name ) ) names.push( name ); + }); + }); + const encoded = ( getSemis( this.intro ) + this.sources.map( ( source, i ) => { @@ -93,7 +101,7 @@ class Bundle { mappings = getSemis( source.content.toString() ); } else { const sourceIndex = this.uniqueSourceIndexByFilename[ source.filename ]; - mappings = source.content.getMappings( options.hires, sourceIndex, offsets, [], {} ); // TODO names and nameLocations + mappings = source.content.getMappings( options.hires, sourceIndex, offsets, names ); } return prefix + mappings; @@ -109,7 +117,7 @@ class Bundle { sourcesContent: this.uniqueSources.map( source => { return options.includeContent ? source.content : null; }), - names: [], + names, mappings: encoded }); } diff --git a/src/MagicString/index.js b/src/MagicString/index.js index 1830448..65fb206 100644 --- a/src/MagicString/index.js +++ b/src/MagicString/index.js @@ -70,7 +70,7 @@ class MagicString { sources: [ options.source ? getRelativePath( options.file || '', options.source ) : null ], sourcesContent: options.includeContent ? [ this.original ] : [ null ], names, - mappings: this.getMappings( options.hires, 0, {}, names, this.nameLocations ) + mappings: this.getMappings( options.hires, 0, {}, names ) }); } @@ -78,8 +78,8 @@ class MagicString { return this.indentStr === null ? '\t' : this.indentStr; } - getMappings ( hires, sourceIndex, offsets, names, nameLocations ) { - return encodeMappings( this.original, this.str, this.mappings, hires, this.sourcemapLocations, sourceIndex, offsets, names, nameLocations ); + getMappings ( hires, sourceIndex, offsets, names ) { + return encodeMappings( this.original, this.str, this.mappings, hires, this.sourcemapLocations, sourceIndex, offsets, names, this.nameLocations ); } indent ( indentStr, options ) { diff --git a/test/index.js b/test/index.js index 5823c36..f5ce7d3 100644 --- a/test/index.js +++ b/test/index.js @@ -1023,6 +1023,33 @@ describe( 'MagicString.Bundle', function () { assert.equal( loc.column, 0 ); assert.equal( loc.source, 'bar.js' ); }); + + it.only( 'should recover original names', function () { + var b = new MagicString.Bundle(); + + var one = new MagicString( 'function one () {}', { filename: 'one.js' }); + var two = new MagicString( 'function two () {}', { filename: 'two.js' }); + + one.overwrite( 9, 12, 'three', true ); + two.overwrite( 9, 12, 'four', true ); + + b.addSource( one ); + b.addSource( two ); + + map = b.generateMap({ + file: 'output.js', + source: 'input.js', + includeContent: true + }); + + smc = new SourceMapConsumer( map ); + + loc = smc.originalPositionFor({ line: 1, column: 9 }); + assert.equal( loc.name, 'one' ); + + loc = smc.originalPositionFor({ line: 2, column: 9 }); + assert.equal( loc.name, 'two' ); + }); }); describe( 'indent', function () {