Skip to content

Commit

Permalink
extend #16 fix to bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 2, 2015
1 parent 2fffedb commit 634a861
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/Bundle/index.js
Expand Up @@ -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 ) => {
Expand All @@ -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;
Expand All @@ -109,7 +117,7 @@ class Bundle {
sourcesContent: this.uniqueSources.map( source => {
return options.includeContent ? source.content : null;
}),
names: [],
names,
mappings: encoded
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/MagicString/index.js
Expand Up @@ -70,16 +70,16 @@ 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 )
});
}

getIndentString () {
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 ) {
Expand Down
27 changes: 27 additions & 0 deletions test/index.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 634a861

Please sign in to comment.