Skip to content

Commit

Permalink
Fix sourcemap reference in JS output (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper De Moor authored and devongovett committed Apr 9, 2018
1 parent 9f3f30a commit 28b87cf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,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();
}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/sourcemap-reference/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"some": "data",
"more": {
"data": "value"
}
}
8 changes: 8 additions & 0 deletions test/integration/sourcemap-reference/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<script src="./index.js"></script>
</head>
<body>
Hello!
</body>
</html>
1 change: 1 addition & 0 deletions test/integration/sourcemap-reference/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './data.json';
39 changes: 39 additions & 0 deletions test/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 28b87cf

Please sign in to comment.