Skip to content

Commit

Permalink
feat: use the inline source content if available (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles committed Feb 6, 2020
1 parent f3d843b commit 1a6d47f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ module.exports = class V8ToIstanbul {
this.sourceMap = await new SourceMapConsumer(rawSourceMap.sourcemap)

let originalRawSource
if (this.sources.originalSource) {
// If the source map has inline source content then it should be here
// so use this inline one instead of trying to read the file off disk
// Not sure what it means to have an array of more than 1 here so just ignore it
// since we wouldn't know how to handle it
if (this.sources.sourceMap && this.sources.sourceMap.sourcemap.sourcesContent.length === 1) {
originalRawSource = this.sources.sourceMap.sourcemap.sourcesContent[0]
} else if (this.sources.originalSource) {
originalRawSource = this.sources.originalSource
} else {
originalRawSource = await readFile(this.path, 'utf8')
Expand Down
31 changes: 31 additions & 0 deletions test/v8-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,37 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap}

v8ToIstanbul.path.should.equal(absoluteSourceFilePath)
})

it('handles sourceContent', async () => {
const sourceFileName = 'sourcemap-source.js'
const sourceRoot = path.dirname(require.resolve(`./fixtures/scripts/${sourceFileName}`))
const absoluteSourceFilePath = path.join(sourceRoot, sourceFileName)
const map = new sourcemap.SourceMapGenerator({
file: sourceFileName
})
map.addMapping({
original: { line: 1, column: 1 },
generated: { line: 1, column: 1 },
source: sourceFileName
})
map.setSourceContent(sourceFileName, readFileSync(absoluteSourceFilePath).toString())

const source = 'const foo = "bar";'
const tmpPath = path.join(os.tmpdir(), crypto.randomBytes(4).readUInt32LE(0) + '.js')
writeFileSync(tmpPath, source)

const sources = {
sourceMap: {
sourcemap: map.toJSON()
}
}
const v8ToIstanbul = new V8ToIstanbul(tmpPath, undefined, sources)
await v8ToIstanbul.load()

// if the source is transpiled and since we didn't inline the source map into the transpiled source file
// that means it was bale to access the content via the provided sources object
v8ToIstanbul.sourceTranspiled.should.not.be.undefined()
})
})

// execute JavaScript files in fixtures directory; these
Expand Down

0 comments on commit 1a6d47f

Please sign in to comment.