Skip to content

Commit 50977c0

Browse files
author
David Sheldrick
committed
force generateSourceMaps, compose source maps manually
1 parent e2d89a1 commit 50977c0

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

__specs__/__snapshots__/transformer.test.js.snap

+2-21
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,11 @@ exports.default = Cheese;"
1212

1313
exports[`the transformer works 2`] = `
1414
Object {
15-
"mappings": "AAAA;;AACAA,OAAOC,cAAP,CAAsBC,OAAtB,EAA+B,YAA/B,EAA6C,EAAEC,OAAO,IAAT,EAA7C;AACA,SAASC,MAAT,GAAkB;AACd,WAAO,EAAEC,QAAQ,SAAV,EAAP;AACH;AACDH,QAAQI,OAAR,GAAkBF,MAAlB",
16-
"names": Array [
17-
"Object",
18-
"defineProperty",
19-
"exports",
20-
"value",
21-
"Cheese",
22-
"cheese",
23-
"default",
24-
],
15+
"mappings": ";;;AAKA,SAAA,MAAA,GAAA;AACE,WAAO,EAAC,QAAQ,SAAT,EAAP;AACD;AAFD,QAAA,OAAA,GAAA,MAAA",
16+
"names": Array [],
2517
"sources": Array [
2618
"blah.tsx",
2719
],
28-
"sourcesContent": Array [
29-
"
30-
type Cheese = {
31-
readonly cheese: string
32-
}
33-
34-
export default function Cheese(): Cheese {
35-
return {cheese: 'stilton'};
36-
}
37-
",
38-
],
3920
"version": 3,
4021
}
4122
`;

index.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,38 @@ const TSCONFIG_PATH = process.env.TSCONFIG_PATH
99

1010
const { SourceMapConsumer, SourceMapGenerator } = require('source-map')
1111

12-
function composeSourceMaps(tsMap, babelMap) {
13-
const map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(babelMap))
14-
map.applySourceMap(new SourceMapConsumer(tsMap))
12+
function composeSourceMaps(tsMap, babelMap, fileName) {
13+
// applySourceMap wasn't working for me, so doing it manually
14+
const map = new SourceMapGenerator()
15+
const tsConsumer = new SourceMapConsumer(tsMap)
16+
const babelConsumer = new SourceMapConsumer(babelMap)
17+
18+
babelConsumer.eachMapping(
19+
({
20+
source,
21+
generatedLine,
22+
generatedColumn,
23+
originalLine,
24+
originalColumn,
25+
name,
26+
}) => {
27+
if (originalLine) {
28+
const original = tsConsumer.originalPositionFor({
29+
line: originalLine,
30+
column: originalColumn,
31+
})
32+
if (original.line) {
33+
map.addMapping({
34+
generated: { line: generatedLine, column: generatedColumn },
35+
original: { line: original.line, column: original.column },
36+
source: fileName,
37+
name: null,
38+
})
39+
}
40+
}
41+
}
42+
)
43+
1544
return map.toJSON()
1645
}
1746

@@ -41,21 +70,25 @@ const compilerOptions = Object.assign(tsConfig.compilerOptions, {
4170
})
4271

4372
module.exports.transform = function(sourceCode, fileName, options) {
73+
options = Object.assign({}, options, { generateSourceMaps: true })
74+
4475
if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) {
4576
const tsCompileResult = ts.transpileModule(sourceCode, {
4677
compilerOptions,
4778
fileName,
4879
})
80+
4981
const babelCompileResult = upstreamTransformer.transform(
5082
tsCompileResult.outputText,
5183
fileName,
52-
Object.assign({}, options, { generateSourceMaps: true })
84+
options
5385
)
5486

5587
return Object.assign({}, babelCompileResult, {
5688
map: composeSourceMaps(
5789
tsCompileResult.sourceMapText,
58-
babelCompileResult.map
90+
babelCompileResult.map,
91+
fileName
5992
),
6093
})
6194
} else {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-typescript-transformer",
3-
"version": "1.0.0",
3+
"version": "1.0.3",
44
"description": "TypeScript transformer for react-native",
55
"main": "index.js",
66
"repository": "https://github.com/ds300/react-native-typescript-transformer",

0 commit comments

Comments
 (0)