diff --git a/src/SourceMap.js b/src/SourceMap.js index a55ea4102c7..4908ab6abb9 100644 --- a/src/SourceMap.js +++ b/src/SourceMap.js @@ -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); } diff --git a/test/integration/sourcemap-typescript-nested/index.ts b/test/integration/sourcemap-typescript-nested/index.ts new file mode 100644 index 00000000000..5c950cb0369 --- /dev/null +++ b/test/integration/sourcemap-typescript-nested/index.ts @@ -0,0 +1,5 @@ +import { local } from './local'; + +export function env() { + return local; +} \ No newline at end of file diff --git a/test/integration/sourcemap-typescript-nested/local.ts b/test/integration/sourcemap-typescript-nested/local.ts new file mode 100644 index 00000000000..6386b8a701c --- /dev/null +++ b/test/integration/sourcemap-typescript-nested/local.ts @@ -0,0 +1 @@ +exports.local = process.env.NODE_ENV; \ No newline at end of file diff --git a/test/sourcemaps.js b/test/sourcemaps.js index 0b6c10618d6..0c2a8f82472 100644 --- a/test/sourcemaps.js +++ b/test/sourcemaps.js @@ -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');