Share code in two applications in React Native

I have two applications that actually use the same code base (inside the src folder), with the exception of several configurations. The current folder structure is as follows:

β”œβ”€β”€ app1
β”‚   β”œβ”€β”€ android
β”‚   β”œβ”€β”€ index.android.js
β”‚   β”œβ”€β”€ index.ios.js
β”‚   β”œβ”€β”€ ios
β”‚   β”œβ”€β”€ node_modules
β”‚   β”œβ”€β”€ package.json
β”‚   └── src
└── app2
    β”œβ”€β”€ android
    β”œβ”€β”€ index.android.js
    β”œβ”€β”€ index.ios.js
    β”œβ”€β”€ ios
    β”œβ”€β”€ node_modules
    β”œβ”€β”€ package.json
    └── src

Inside index.android.jsand index.ios.jsI need modules from src / folder, for example.import MainView from './src/mainView';

Thus, I have to maintain the same code base in two different places, which is certainly not ideal. This is why I would like to have a folder structure like this:

β”œβ”€β”€ app1
β”‚   β”œβ”€β”€ android
β”‚   β”œβ”€β”€ index.android.js
β”‚   β”œβ”€β”€ index.ios.js
β”‚   β”œβ”€β”€ ios
β”‚   β”œβ”€β”€ node_modules
β”‚   └── package.json
β”œβ”€β”€ app2
β”‚   β”œβ”€β”€ android
β”‚   β”œβ”€β”€ index.android.js
β”‚   β”œβ”€β”€ index.ios.js
β”‚   β”œβ”€β”€ ios
β”‚   β”œβ”€β”€ node_modules
β”‚   └── package.json
└── src

Unfortunately, the required calls do not correctly resolve the path with this structure. When I use import MainView from '../src/MainView', the following error message appears:

error

Is there any other way to achieve what I want?

+4
2

.

src. .

0

. , watchman. RN .

, , npm ( , ). . GitHub ( ), GH repos .

0

Source: https://habr.com/ru/post/1660072/


All Articles