Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sourcemap reference #1148

Merged
merged 9 commits into from
Apr 9, 2018
17 changes: 10 additions & 7 deletions src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
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);
});
});