Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure absolute paths for imports in TypeScript based React Native apps?

In order to avoid '../../../../' style relative imports in a TypeScript based React Native app, I would like to configure the app so that I can use absolute imports instead.

It is important that the configuration also supports Jest unit tests.

I created the app using npx react-native init MyTestApp --template typescript

React Native version: 0.60.5

What is the exact configuration I would need to achieve this?

like image 332
Svein Fidjestøl Avatar asked Sep 05 '19 05:09

Svein Fidjestøl


People also ask

How do you use absolute path in TypeScript?

To be able to use absolute paths in TypeScript we can set the baseUrl property in the tsconfig. json file. With this, we define src as our root directory (for module resolution).

How do I add absolute imports in react?

According to create-react-app Docs, We can use absolute imports in our react project by configuring a jsconfig. json / tsconfig. json (for typescript projects) file in the root of our project. If you're using TypeScript in your project, you will already have a tsconfig.


2 Answers

Requirement

// Meh import config from '../../../../../../../config';  // Awesome! import config from '@cuteapp/config'; 

How To

  1. Add this babel plugin package
yarn add --dev babel-plugin-module-resolver 
  1. My babel.config.js
module.exports = {   presets: ['module:metro-react-native-babel-preset'],   plugins: [     [       require.resolve('babel-plugin-module-resolver'),       {         cwd: 'babelrc',         extensions: ['.ts', '.tsx', '.js', '.ios.js', '.android.js'],         alias: {           '@cuteapp': './app'         }       }     ],     'jest-hoist'   ] }; 
  1. My tsconfig.json
{   "compilerOptions": {     "allowJs": true,     "allowSyntheticDefaultImports": true,     "esModuleInterop": true,     "isolatedModules": true,     "jsx": "react",     "lib": ["es2015", "es2015.promise", "es2016.array.include", "dom"],     "strict": true,     "moduleResolution": "node",     "baseUrl": "./",     "paths": {       "@cuteapp/*": ["app/*/index", "app/*"]     },     "noEmit": true,     "resolveJsonModule": true,     "target": "esnext",     "types": ["jest"]   },   "exclude": ["node_modules", "babel.config.js", "metro.config.js"] } 
  1. Restart the IDE.
  2. That's it.
like image 80
I Putu Yoga Permana Avatar answered Sep 29 '22 17:09

I Putu Yoga Permana


Summary:

The npm package babel-plugin-module-resolver is needed, as well as some configuration in tsconfig.json and babel.config.js


Step by step:

  1. Install babel-plugin-module-resolver using npm or yarn.

    npm i babel-plugin-module-resolver --save-dev  # Or (If you're using yarn):  yarn add --dev babel-plugin-module-resolver 
  2. tsconfig.json: Add "baseUrl": "." to compilerOptions

  3. babel.config.js: Add a key named plugins with the following value:

[     [       'module-resolver',       {         extensions: [           '.js',           '.jsx',           '.ts',           '.tsx',           '.android.js',           '.android.tsx',           '.ios.js',           '.ios.tsx'         ],         root: ['.']       }     ]   ] 

Complete configuration:

tsconfig.json:

{   "compilerOptions": {     "allowJs": true,     "allowSyntheticDefaultImports": true,     "esModuleInterop": true,     "isolatedModules": true,     "jsx": "react",     "lib": ["es6"],     "moduleResolution": "node",     "noEmit": true,     "strict": true,     "target": "esnext",     "baseUrl": "."   },   "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"] } 

babel.config.js:

module.exports = {   presets: ['module:metro-react-native-babel-preset'],   plugins: [     [       'module-resolver',       {         extensions: [           '.js',           '.jsx',           '.ts',           '.tsx',           '.android.js',           '.android.tsx',           '.ios.js',           '.ios.tsx'         ],         root: ['.']       }     ]   ] };  

This is for a clean new project created using npx react-native init MyTestApp --template typescript on React Native version 0.60.5

like image 26
Svein Fidjestøl Avatar answered Sep 29 '22 17:09

Svein Fidjestøl