Created
September 22, 2016 14:13
-
-
Save miklschmidt/f68a3898a2224b0268d84b19392929db to your computer and use it in GitHub Desktop.
React Native typescript configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
module.exports = { | |
getAssetExts() { | |
return [ | |
'ts', | |
'tsx' | |
] | |
}, | |
getTransformModulePath() { | |
return path.join(__dirname, 'transformer.js'); | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let path = require('path'); | |
let ts = require('typescript'); | |
let tsConfig = require('./tsconfig.json'); | |
let rnTransform = require('react-native/packager/transformer').transform; | |
function transform(data, callback) { | |
// Do custom transformations | |
let result = data.sourceCode; | |
if (path.extname(data.filename) == '.tsx' || path.extname(data.filename) == '.ts') { | |
try { | |
result = ts.transpileModule(result, {compilerOptions: tsConfig.compilerOptions}); | |
result = result.outputText; | |
} catch(e) { | |
callback(e); | |
return; | |
} | |
} | |
// Pass the transformed source to the original react native transformer | |
try { | |
result = rnTransform(result, data.filename, data.options); | |
} catch(e) { | |
callback(e); | |
return; | |
} | |
callback(null, result); | |
} | |
module.exports = transform; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thanks for the gist. I'm having a bit trouble getting this to work though.
I've placed the two files in the root folder of my RN project, but the packager doesn't seem to be picking up the new transformer when I run for example
react-native start
ornpm start
. Is there anything else I should do?