Skip to content

Commit 695cfd0

Browse files
committed
more sourcemap stuff
1 parent 5e8a25f commit 695cfd0

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ dist
44
.nyc_output
55
coverage
66
coverage.lcov
7+
test/sourcemaps/*/output.js
8+
test/sourcemaps/*/output.js.map

compiler/generate/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,16 @@ export default function generate ( parsed, source, options ) {
514514

515515
addString( getIntro( format, options, imports ) );
516516

517+
// a filename is necessary for sourcemap generation
518+
const filename = options.filename || 'SvelteComponent.html';
519+
517520
parts.forEach( str => {
518521
const match = pattern.exec( str );
519522

520523
addString( str.replace( pattern, '' ) );
521524

522525
compiled.addSource({
523-
filename: options.filename,
526+
filename,
524527
content: generator.code.snip( +match[1], +match[2] )
525528
});
526529
});
@@ -531,6 +534,6 @@ export default function generate ( parsed, source, options ) {
531534

532535
return {
533536
code: compiled.toString(),
534-
map: compiled.generateMap()
537+
map: compiled.generateMap({ includeContent: true })
535538
};
536539
}

test/sourcemaps/basic/test.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
export function test ( assert, code, map, smc, locator ) {
2-
console.log( `code`, code )
3-
console.log( `map`, map )
1+
export function test ({ assert, smc, locateInSource, locateInGenerated }) {
2+
const expected = locateInSource( 'foo.bar.baz' );
3+
const loc = locateInGenerated( 'foo.bar.baz' );
44

5-
let loc = locator( 'foo.bar.baz' );
6-
console.log( `loc`, loc )
5+
const actual = smc.originalPositionFor({
6+
line: loc.line + 1,
7+
column: loc.column
8+
});
9+
10+
assert.deepEqual( actual, {
11+
source: 'SvelteComponent.html',
12+
name: null,
13+
line: expected.line,
14+
column: expected.column
15+
});
716
}

test/test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,17 @@ describe( 'svelte', () => {
481481
const input = fs.readFileSync( `test/sourcemaps/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
482482
const { code, map } = svelte.compile( input );
483483

484+
fs.writeFileSync( `test/sourcemaps/${dir}/output.js`, `${code}\n//# sourceMappingURL=output.js.map` );
485+
fs.writeFileSync( `test/sourcemaps/${dir}/output.js.map`, JSON.stringify( map, null, ' ' ) );
486+
484487
const { test } = require( `./sourcemaps/${dir}/test.js` );
485488

486489
const smc = new SourceMapConsumer( map );
487-
const locator = getLocator( code );
488490

489-
test( assert, code, map, smc, locator );
491+
const locateInSource = getLocator( input );
492+
const locateInGenerated = getLocator( code );
493+
494+
test({ assert, code, map, smc, locateInSource, locateInGenerated });
490495
});
491496
});
492497
});

0 commit comments

Comments
 (0)