Skip to content

Commit 0329180

Browse files
Jasper De Moordevongovett
authored andcommitted
Purify sourcemaps (Prevent babel from giving invalid mappings) (#639)
* purify sourcemaps * even more strict purifying * Cleanup
1 parent e78baca commit 0329180

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/SourceMap.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@ const lineCounter = require('./utils/lineCounter');
33

44
class SourceMap {
55
constructor(mappings, sources) {
6-
this.mappings = mappings || [];
6+
this.mappings = this.purifyMappings(mappings);
77
this.sources = sources || {};
88
this.lineCount = null;
99
}
1010

11+
purifyMappings(mappings) {
12+
if (Array.isArray(mappings)) {
13+
return mappings.filter(mapping => {
14+
return (
15+
mapping &&
16+
mapping.source &&
17+
mapping.original &&
18+
typeof mapping.original.line === 'number' &&
19+
mapping.original.line > 0 &&
20+
typeof mapping.original.column === 'number' &&
21+
mapping.generated &&
22+
typeof mapping.generated.line === 'number' &&
23+
mapping.generated.line > 0 &&
24+
typeof mapping.generated.column === 'number'
25+
);
26+
});
27+
}
28+
29+
return [];
30+
}
31+
1132
async getConsumer(map) {
1233
if (map instanceof SourceMapConsumer) {
1334
return map;
1435
}
15-
36+
map = typeof map === 'string' ? JSON.parse(map) : map;
1637
return await new SourceMapConsumer(map);
1738
}
1839

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { local } from './local';
2+
3+
export function env() {
4+
return local;
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.local = process.env.NODE_ENV;

test/sourcemaps.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ describe('sourcemaps', function() {
6161
assert.equal(output.env(), process.env.NODE_ENV);
6262
});
6363

64+
it('should create a valid sourcemap as a child of a nested TS bundle', async function() {
65+
let b = await bundle(
66+
__dirname + '/integration/sourcemap-typescript-nested/index.ts'
67+
);
68+
69+
assertBundleTree(b, {
70+
name: 'index.js',
71+
assets: ['index.ts', 'local.ts'],
72+
childBundles: [
73+
{
74+
name: 'index.map',
75+
type: 'map'
76+
}
77+
]
78+
});
79+
80+
let raw = fs
81+
.readFileSync(path.join(__dirname, '/dist/index.js'))
82+
.toString();
83+
let map = fs
84+
.readFileSync(path.join(__dirname, '/dist/index.map'))
85+
.toString();
86+
mapValidator(raw, map);
87+
88+
let output = run(b);
89+
assert.equal(typeof output.env, 'function');
90+
assert.equal(output.env(), process.env.NODE_ENV);
91+
});
92+
6493
it('should create a valid sourcemap for a js file with requires', async function() {
6594
let b = await bundle(__dirname + '/integration/sourcemap-nested/index.js');
6695

0 commit comments

Comments
 (0)