Skip to content

Commit

Permalink
Merge pull request #295 from sveltejs/sourcemaps
Browse files Browse the repository at this point in the history
add outputFilename option for sourcemap generation
  • Loading branch information
Rich-Harris committed Feb 24, 2017
2 parents f3afc4a + 42497fd commit 610dff5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/generators/Generator.js
Expand Up @@ -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 );
Expand All @@ -168,7 +177,7 @@ export default class Generator {

return {
code: compiled.toString(),
map: compiled.generateMap({ includeContent: true })
map: compiled.generateMap({ includeContent: true, file: options.outputFilename })
};
}

Expand Down
17 changes: 13 additions & 4 deletions 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';
Expand All @@ -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` );

Expand Down
4 changes: 2 additions & 2 deletions test/sourcemaps/basic/test.js
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/sourcemaps/script/test.js
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/sourcemaps/static-no-script/input.html
@@ -0,0 +1 @@
<p>no moving parts</p>
9 changes: 9 additions & 0 deletions 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' )
]);
}

0 comments on commit 610dff5

Please sign in to comment.