Skip to content

Commit

Permalink
Use String#slice() for inlining source map
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 24, 2017
1 parent c69b242 commit fba8b2d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Expand Up @@ -469,7 +469,7 @@ function readThrough (
debug('readThrough', fileName)

const [value, sourceMap] = compile(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap)
const output = updateOutput(value, fileName, sourceMap, getExtension)

cache.outputs[fileName] = output

Expand All @@ -494,7 +494,7 @@ function readThrough (
} catch (err) {/* Ignore. */}

const [value, sourceMap] = compile(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap)
const output = updateOutput(value, fileName, sourceMap, getExtension)

cache.outputs[fileName] = output
writeFileSync(outputPath, output)
Expand All @@ -506,11 +506,12 @@ function readThrough (
/**
* Update the output remapping the source map.
*/
function updateOutput (outputText: string, fileName: string, sourceMap: string) {
function updateOutput (outputText: string, fileName: string, sourceMap: string, getExtension: (fileName: string) => string) {
const base64Map = new Buffer(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64')
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`
const sourceMapLength = `${basename(fileName)}.map`.length + (getExtension(fileName).length - extname(fileName).length)

return outputText.replace(/[^=]+$/, sourceMapContent)
return outputText.slice(0, -1 * sourceMapLength) + sourceMapContent
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/emit-compiled.ts
Expand Up @@ -7,7 +7,7 @@ extensions.forEach(ext => {
const _compile = m._compile

m._compile = (code, path) => {
console.log(code)
console.error(code)
return _compile.call(this, code, path)
}

Expand Down

0 comments on commit fba8b2d

Please sign in to comment.