Our current setup with napps and wapp is a bit of a hassle when you have to work on feature that are cross-platform, with plenty of code duplication. Some of that duplication is unavoidable, but some of it could be removed with a strucutre where we are able to share more code between the platforms.
As long as we don't adopt a abstraction layer for markup (which we've tried and dismissed), views won't be shareable. In React language, we won't be able to share render
methods with platform specific tags. There are also some feature that are tied to the platform, e.g. event listener, interaction handlers etc. Business logic on the other hand, could easily be shared.
React Native’s Metro bundler has capabilities that are well-suited for what we need. It supports loading files with two different naming patterns:
file.ios|android|native.js
file.js
When a file is referenced, the bundler will first look for files with ios|andriod|native
at that path, and if that is not found it will continue to just .js
files. We could use this to separate the files of a component
components/
|- Foo.js
|- Foo.native.js
In this example, Foo.js
would be for tha wapp, and Foo.native.js
for napps. Since we have more control of the bundling of files for the web project we could also use a naming convention with a .web
suffix for web files.
We could create a component folder at the root of src/, where all shared components would be placed. This would make it easier to remember to work on them both for changes that are cross-plattform.
src/
|- common/
|- components/
|- native/
|- web/
This does make the naming of the common
folder a little odd though, since components are also a common resource with this approach. So a better structure might be
src/
|- common/
|- config/
|- components/
|- state/
|- native/
|- web/
or, simpler
src/
|- config/
|- components/
|- native/
|- state/
|- web/
Trying to structure my thoughts on this matter I created a simple example (based on our <Experiment />
component) and implemented it in four different ways. Have a look.