From 508a046b4d6eb1ee65533bbdf5cc19f773742cbe Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 2 Jan 2016 23:11:23 -0500 Subject: [PATCH] fix sourcemap resolution in cases where some moudles are untransformed (#404) --- src/utils/collapseSourcemaps.js | 34 ++++++++++--------- .../untransformed-modules/_config.js | 25 ++++++++++++++ test/sourcemaps/untransformed-modules/foo.js | 1 + test/sourcemaps/untransformed-modules/main.js | 2 ++ 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 test/sourcemaps/untransformed-modules/_config.js create mode 100644 test/sourcemaps/untransformed-modules/foo.js create mode 100644 test/sourcemaps/untransformed-modules/main.js diff --git a/src/utils/collapseSourcemaps.js b/src/utils/collapseSourcemaps.js index 9399f11..36a46c8 100644 --- a/src/utils/collapseSourcemaps.js +++ b/src/utils/collapseSourcemaps.js @@ -1,6 +1,17 @@ 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' ); this.sources = sources; @@ -8,7 +19,7 @@ function Source ( map, sources ) { 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 () { 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.isOriginal ) { - return { - index: source.index, - line: segment[2], - column: segment[3], - name: this.names[ segment[4] ] || name - }; - } - - return source.traceSegment( segment[2], segment[3], name ); + return source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] || name ); } } @@ -82,19 +84,19 @@ Source.prototype = { // TODO bring into line with others post-https://github.com export default function collapseSourcemaps ( map, modules, bundleSourcemapChain ) { const sources = modules.map( ( module, i ) => { - let source = { isOriginal: true, index: i }; + let source = new Source( i ); module.sourceMapChain.forEach( map => { - source = new Source( map, [ source ]); + source = new Link( map, [ source ]); }); return source; }); - let source = new Source( map, sources ); + let source = new Link( map, sources ); bundleSourcemapChain.forEach( map => { - source = new Source( map, [ source ] ); + source = new Link( map, [ source ] ); }); const { names, mappings } = source.traceMappings(); diff --git a/test/sourcemaps/untransformed-modules/_config.js b/test/sourcemaps/untransformed-modules/_config.js new file mode 100644 index 0000000..b3498e4 --- /dev/null +++ b/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 ); + } +}; diff --git a/test/sourcemaps/untransformed-modules/foo.js b/test/sourcemaps/untransformed-modules/foo.js new file mode 100644 index 0000000..e2e71aa --- /dev/null +++ b/test/sourcemaps/untransformed-modules/foo.js @@ -0,0 +1 @@ +export var foo = 1; diff --git a/test/sourcemaps/untransformed-modules/main.js b/test/sourcemaps/untransformed-modules/main.js new file mode 100644 index 0000000..538cbe4 --- /dev/null +++ b/test/sourcemaps/untransformed-modules/main.js @@ -0,0 +1,2 @@ +import { foo } from './foo.js'; +assert.equal( foo, 2 );