more sourcemap stuff

pull/97/head
Rich Harris 8 years ago
parent 5e8a25fe65
commit 695cfd01c9

2
.gitignore vendored

@ -4,3 +4,5 @@ dist
.nyc_output .nyc_output
coverage coverage
coverage.lcov coverage.lcov
test/sourcemaps/*/output.js
test/sourcemaps/*/output.js.map

@ -514,13 +514,16 @@ export default function generate ( parsed, source, options ) {
addString( getIntro( format, options, imports ) ); addString( getIntro( format, options, imports ) );
// a filename is necessary for sourcemap generation
const filename = options.filename || 'SvelteComponent.html';
parts.forEach( str => { parts.forEach( str => {
const match = pattern.exec( str ); const match = pattern.exec( str );
addString( str.replace( pattern, '' ) ); addString( str.replace( pattern, '' ) );
compiled.addSource({ compiled.addSource({
filename: options.filename, filename,
content: generator.code.snip( +match[1], +match[2] ) content: generator.code.snip( +match[1], +match[2] )
}); });
}); });
@ -531,6 +534,6 @@ export default function generate ( parsed, source, options ) {
return { return {
code: compiled.toString(), code: compiled.toString(),
map: compiled.generateMap() map: compiled.generateMap({ includeContent: true })
}; };
} }

@ -1,7 +1,16 @@
export function test ( assert, code, map, smc, locator ) { export function test ({ assert, smc, locateInSource, locateInGenerated }) {
console.log( `code`, code ) const expected = locateInSource( 'foo.bar.baz' );
console.log( `map`, map ) const loc = locateInGenerated( 'foo.bar.baz' );
let loc = locator( 'foo.bar.baz' ); const actual = smc.originalPositionFor({
console.log( `loc`, loc ) line: loc.line + 1,
column: loc.column
});
assert.deepEqual( actual, {
source: 'SvelteComponent.html',
name: null,
line: expected.line,
column: expected.column
});
} }

@ -481,12 +481,17 @@ describe( 'svelte', () => {
const input = fs.readFileSync( `test/sourcemaps/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' ); const input = fs.readFileSync( `test/sourcemaps/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
const { code, map } = svelte.compile( input ); const { code, map } = svelte.compile( input );
fs.writeFileSync( `test/sourcemaps/${dir}/output.js`, `${code}\n//# sourceMappingURL=output.js.map` );
fs.writeFileSync( `test/sourcemaps/${dir}/output.js.map`, JSON.stringify( map, null, ' ' ) );
const { test } = require( `./sourcemaps/${dir}/test.js` ); const { test } = require( `./sourcemaps/${dir}/test.js` );
const smc = new SourceMapConsumer( map ); const smc = new SourceMapConsumer( map );
const locator = getLocator( code );
test( assert, code, map, smc, locator ); const locateInSource = getLocator( input );
const locateInGenerated = getLocator( code );
test({ assert, code, map, smc, locateInSource, locateInGenerated });
}); });
}); });
}); });

Loading…
Cancel
Save