Skip to content

Commit 3b1a585

Browse files
Jasper De Moordevongovett
authored andcommitted
Sourcemap sourceRoot (#1141)
1 parent 28b87cf commit 3b1a585

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/SourceMap.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,8 @@ class SourceMap {
278278
}
279279
}
280280

281-
stringify(file) {
282-
let generator = new SourceMapGenerator({
283-
file: file
284-
});
281+
stringify(file, sourceRoot) {
282+
let generator = new SourceMapGenerator({file, sourceRoot});
285283

286284
this.eachMapping(mapping => generator.addMapping(mapping));
287285
Object.keys(this.sources).forEach(sourceName =>

src/packagers/SourceMapPackager.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class SourceMapPackager extends Packager {
1616

1717
async end() {
1818
let file = path.basename(this.bundle.name);
19-
await this.write(this.sourceMap.stringify(file));
19+
await this.write(
20+
this.sourceMap.stringify(
21+
file,
22+
path.relative(this.options.outDir, this.options.rootDir)
23+
)
24+
);
2025
await super.end();
2126
}
2227
}

test/sourcemaps.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ const assert = require('assert');
22
const fs = require('fs');
33
const path = require('path');
44
const mapValidator = require('sourcemap-validator');
5-
const {bundle, run, assertBundleTree} = require('./utils');
5+
const {bundler, bundle, run, assertBundleTree} = require('./utils');
66

77
describe('sourcemaps', function() {
88
it('should create a valid sourcemap as a child of a JS bundle', async function() {
9-
let b = await bundle(__dirname + '/integration/sourcemap/index.js');
9+
let b = bundler(__dirname + '/integration/sourcemap/index.js');
10+
let bu = await b.bundle();
1011

11-
assertBundleTree(b, {
12+
assertBundleTree(bu, {
1213
name: 'index.js',
1314
assets: ['index.js'],
1415
childBundles: [
@@ -26,8 +27,24 @@ describe('sourcemaps', function() {
2627
.readFileSync(path.join(__dirname, '/dist/index.map'))
2728
.toString();
2829
mapValidator(raw, map);
30+
let mapObject = JSON.parse(map);
31+
assert(
32+
mapObject.sourceRoot ===
33+
path.relative(b.options.outDir, b.options.rootDir),
34+
'sourceRoot should be the root of the source files, relative to the output directory.'
35+
);
36+
assert(
37+
fs.existsSync(
38+
path.resolve(
39+
b.options.outDir,
40+
mapObject.sourceRoot,
41+
mapObject.sources[0]
42+
)
43+
),
44+
'combining sourceRoot and sources object should resolve to the original file'
45+
);
2946

30-
let output = run(b);
47+
let output = run(bu);
3148
assert.equal(typeof output, 'function');
3249
assert.equal(output(), 'hello world');
3350
});

0 commit comments

Comments
 (0)