Skip to content

Commit 4c7fd40

Browse files
committed
Correctly combine input source maps when using outDir with transformers.
1 parent 4d18324 commit 4c7fd40

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/transformer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ function combineInlineSourceMaps(
205205
function combineSourceMaps(
206206
program: ts.Program, filePath: string, tscSourceMapText: string): string {
207207
const tscSourceMap = parseSourceMap(tscSourceMapText);
208+
const fileDir = path.dirname(filePath);
208209
let tscSourceMapGenerator: SourceMapGenerator|undefined;
209210
for (const sourceFileName of tscSourceMap.sources) {
210-
const sourceFile = program.getSourceFile(sourceFileName);
211+
const sourceFile = program.getSourceFile(path.resolve(fileDir, sourceFileName));
211212
if (!sourceFile || !containsInlineSourceMap(sourceFile.text)) {
212213
continue;
213214
}

test/e2e_source_map_test.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import {assert, expect} from 'chai';
1212
import * as path from 'path';
13-
import {SourceMapConsumer} from 'source-map';
13+
import {RawSourceMap, SourceMapConsumer} from 'source-map';
1414
import * as ts from 'typescript';
1515

1616
import * as cliSupport from '../src/cli_support';
@@ -251,14 +251,25 @@ function createTests(useTransformer: boolean) {
251251
expect(sourceMap.originalPositionFor({line, column}).line).to.equal(6, 'method position');
252252
});
253253

254-
it('handles input source maps with different file names than supplied to tsc', () => {
254+
function createInputWithSourceMap(overrides: Partial<RawSourceMap> = {}): Map<string, string> {
255255
const sources = new Map<string, string>();
256-
const inputSourceMap =
257-
`{"version":3,"sources":["original.ts"],"names":[],"mappings":"AAAA,MAAM,EAAE,EAAE,CAAC","file":"foo/bar/intermediate.ts","sourceRoot":""}`;
258-
const encodedSourceMap = Buffer.from(inputSourceMap, 'utf8').toString('base64');
256+
const inputSourceMap = {
257+
'version': 3,
258+
'sources': ['original.ts'],
259+
'names': [],
260+
'mappings': 'AAAA,MAAM,EAAE,EAAE,CAAC',
261+
'file': 'intermediate.ts',
262+
'sourceRoot': '',
263+
...overrides
264+
};
265+
const encodedSourceMap = Buffer.from(JSON.stringify(inputSourceMap), 'utf8').toString('base64');
259266
sources.set('intermediate.ts', `const x = 3;
260267
//# sourceMappingURL=data:application/json;base64,${encodedSourceMap}`);
268+
return sources;
269+
}
261270

271+
it('handles input source maps with different file names than supplied to tsc', () => {
272+
const sources = createInputWithSourceMap({file: 'foo/bar/intermediate.ts'});
262273
const {compiledJS, sourceMap} = compile(sources, {useTransformer});
263274
expect(getInlineSourceMapCount(compiledJS)).to.equal(0);
264275

@@ -268,6 +279,18 @@ function createTests(useTransformer: boolean) {
268279
.to.equal('original.ts', 'input file name');
269280
});
270281

282+
it('handles input source maps with an outDir different than the rootDir', () => {
283+
const sources = createInputWithSourceMap({file: 'foo/bar/intermediate.ts'});
284+
285+
const {compiledJS, sourceMap} =
286+
compile(sources, {inlineSourceMap: true, useTransformer, outFile: '/out/output.js'});
287+
288+
const {line, column} = getLineAndColumn(compiledJS, 'x = 3');
289+
expect(sourceMap.originalPositionFor({line, column}).source)
290+
.to.equal('original.ts', 'input file name');
291+
});
292+
293+
271294
it(`doesn't blow up putting an inline source map in an empty file`, () => {
272295
const sources = new Map<string, string>();
273296
sources.set('input.ts', ``);

0 commit comments

Comments
 (0)