Skip to content

Commit fba8b2d

Browse files
committed
Use String#slice() for inlining source map
1 parent c69b242 commit fba8b2d

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ function readThrough (
469469
debug('readThrough', fileName)
470470

471471
const [value, sourceMap] = compile(code, fileName, lineOffset)
472-
const output = updateOutput(value, fileName, sourceMap)
472+
const output = updateOutput(value, fileName, sourceMap, getExtension)
473473

474474
cache.outputs[fileName] = output
475475

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

496496
const [value, sourceMap] = compile(code, fileName, lineOffset)
497-
const output = updateOutput(value, fileName, sourceMap)
497+
const output = updateOutput(value, fileName, sourceMap, getExtension)
498498

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

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

516517
/**

tests/emit-compiled.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extensions.forEach(ext => {
77
const _compile = m._compile
88

99
m._compile = (code, path) => {
10-
console.log(code)
10+
console.error(code)
1111
return _compile.call(this, code, path)
1212
}
1313

0 commit comments

Comments
 (0)