mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
9 years ago
9 changed files with 173 additions and 26 deletions
@ -0,0 +1,66 @@ |
|||||
|
import { encode, decode } from 'sourcemap-codec'; |
||||
|
|
||||
|
function traceSegment ( loc, mappings ) { |
||||
|
const line = loc[0]; |
||||
|
const column = loc[1]; |
||||
|
|
||||
|
const segments = mappings[ line ]; |
||||
|
|
||||
|
for ( let i = 0; i < segments.length; i += 1 ) { |
||||
|
const segment = segments[i]; |
||||
|
|
||||
|
if ( segment[0] > column ) return null; |
||||
|
|
||||
|
if ( segment[0] === column ) { |
||||
|
if ( segment[1] !== 0 ) { |
||||
|
throw new Error( 'Bad sourcemap' ); |
||||
|
} |
||||
|
|
||||
|
return [ segment[2], segment[3] ]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
export default function collapseSourcemaps ( map, modules ) { |
||||
|
const chains = modules.map( module => { |
||||
|
return module.sourceMapChain.map( map => decode( map.mappings ) ); |
||||
|
}); |
||||
|
|
||||
|
const decodedMappings = decode( map.mappings ); |
||||
|
|
||||
|
const tracedMappings = decodedMappings.map( line => { |
||||
|
let tracedLine = []; |
||||
|
|
||||
|
line.forEach( segment => { |
||||
|
const sourceIndex = segment[1]; |
||||
|
const sourceCodeLine = segment[2]; |
||||
|
const sourceCodeColumn = segment[3]; |
||||
|
|
||||
|
const chain = chains[ sourceIndex ]; |
||||
|
|
||||
|
let i = chain.length; |
||||
|
let traced = [ sourceCodeLine, sourceCodeColumn ]; |
||||
|
|
||||
|
while ( i-- && traced ) { |
||||
|
traced = traceSegment( traced, chain[i] ); |
||||
|
} |
||||
|
|
||||
|
if ( traced ) { |
||||
|
tracedLine.push([ |
||||
|
segment[0], |
||||
|
segment[1], |
||||
|
traced[0], |
||||
|
traced[1] |
||||
|
// TODO name?
|
||||
|
]); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
return tracedLine; |
||||
|
}); |
||||
|
|
||||
|
map.mappings = encode( tracedMappings ); |
||||
|
return map; |
||||
|
} |
@ -1,10 +1,5 @@ |
|||||
import { readFileSync } from './fs'; |
import { readFileSync } from './fs'; |
||||
|
|
||||
export function defaultLoader ( id, options ) { |
export function defaultLoader ( id ) { |
||||
// TODO support plugins e.g. !css and !json?
|
return readFileSync( id, 'utf-8' ); |
||||
const source = readFileSync( id, 'utf-8' ); |
|
||||
|
|
||||
return options.transform.reduce( ( source, transformer ) => { |
|
||||
return transformer( source, id ); |
|
||||
}, source ); |
|
||||
} |
} |
||||
|
@ -0,0 +1,31 @@ |
|||||
|
export default function transform ( source, id, transformers ) { |
||||
|
let sourceMapChain = []; |
||||
|
|
||||
|
if ( typeof source === 'string' ) { |
||||
|
source = { |
||||
|
code: source, |
||||
|
ast: null |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
let ast = source.ast; |
||||
|
|
||||
|
let code = transformers.reduce( ( previous, transformer ) => { |
||||
|
let result = transformer( previous, id ); |
||||
|
|
||||
|
if ( typeof result === 'string' ) { |
||||
|
result = { |
||||
|
code: result, |
||||
|
ast: null, |
||||
|
map: null |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
sourceMapChain.push( result.map ); |
||||
|
ast = result.ast; |
||||
|
|
||||
|
return result.code; |
||||
|
}, source.code ); |
||||
|
|
||||
|
return { code, ast, sourceMapChain }; |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
var babel = require( 'babel-core' ); |
||||
|
var MagicString = require( 'magic-string' ); |
||||
|
var assert = require( 'assert' ); |
||||
|
var getLocation = require( '../../utils/getLocation' ); |
||||
|
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer; |
||||
|
|
||||
|
module.exports = { |
||||
|
solo: true, |
||||
|
description: 'preserves sourcemap chains when transforming', |
||||
|
options: { |
||||
|
transform: [ |
||||
|
function ( source, id ) { |
||||
|
return babel.transform( source, { |
||||
|
blacklist: [ 'es6.modules' ], |
||||
|
sourceMap: true |
||||
|
}); |
||||
|
}, |
||||
|
|
||||
|
function ( source, id ) { |
||||
|
var s = new MagicString( source ); |
||||
|
s.prepend( '// this is a comment\n' ); |
||||
|
|
||||
|
return { |
||||
|
code: s.toString(), |
||||
|
map: s.generateMap({ hires: true }) |
||||
|
}; |
||||
|
} |
||||
|
] |
||||
|
}, |
||||
|
test: function ( code, map ) { |
||||
|
var smc = new SourceMapConsumer( map ); |
||||
|
|
||||
|
var generatedLoc = getLocation( code, code.indexOf( 42 ) ); |
||||
|
var originalLoc = smc.originalPositionFor( generatedLoc ); |
||||
|
|
||||
|
assert.ok( /foo/.test( originalLoc.source ) ); |
||||
|
assert.equal( originalLoc.line, 1 ); |
||||
|
assert.equal( originalLoc.column, 25 ); |
||||
|
|
||||
|
generatedLoc = getLocation( code, code.indexOf( 'log' ) ); |
||||
|
originalLoc = smc.originalPositionFor( generatedLoc ); |
||||
|
|
||||
|
assert.ok( /main/.test( originalLoc.source ) ); |
||||
|
assert.equal( originalLoc.line, 3 ); |
||||
|
assert.equal( originalLoc.column, 8 ); |
||||
|
} |
||||
|
}; |
@ -0,0 +1 @@ |
|||||
|
export const foo = () => 42; |
@ -0,0 +1,3 @@ |
|||||
|
import { foo } from './foo'; |
||||
|
|
||||
|
console.log( `the answer is ${foo()}` ); |
Loading…
Reference in new issue