@alot/transformer v1.3.4
@alot/transformer
Use TypeScript and ES6+ directly in your React Native projects.
This is a fork of
ds300/react-native-typescript-transformer
and facebook/metro-bundler with an
extra environment variable, PLATFORM_TARGET. See Platform Specific Builds for more
information.
Usage
Add as a dependency:
yarn add --dev @alot/transformer
Create a
tsconfig.jsonconfiguration in your app root if you haven't already, for example:{ "compilerOptions": { "target": "es2015", "jsx": "react", "noEmit": true, "moduleResolution": "node" }, "exclude": [ "node_modules" ] }Modify or create a
rn-cli.config.jsin your app to definegetTransformModulePathandgetSourceExts, as follows:module.exports = { getTransformModulePath() { return require.resolve('@alot/transformer') }, getSourceExts() { return ['ts', 'tsx']; } }That's it, add
.tsand.tsxfiles to your project andReact Nativewill load them!
Platform specific builds
You might wish to specify different tsconfig.json or .babelrc configurations
for different platform targets, one for android, one for web, and so on. Set
an environment variable PLATFORM_TARGET before running your build to search
for these configuration files in that folder. If .babelrc and tsconfig.json
aren't found there, the app root will be searched afterward.
An example package.json script might look like:
{
"scripts": {
"expo": "cross-env PLATFORM_TARGET=expo react-native-scripts start --reset-cache"
}
}This will cause the transformer to search the expo folder for .babelrc and
tsconfig.json. This might be necessary because
expo uses a custom babel configuration,
which might be incompatible with other platforms.
Smaller bundles with tslib
Instead of defining helper functions in each transpiled module, TypeScript can use a shared library like Babel's use of a polyfill library.
Add tslib:
yarn add tslibAdd
"importHelpers": "true"to thecompilerOptionsof yourtsconfig.json:
```diff
{
"compilerOptions": {
...
+ "importHelpers": true,
...
}
}
```