Skip to content

Commit

Permalink
Sourcemap sourceRoot (#1141)
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 28b87cf commit 3b1a585
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/SourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,8 @@ class SourceMap {
}
}

stringify(file) {
let generator = new SourceMapGenerator({
file: file
});
stringify(file, sourceRoot) {
let generator = new SourceMapGenerator({file, sourceRoot});

this.eachMapping(mapping => generator.addMapping(mapping));
Object.keys(this.sources).forEach(sourceName =>
Expand Down
7 changes: 6 additions & 1 deletion src/packagers/SourceMapPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class SourceMapPackager extends Packager {

async end() {
let file = path.basename(this.bundle.name);
await this.write(this.sourceMap.stringify(file));
await this.write(
this.sourceMap.stringify(
file,
path.relative(this.options.outDir, this.options.rootDir)
)
);
await super.end();
}
}
Expand Down
25 changes: 21 additions & 4 deletions test/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const assert = require('assert');
const fs = require('fs');
const path = require('path');
const mapValidator = require('sourcemap-validator');
const {bundle, run, assertBundleTree} = require('./utils');
const {bundler, bundle, run, assertBundleTree} = require('./utils');

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

assertBundleTree(b, {
assertBundleTree(bu, {
name: 'index.js',
assets: ['index.js'],
childBundles: [
Expand All @@ -26,8 +27,24 @@ describe('sourcemaps', function() {
.readFileSync(path.join(__dirname, '/dist/index.map'))
.toString();
mapValidator(raw, map);
let mapObject = JSON.parse(map);
assert(
mapObject.sourceRoot ===
path.relative(b.options.outDir, b.options.rootDir),
'sourceRoot should be the root of the source files, relative to the output directory.'
);
assert(
fs.existsSync(
path.resolve(
b.options.outDir,
mapObject.sourceRoot,
mapObject.sources[0]
)
),
'combining sourceRoot and sources object should resolve to the original file'
);

let output = run(b);
let output = run(bu);
assert.equal(typeof output, 'function');
assert.equal(output(), 'hello world');
});
Expand Down

0 comments on commit 3b1a585

Please sign in to comment.