Skip to content

Commit

Permalink
Add support for loader sourceMap option and generate sourcemap from s…
Browse files Browse the repository at this point in the history
…cratch when previous sourcemap is undefined
  • Loading branch information
Toilal committed Mar 22, 2017
1 parent 25dceec commit 4802a5d
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const genId = require('./gen-id')

const sourceMap = require('source-map')
const SourceMapConsumer = sourceMap.SourceMapConsumer
const SourceMapGenerator = sourceMap.SourceMapGenerator

module.exports = function(contents, sourcemap) {
const id = genId(this.resourcePath)
Expand Down Expand Up @@ -45,29 +44,33 @@ module.exports = function(contents, sourcemap) {
}
})

if (hasComponent) {
if (sourcemap) {
const generatedContents = genCode.generate(res, {
sourceMap: this.resourcePath,
sourceMapWithCode: true,
sourceContent: contents
})
if (!hasComponent) {
this.callback(null, contents, sourcemap)
return;
}

// apply the original sourcemap to the sourcemap generated by escodegen.
const sourceMapGenerator = generatedContents.map
if (this.sourceMap) {
const generatedContents = genCode.generate(res, {
sourceMap: this.resourcePath,
sourceMapWithCode: true,
sourceContent: contents
})

// apply the original sourcemap to the sourcemap generated by escodegen.
const sourceMapGenerator = generatedContents.map

if (sourcemap) {
const originalSmc = new SourceMapConsumer(sourcemap)
sourceMapGenerator.applySourceMap(originalSmc)
const outSourcemap = sourceMapGenerator.toJSON()
}

this.callback(null, generatedContents.code + genHotReload(id), outSourcemap)
} else {
const generatedContents = genCode.generate(res)
const outSourcemap = sourceMapGenerator.toJSON()

this.callback(null, generatedContents + genHotReload(id))
}
this.callback(null, generatedContents.code + genHotReload(id), outSourcemap)
} else {
this.callback(null, contents, sourcemap)
const generatedContents = genCode.generate(res)

this.callback(null, generatedContents + genHotReload(id))
}
}

Expand Down

0 comments on commit 4802a5d

Please sign in to comment.