mirror of https://github.com/lukechilds/rollup.git
Rich Harris
9 years ago
committed by
GitHub
9 changed files with 147 additions and 30 deletions
@ -0,0 +1,63 @@ |
|||
var babel = require( 'babel-core' ); |
|||
var fs = require( 'fs' ); |
|||
var assert = require( 'assert' ); |
|||
var getLocation = require( '../../utils/getLocation' ); |
|||
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer; |
|||
|
|||
module.exports = { |
|||
description: 'preserves sourcemap chains when transforming', |
|||
options: { |
|||
plugins: [ |
|||
{ |
|||
load: function ( id ) { |
|||
if ( /foo.js$/.test( id ) ) { |
|||
id = id.replace( /foo.js$/, 'bar.js' ); |
|||
} else if ( /bar.js$/.test( id ) ) { |
|||
id = id.replace( /bar.js$/, 'foo.js' ); |
|||
} |
|||
|
|||
var out = babel.transformFileSync( id, { |
|||
blacklist: [ 'es6.modules' ], |
|||
sourceMap: true, |
|||
comments: false // misalign the columns
|
|||
}); |
|||
|
|||
if ( /main.js$/.test( id ) ) { |
|||
delete out.map.sources; |
|||
} else { |
|||
const slash = out.map.sources[0].lastIndexOf( '/' ) + 1; |
|||
out.map.sources = out.map.sources.map( source => '../' + source.slice( slash ) ); |
|||
out.map.sourceRoot = 'fake'; |
|||
} |
|||
|
|||
return { code: out.code, map: out.map }; |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
test: function ( code, map ) { |
|||
var smc = new SourceMapConsumer( map ); |
|||
|
|||
var generatedLoc = getLocation( code, code.indexOf( '22' ) ); |
|||
var originalLoc = smc.originalPositionFor( generatedLoc ); |
|||
|
|||
assert.equal( originalLoc.source, '../foo.js' ); |
|||
assert.equal( originalLoc.line, 1 ); |
|||
assert.equal( originalLoc.column, 32 ); |
|||
|
|||
var generatedLoc = getLocation( code, code.indexOf( '20' ) ); |
|||
var originalLoc = smc.originalPositionFor( generatedLoc ); |
|||
|
|||
assert.equal( originalLoc.source, '../bar.js' ); |
|||
assert.equal( originalLoc.line, 1 ); |
|||
assert.equal( originalLoc.column, 37 ); |
|||
|
|||
generatedLoc = getLocation( code, code.indexOf( 'log' ) ); |
|||
originalLoc = smc.originalPositionFor( generatedLoc ); |
|||
|
|||
assert.equal( originalLoc.source, '../main.js' ); |
|||
assert.ok( /columns/.test( smc.sourceContentFor( '../main.js' ) ) ); |
|||
assert.equal( originalLoc.line, 4 ); |
|||
assert.equal( originalLoc.column, 19 ); |
|||
} |
|||
}; |
@ -0,0 +1 @@ |
|||
/*misalign*/export const foo = () => 20; |
@ -0,0 +1 @@ |
|||
/*the*/export const bar = () => 22; |
@ -0,0 +1,4 @@ |
|||
import { foo } from './foo'; |
|||
import { bar } from './bar'; |
|||
|
|||
/*columns*/console.log( `the answer is ${foo() + bar()}` ); |
Loading…
Reference in new issue