Browse Source

handle names in sourcemaps

gh-438-b
Rich-Harris 9 years ago
parent
commit
5e4b54c92b
  1. 47
      src/utils/collapseSourcemaps.js
  2. 58
      test/sourcemaps/names-transformed/_config.js
  3. 4
      test/sourcemaps/names-transformed/a.js
  4. 4
      test/sourcemaps/names-transformed/b.js
  5. 5
      test/sourcemaps/names-transformed/main.js
  6. 1
      test/sourcemaps/transform-bundle/_config.js

47
src/utils/collapseSourcemaps.js

@ -10,33 +10,45 @@ function Source ( map, sources ) {
Source.prototype = { // TODO bring into line with others post-https://github.com/rollup/rollup/pull/386 Source.prototype = { // TODO bring into line with others post-https://github.com/rollup/rollup/pull/386
traceMappings () { traceMappings () {
return this.mappings.map( line => { let names = [];
const mappings = this.mappings.map( line => {
let tracedLine = []; let tracedLine = [];
line.forEach( segment => { line.forEach( segment => {
const source = this.sources[ segment[1] ]; const source = this.sources[ segment[1] ];
const traced = source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] );
const sourceCodeLine = segment[2];
const sourceCodeColumn = segment[3];
const traced = source.traceSegment( sourceCodeLine, sourceCodeColumn );
if ( traced ) { if ( traced ) {
tracedLine.push([ let nameIndex = null;
segment = [
segment[0], segment[0],
traced.index, traced.index,
traced.line, traced.line,
traced.column traced.column
// TODO name? ];
]);
if ( traced.name ) {
nameIndex = names.indexOf( traced.name );
if ( nameIndex === -1 ) {
nameIndex = names.length;
names.push( traced.name );
}
segment[4] = nameIndex;
}
tracedLine.push( segment );
} }
}); });
return tracedLine; return tracedLine;
}); });
return { names, mappings };
}, },
traceSegment ( line, column ) { traceSegment ( line, column, name ) {
const segments = this.mappings[ line ]; const segments = this.mappings[ line ];
if ( !segments ) return null; if ( !segments ) return null;
@ -52,10 +64,15 @@ Source.prototype = { // TODO bring into line with others post-https://github.com
if ( !source ) throw new Error( 'Bad sourcemap' ); if ( !source ) throw new Error( 'Bad sourcemap' );
if ( source.isOriginal ) { if ( source.isOriginal ) {
return { index: source.index, line: segment[2], column: segment[3] }; return {
index: source.index,
line: segment[2],
column: segment[3],
name: this.names[ segment[4] ] || name
};
} }
return source.traceSegment( segment[2], segment[3] ); return source.traceSegment( segment[2], segment[3], name );
} }
} }
@ -80,8 +97,12 @@ export default function collapseSourcemaps ( map, modules, bundleSourcemapChain
source = new Source( map, [ source ] ); source = new Source( map, [ source ] );
}); });
const { names, mappings } = source.traceMappings();
// we re-use the `map` object because it has convenient toString/toURL methods // we re-use the `map` object because it has convenient toString/toURL methods
map.sourcesContent = modules.map( module => module.originalCode ); map.sourcesContent = modules.map( module => module.originalCode );
map.mappings = encode( source.traceMappings() ); map.mappings = encode( mappings );
map.names = names;
return map; return map;
} }

58
test/sourcemaps/names-transformed/_config.js

@ -0,0 +1,58 @@
var assert = require( 'assert' );
var uglify = require( 'uglify-js' );
var MagicString = require( 'magic-string' );
var getLocation = require( '../../utils/getLocation' );
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;
module.exports = {
description: 'names are recovered if transforms are used',
options: {
plugins: [
{
transform: function ( code ) {
var s = new MagicString( code );
var pattern = /mangleMe/g;
var match;
while ( match = pattern.exec( code ) ) {
s.overwrite( match.index, match.index + match[0].length, 'mangleMePlease', true );
}
return {
code: s.toString(),
map: s.generateMap({ hires: true })
};
},
transformBundle: function ( code ) {
return uglify.minify( code, {
fromString: true,
outSourceMap: 'x'
});
}
}
]
},
test: function ( code, map ) {
var smc = new SourceMapConsumer( map );
var generatedLoc = getLocation( code, /\w+=1/.exec( code ).index );
var originalLoc = smc.originalPositionFor( generatedLoc );
assert.deepEqual( originalLoc, {
source: '../a.js',
line: 1,
column: 4,
name: 'mangleMe'
});
generatedLoc = getLocation( code, /\w+=2/.exec( code ).index );
originalLoc = smc.originalPositionFor( generatedLoc );
assert.deepEqual( originalLoc, {
source: '../b.js',
line: 1,
column: 4,
name: 'mangleMe'
});
}
};

4
test/sourcemaps/names-transformed/a.js

@ -0,0 +1,4 @@
var mangleMe = 1;
export default function () {
assert.equal( mangleMe, 1 );
}

4
test/sourcemaps/names-transformed/b.js

@ -0,0 +1,4 @@
var mangleMe = 2;
export default function () {
assert.equal( mangleMe, 2 );
}

5
test/sourcemaps/names-transformed/main.js

@ -0,0 +1,5 @@
import a from './a.js';
import b from './b.js';
a();
b();

1
test/sourcemaps/transform-bundle/_config.js

@ -1,5 +1,4 @@
var uglify = require( 'uglify-js' ); var uglify = require( 'uglify-js' );
var MagicString = require( 'magic-string' );
var assert = require( 'assert' ); var assert = require( 'assert' );
var getLocation = require( '../../utils/getLocation' ); var getLocation = require( '../../utils/getLocation' );
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer; var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;

Loading…
Cancel
Save