From fba8b2dc1f4307d454c53844009365118f74aef3 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 24 Jul 2017 10:35:30 -0700 Subject: [PATCH] Use `String#slice()` for inlining source map --- src/index.ts | 9 +++++---- tests/emit-compiled.ts | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index bde082a81..c388becdc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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) @@ -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 } /** diff --git a/tests/emit-compiled.ts b/tests/emit-compiled.ts index c2eeffda3..a2e22c7c8 100644 --- a/tests/emit-compiled.ts +++ b/tests/emit-compiled.ts @@ -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) }