From c1ba7638ce0c616943c03ea66f79907c06ffa1be Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 24 Feb 2017 15:12:17 -0500 Subject: [PATCH 1/2] add outputFilename option for sourcemap generation --- src/generators/Generator.js | 2 +- test/sourcemaps.js | 17 +++++++++++++---- test/sourcemaps/basic/test.js | 4 ++-- test/sourcemaps/script/test.js | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/generators/Generator.js b/src/generators/Generator.js index 8568a515e7e8..beca3ec09f9d 100644 --- a/src/generators/Generator.js +++ b/src/generators/Generator.js @@ -168,7 +168,7 @@ export default class Generator { return { code: compiled.toString(), - map: compiled.generateMap({ includeContent: true }) + map: compiled.generateMap({ includeContent: true, file: options.outputFilename }) }; } diff --git a/test/sourcemaps.js b/test/sourcemaps.js index bafaf11a8ab6..80d5edb315ef 100644 --- a/test/sourcemaps.js +++ b/test/sourcemaps.js @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as path from 'path'; import assert from 'assert'; import { svelte, exists } from './helpers.js'; import { SourceMapConsumer } from 'source-map'; @@ -15,11 +16,19 @@ describe( 'sourcemaps', () => { } ( solo ? it.only : it )( dir, () => { - const input = fs.readFileSync( `test/sourcemaps/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' ); - const { code, map } = svelte.compile( input ); + const filename = path.resolve( `test/sourcemaps/${dir}/input.html` ); + const outputFilename = path.resolve( `test/sourcemaps/${dir}/output.js` ); - 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 input = fs.readFileSync( filename, 'utf-8' ).replace( /\s+$/, '' ); + const { code, map } = svelte.compile( input, { + filename, + outputFilename + }); + + fs.writeFileSync( outputFilename, `${code}\n//# sourceMappingURL=output.js.map` ); + fs.writeFileSync( `${outputFilename}.map`, JSON.stringify( map, null, ' ' ) ); + + assert.deepEqual( map.sources, [ 'input.html' ]); const { test } = require( `./sourcemaps/${dir}/test.js` ); diff --git a/test/sourcemaps/basic/test.js b/test/sourcemaps/basic/test.js index b169b04f7a08..a8803c00631d 100644 --- a/test/sourcemaps/basic/test.js +++ b/test/sourcemaps/basic/test.js @@ -12,7 +12,7 @@ export function test ({ assert, smc, locateInSource, locateInGenerated }) { }); assert.deepEqual( actual, { - source: 'SvelteComponent.html', + source: 'input.html', name: null, line: expected.line + 1, column: expected.column @@ -26,7 +26,7 @@ export function test ({ assert, smc, locateInSource, locateInGenerated }) { }); assert.deepEqual( actual, { - source: 'SvelteComponent.html', + source: 'input.html', name: null, line: expected.line + 1, column: expected.column diff --git a/test/sourcemaps/script/test.js b/test/sourcemaps/script/test.js index 6b14b91a9d5d..80468b23bf92 100644 --- a/test/sourcemaps/script/test.js +++ b/test/sourcemaps/script/test.js @@ -8,7 +8,7 @@ export function test ({ assert, smc, locateInSource, locateInGenerated }) { }); assert.deepEqual( actual, { - source: 'SvelteComponent.html', + source: 'input.html', name: null, line: expected.line + 1, column: expected.column From 42497fd1aad2f198bb94ec062d215f652896b0a6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 24 Feb 2017 15:37:53 -0500 Subject: [PATCH 2/2] populate map.sources and map.sourcesContent even if none of the original code is used --- src/generators/Generator.js | 9 +++++++++ test/sourcemaps/static-no-script/input.html | 1 + test/sourcemaps/static-no-script/test.js | 9 +++++++++ 3 files changed, 19 insertions(+) create mode 100644 test/sourcemaps/static-no-script/input.html create mode 100644 test/sourcemaps/static-no-script/test.js diff --git a/src/generators/Generator.js b/src/generators/Generator.js index beca3ec09f9d..995c39dd2355 100644 --- a/src/generators/Generator.js +++ b/src/generators/Generator.js @@ -149,6 +149,15 @@ export default class Generator { const { filename } = options; + // special case — the source file doesn't actually get used anywhere. we need + // to add an empty file to populate map.sources and map.sourcesContent + if ( !parts.length ) { + compiled.addSource({ + filename, + content: new MagicString( this.source ).remove( 0, this.source.length ) + }); + } + parts.forEach( str => { const chunk = str.replace( pattern, '' ); if ( chunk ) addString( chunk ); diff --git a/test/sourcemaps/static-no-script/input.html b/test/sourcemaps/static-no-script/input.html new file mode 100644 index 000000000000..1e2d59797188 --- /dev/null +++ b/test/sourcemaps/static-no-script/input.html @@ -0,0 +1 @@ +

no moving parts

\ No newline at end of file diff --git a/test/sourcemaps/static-no-script/test.js b/test/sourcemaps/static-no-script/test.js new file mode 100644 index 000000000000..0a989619ac93 --- /dev/null +++ b/test/sourcemaps/static-no-script/test.js @@ -0,0 +1,9 @@ +const fs = require( 'fs' ); +const path = require( 'path' ); + +export function test ({ assert, map }) { + assert.deepEqual( map.sources, [ 'input.html' ]); + assert.deepEqual( map.sourcesContent, [ + fs.readFileSync( path.join( __dirname, 'input.html' ), 'utf-8' ) + ]); +}