Browse Source

fix sourcemap resolution in cases where some moudles are untransformed (#404)

gh-438-b
Rich-Harris 9 years ago
parent
commit
508a046b4d
  1. 34
      src/utils/collapseSourcemaps.js
  2. 25
      test/sourcemaps/untransformed-modules/_config.js
  3. 1
      test/sourcemaps/untransformed-modules/foo.js
  4. 2
      test/sourcemaps/untransformed-modules/main.js

34
src/utils/collapseSourcemaps.js

@ -1,6 +1,17 @@
import { encode, decode } from 'sourcemap-codec'; import { encode, decode } from 'sourcemap-codec';
function Source ( map, sources ) { function Source ( index ) {
this.isOriginal = true;
this.index = index;
}
Source.prototype = {
traceSegment ( line, column, name ) {
return { line, column, name, index: this.index };
}
};
function Link ( map, sources ) {
if ( !map ) throw new Error( 'Cannot generate a sourcemap if non-sourcemap-generating transformers are used' ); if ( !map ) throw new Error( 'Cannot generate a sourcemap if non-sourcemap-generating transformers are used' );
this.sources = sources; this.sources = sources;
@ -8,7 +19,7 @@ function Source ( map, sources ) {
this.mappings = decode( map.mappings ); this.mappings = decode( map.mappings );
} }
Source.prototype = { // TODO bring into line with others post-https://github.com/rollup/rollup/pull/386 Link.prototype = { // TODO bring into line with others post-https://github.com/rollup/rollup/pull/386
traceMappings () { traceMappings () {
let names = []; let names = [];
@ -63,16 +74,7 @@ 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 ) { return source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] || name );
return {
index: source.index,
line: segment[2],
column: segment[3],
name: this.names[ segment[4] ] || name
};
}
return source.traceSegment( segment[2], segment[3], name );
} }
} }
@ -82,19 +84,19 @@ Source.prototype = { // TODO bring into line with others post-https://github.com
export default function collapseSourcemaps ( map, modules, bundleSourcemapChain ) { export default function collapseSourcemaps ( map, modules, bundleSourcemapChain ) {
const sources = modules.map( ( module, i ) => { const sources = modules.map( ( module, i ) => {
let source = { isOriginal: true, index: i }; let source = new Source( i );
module.sourceMapChain.forEach( map => { module.sourceMapChain.forEach( map => {
source = new Source( map, [ source ]); source = new Link( map, [ source ]);
}); });
return source; return source;
}); });
let source = new Source( map, sources ); let source = new Link( map, sources );
bundleSourcemapChain.forEach( map => { bundleSourcemapChain.forEach( map => {
source = new Source( map, [ source ] ); source = new Link( map, [ source ] );
}); });
const { names, mappings } = source.traceMappings(); const { names, mappings } = source.traceMappings();

25
test/sourcemaps/untransformed-modules/_config.js

@ -0,0 +1,25 @@
var assert = require( 'assert' );
var MagicString = require( 'magic-string' );
module.exports = {
description: 'allows sourcemap chains with some untransformed modules (#404)',
options: {
plugins: [{
transform: function ( code, id ) {
if ( /untransformed-modules\/foo/.test( id ) ) {
var s = new MagicString( code );
var index = code.indexOf( '1' );
s.overwrite( index, index + 1, '2' );
return {
code: s.toString(),
map: s.generateMap({ hires: true })
};
}
}
}]
},
test: function () {
assert.ok( true );
}
};

1
test/sourcemaps/untransformed-modules/foo.js

@ -0,0 +1 @@
export var foo = 1;

2
test/sourcemaps/untransformed-modules/main.js

@ -0,0 +1,2 @@
import { foo } from './foo.js';
assert.equal( foo, 2 );
Loading…
Cancel
Save