diff --git a/src/packagers/JSPackager.js b/src/packagers/JSPackager.js index a3ce3fab491..6d8bafae2dc 100644 --- a/src/packagers/JSPackager.js +++ b/src/packagers/JSPackager.js @@ -204,13 +204,16 @@ class JSPackager extends Packager { await this.write('},{},' + JSON.stringify(entry) + ')'); if (this.options.sourceMaps) { - // Add source map url - await this.write( - `\n//# sourceMappingURL=${urlJoin( - this.options.publicURL, - path.basename(this.bundle.name, '.js') + '.map' - )}` - ); + // Add source map url if a map bundle exists + let mapBundle = this.bundle.siblingBundlesMap.get('map'); + if (mapBundle) { + await this.write( + `\n//# sourceMappingURL=${urlJoin( + this.options.publicURL, + path.basename(mapBundle.name) + )}` + ); + } } await this.dest.end(); } diff --git a/test/integration/sourcemap-reference/data.json b/test/integration/sourcemap-reference/data.json new file mode 100644 index 00000000000..f18b8ec4913 --- /dev/null +++ b/test/integration/sourcemap-reference/data.json @@ -0,0 +1,6 @@ +{ + "some": "data", + "more": { + "data": "value" + } +} \ No newline at end of file diff --git a/test/integration/sourcemap-reference/index.html b/test/integration/sourcemap-reference/index.html new file mode 100644 index 00000000000..f1058e56ee0 --- /dev/null +++ b/test/integration/sourcemap-reference/index.html @@ -0,0 +1,8 @@ + + + + + + Hello! + + \ No newline at end of file diff --git a/test/integration/sourcemap-reference/index.js b/test/integration/sourcemap-reference/index.js new file mode 100644 index 00000000000..298eba3f0a8 --- /dev/null +++ b/test/integration/sourcemap-reference/index.js @@ -0,0 +1 @@ +import './data.json'; \ No newline at end of file diff --git a/test/sourcemaps.js b/test/sourcemaps.js index 0d184ab0907..cbd41a312ed 100644 --- a/test/sourcemaps.js +++ b/test/sourcemaps.js @@ -148,4 +148,43 @@ describe('sourcemaps', function() { assert.equal(typeof output, 'function'); assert.equal(output(), 14); }); + + it('should create a valid sourcemap reference for a child bundle', async function() { + let b = await bundle( + __dirname + '/integration/sourcemap-reference/index.html' + ); + + assertBundleTree(b, { + name: 'index.html', + assets: ['index.html'], + childBundles: [ + { + type: 'js', + assets: ['index.js', 'data.json'], + childBundles: [ + { + type: 'map' + } + ] + } + ] + }); + + let jsOutput = fs + .readFileSync(Array.from(b.childBundles)[0].name) + .toString(); + + let sourcemapReference = path.join( + __dirname, + '/dist/', + jsOutput.substring(jsOutput.lastIndexOf('//# sourceMappingURL') + 22) + ); + assert( + fs.existsSync(path.join(sourcemapReference)), + 'referenced sourcemap should exist' + ); + + let map = fs.readFileSync(path.join(sourcemapReference)).toString(); + mapValidator(jsOutput, map); + }); });