Skip to content

Commit

Permalink
Purify sourcemaps (Prevent babel from giving invalid mappings) (#639)
Browse files Browse the repository at this point in the history
* purify sourcemaps

* even more strict purifying

* Cleanup
  • Loading branch information
Jasper De Moor authored and devongovett committed Jan 26, 2018
1 parent e78baca commit 0329180
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/SourceMap.js
Expand Up @@ -3,16 +3,37 @@ const lineCounter = require('./utils/lineCounter');

class SourceMap {
constructor(mappings, sources) {
this.mappings = mappings || [];
this.mappings = this.purifyMappings(mappings);
this.sources = sources || {};
this.lineCount = null;
}

purifyMappings(mappings) {
if (Array.isArray(mappings)) {
return mappings.filter(mapping => {
return (
mapping &&
mapping.source &&
mapping.original &&
typeof mapping.original.line === 'number' &&
mapping.original.line > 0 &&
typeof mapping.original.column === 'number' &&
mapping.generated &&
typeof mapping.generated.line === 'number' &&
mapping.generated.line > 0 &&
typeof mapping.generated.column === 'number'
);
});
}

return [];
}

async getConsumer(map) {
if (map instanceof SourceMapConsumer) {
return map;
}

map = typeof map === 'string' ? JSON.parse(map) : map;
return await new SourceMapConsumer(map);
}

Expand Down
5 changes: 5 additions & 0 deletions test/integration/sourcemap-typescript-nested/index.ts
@@ -0,0 +1,5 @@
import { local } from './local';

export function env() {
return local;
}
1 change: 1 addition & 0 deletions test/integration/sourcemap-typescript-nested/local.ts
@@ -0,0 +1 @@
exports.local = process.env.NODE_ENV;
29 changes: 29 additions & 0 deletions test/sourcemaps.js
Expand Up @@ -61,6 +61,35 @@ describe('sourcemaps', function() {
assert.equal(output.env(), process.env.NODE_ENV);
});

it('should create a valid sourcemap as a child of a nested TS bundle', async function() {
let b = await bundle(
__dirname + '/integration/sourcemap-typescript-nested/index.ts'
);

assertBundleTree(b, {
name: 'index.js',
assets: ['index.ts', 'local.ts'],
childBundles: [
{
name: 'index.map',
type: 'map'
}
]
});

let raw = fs
.readFileSync(path.join(__dirname, '/dist/index.js'))
.toString();
let map = fs
.readFileSync(path.join(__dirname, '/dist/index.map'))
.toString();
mapValidator(raw, map);

let output = run(b);
assert.equal(typeof output.env, 'function');
assert.equal(output.env(), process.env.NODE_ENV);
});

it('should create a valid sourcemap for a js file with requires', async function() {
let b = await bundle(__dirname + '/integration/sourcemap-nested/index.js');

Expand Down

0 comments on commit 0329180

Please sign in to comment.