<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Renato Duarte on Medium]]></title>
        <description><![CDATA[Stories by Renato Duarte on Medium]]></description>
        <link>https://medium.com/@rntdrt.s?source=rss-601624520656------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XtGqs7YaoZzLDARQebP57Q.jpeg</url>
            <title>Stories by Renato Duarte on Medium</title>
            <link>https://medium.com/@rntdrt.s?source=rss-601624520656------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 10:26:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rntdrt.s/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What if you could build a single app for web and mobile?]]></title>
            <link>https://medium.com/@rntdrt.s/what-if-you-could-build-a-single-app-for-web-and-mobile-b63b74e5d2a1?source=rss-601624520656------2</link>
            <guid isPermaLink="false">https://medium.com/p/b63b74e5d2a1</guid>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[bundler]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Renato Duarte]]></dc:creator>
            <pubDate>Wed, 27 Nov 2019 20:19:21 GMT</pubDate>
            <atom:updated>2019-11-27T20:19:21.951Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TzG6y_ah8kij1NoTk4DjVg.jpeg" /></figure><p>My goal in this post is to write a monorepo application containing web and native apps, sharing the maximum amount of code possible. However, before we dive into the code I feel the need to explain what brought me here.</p><p>I’ve been working at startups since the beginning of my career, where the generic implicit goal is to do more with less. Unfortunately, this can come at a price in the long run despite some short term wins.</p><blockquote>A black box is not magic nor less code means simplicity!</blockquote><p>One of the most common mistakes developers make is to choose a zero-configuration solution over a more configurable and complex one. We tend to focus on more immediate problems, stick to what the team is comfortable with and be restricted to a short schedule.</p><p>Another thing I’ve been noticing is how difficult it is to leave this cycle once it becomes part of the team’s culture.</p><blockquote>How can one invert the cycle?</blockquote><p>I find it helpful to follow three simple steps to make this kind of decisions:</p><ul><li>Deeply analyze the problem</li><li>Understand the available solutions</li><li>Provide a proof of concept (PoC)</li></ul><p>The PoC, from my experience, is the most important of them all. It not only shows a possible solution but in most cases, it removes the fear of stepping into the unknown.</p><p>This article is an outcome of one of those PoC’s. I didn’t make it in order to back any technical decision. I did it because I have seen startups deciding to focus either on a web or a mobile solution without ever considering the possibility of having both in one.</p><p>I’m going to show you how I was able to build a shared web &amp; mobile app. Below is a step by step guide as well as my reasoning for each choice I made.</p><p>Let’s start by creating our monorepo.</p><pre><strong>$</strong> mkdir react-monorepo</pre><pre><strong>$</strong> cd react-monorepo</pre><pre><strong>$</strong> yarn init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*P7hEp7ZPrw9npFTm29n5xA.png" /><figcaption>yarn init</figcaption></figure><p>Note that private: true is required if we mean to use yarn workspaces. You can read more about it <a href="https://yarnpkg.com/lang/en/docs/workspaces/#toc-how-to-use-it">here</a>!</p><p>Before we add more configuration to our monorepo, let’s build our boilerplate:</p><pre><strong>$</strong> mkdir packages</pre><pre><strong>$</strong> cd packages</pre><p>Then we should create our mobile app. For this, you will need the CLI, more info <a href="https://facebook.github.io/react-native/docs/getting-started">here</a>:</p><pre><strong>$</strong> react-native init app</pre><pre><strong># In case you want to test it (you may need to add some dependencies, read the documentation):<br>$</strong> react-native run-ios --simulator=&quot;iPhone 11&quot;</pre><p>Now we can create our web app. To do this, you can add a personal project of mine, — react-bootstrap — you can find it <a href="https://github.com/rntdrts/react-bootstrap">here</a>, or if you prefer, create your own.</p><pre><strong>$</strong> git clone <a href="https://github.com/rntdrts/react-bootstrap.git">https://github.com/rntdrts/react-bootstrap.git</a> web</pre><pre><strong># remove git references</strong><br><strong>$</strong> cd web</pre><pre><strong>$</strong> rm -rf .git</pre><pre><strong># In case you want to test it (you may need to add some dependencies, read the documentation):<br>$</strong> yarn --ignore-scripts &amp;&amp; yarn start</pre><p>For now, you should have this structure:</p><pre>packages/<br>  - app/<br>  - web/<br>package.json</pre><p>It’s time to configure our monorepo, so go to the base of your repository, edit yourpackage.json and add our workspaces:</p><pre>{<br>  &quot;name&quot;: &quot;react-monorepo&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;React &amp; React Native App&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;,<br>  &quot;private&quot;: true,<br>  <strong>&quot;workspaces&quot;: [<br>    &quot;packages/**&quot;<br>  ]</strong><br>}</pre><p>Before installing our dependencies, make sure every package.json on each project has the following attributes:</p><pre>{<br>  &quot;name&quot;: &quot;react-monorepo&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;private&quot;: true,<br>  ...<br>}</pre><p>Let’s now install our dependencies:</p><pre><strong># Before we move forward let&#39;s just make sure we have our dependencies clean!<br>$</strong> rm -rf node_modules &amp;&amp; rm -rf packages/**/node_modules</pre><pre><strong>$</strong> yarn</pre><p>Now let’s see if everything works under our projects, let’s start with our web app:</p><pre><strong>$</strong> cd packages/web</pre><pre><strong>$</strong> yarn start</pre><p>I have my🤞and hope everything went as smoothly for you as it did for me! Now let’s try our mobile app:</p><pre><strong>$</strong> cd ../app</pre><pre><strong>$</strong> react-native run-ios --simulator=&quot;iPhone 11&quot;</pre><p>Did you have this error?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*M6O-dupuZa-xUbKNPT2aWQ.png" /><figcaption>💥💥💥💥💥💥💥💥</figcaption></figure><p>So what is happening? Since we are using <strong>yarn workspaces</strong> our main dependencies are installed under the root of the project and yarn is smart enough to understand this. However, since this build is related to react-native he doesn’t have the same level of understanding.</p><p>To fix this, we need to tell yarn not to hoist the react-native dependency. At the end of your package.json add the following:</p><pre><strong>...<br>  </strong>,<br>  &quot;workspaces&quot;: {<br>    &quot;nohoist&quot;: [&quot;react-native&quot;, &quot;react-native/**&quot;]<br>  }<br>  ...</pre><p>Now for this to work, clean your dependencies and run your app again:</p><pre><strong># Before we move forward, let&#39;s just make sure we have our dependencies clean!<br>$</strong> rm -rf node_modules &amp;&amp; rm -rf packages/**/node_modules</pre><pre><strong>$</strong> yarn</pre><pre><strong>$</strong> react-native run-ios --simulator=&quot;iPhone 11&quot;</pre><p>Everything should work now 🤞!</p><p>At this point, you have our web and mobile apps working <strong>separately</strong>! Let’s create our shared application, then.</p><pre><strong>#</strong> <strong>under the <em>packages</em> folder...</strong></pre><pre><strong>$</strong> mkdir shared</pre><pre><strong>$</strong> cd shared</pre><pre><strong>$</strong> yarn init</pre><p>The outcome should be something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*oAExt2hLIXA03pec9nDrEQ.png" /><figcaption>yarn init</figcaption></figure><p>We then need to build our (initial) boilerplate:</p><pre><strong>$</strong> mkdir src</pre><pre><strong>$</strong> touch src/index.js</pre><p>With this, we can already have our logic code shared between our native and web applications. But what if we want to share components? 🤔</p><p>We can solve this by using <strong><em>universal component libraries</em></strong>, like:</p><ul><li><a href="https://github.com/lelandrichardson/react-primitives/">react-primitives</a></li><li><a href="https://github.com/necolas/react-native-web">react-native-web</a></li></ul><p>To optimize our bundling tools I wen with react-native-web. Let me show you:</p><pre><strong># packages/shared<br>$</strong> yarn add react react-native<br><strong>$</strong> mkdir -p src/components/Shared<br><strong>$</strong> touch src/components/Shared/index.js</pre><p>On our shared component let’s add the following code:</p><pre><strong># src/components/Shared/index.js<br></strong>import React from &#39;react&#39;;<br>import { View, Text } from &#39;react-native&#39;;</pre><pre>export const Shared = () =&gt; (<br>  &lt;View style={{ flex: 1, justifyContent: &#39;center&#39;, alignItems: &#39;center&#39; }}&gt;<br>    &lt;Text&gt;Shared Component!&lt;/Text&gt;<br>  &lt;/View&gt;<br>);</pre><p>That’s it! We create our first shared component! 🎉</p><p>But… yeah, it’s not working! We still need to make some magic!</p><pre><strong># packages/shared/src/index.js<br></strong>export * from &#39;./components/Shared&#39;;</pre><p>Now let’s point our main to the correct file and let’s give it a proper name:</p><pre><strong># packages/shared/package.json</strong></pre><pre>{<br>  <strong>&quot;name&quot;: &quot;@monorepo/shared&quot;</strong>,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  <strong>&quot;main&quot;: &quot;src/index.js&quot;</strong>,<br>  &quot;license&quot;: &quot;MIT&quot;,<br>  &quot;private&quot;: true,<br>  &quot;dependencies&quot;: {<br>    &quot;react&quot;: &quot;^16.12.0&quot;,<br>    &quot;react-native&quot;: &quot;^0.61.5&quot;<br>  }<br>}</pre><p>Let’s make this work on our native app:</p><pre><strong># packages/app/App.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { Shared } from &#39;@monorepo/shared&#39;;</pre><pre>const App = () =&gt; &lt;Shared /&gt;;</pre><pre>export default App;</pre><p>Now the magic part:</p><pre><strong># packages/app/metro.bundle.js</strong></pre><pre>const path = require(&#39;path&#39;);</pre><pre>/**<br> * Metro configuration for React Native<br> * <a href="https://github.com/facebook/react-native">https://github.com/facebook/react-native</a><br> *<br> * <a href="http://twitter.com/format">@format</a><br> */<br>module.exports = {<br>  <strong>watchFolders: [<br>    path.resolve(__dirname, &#39;../shared&#39;),<br>  ],<br>  resolver: {<br>    extraNodeModules: new Proxy({}, {<br>      get: (target, name) =&gt;  path.join(process.cwd(), `node_modules/${name}`)<br>    }),<br>  },</strong><br>  transformer: {<br>    getTransformOptions: async () =&gt; ({<br>      transform: {<br>        experimentalImportSupport: false,<br>        inlineRequires: false,<br>      },<br>    }),<br>  },<br>};</pre><p>To fasten up things, go ahead and run:</p><pre><strong>$</strong> react-native run-ios --simulator=&quot;iPhone 11&quot;</pre><p>If you’re questioning yourself about the changes in the metro.bundle.js. Instead of telling you, let me show you:</p><ul><li>Edit the text on your shared component (<em>whatchFolders</em>)</li><li>Remove the resolver block from your metro.bundle.js and rebuild your app (something must be missing).</li></ul><p>Now let’s make this work with our web app:</p><pre><strong># packages/web/src/containers/Landing/index.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { Shared } from &#39;<a href="http://twitter.com/monorepo/shared">@monorepo/shared</a>&#39;;</pre><pre>const Landing = () =&gt; {<br>  return &lt;Shared /&gt;;<br>};</pre><pre>export default Landing;</pre><p>As we did under our bundler in our native app, we need to do some magic on our web bundler:</p><pre><strong># packages/web/src/webpack.common.js</strong><br>  ...<br>  module: {<br>    rules: [<br>      {<br>        test: /\.jsx?$|\.web\.js?$/,<br>        exclude: /node_modules/,<br><strong>        include: [<br>          path.resolve(__dirname, &#39;../shared/src&#39;),<br>          path.resolve(__dirname, &#39;src&#39;)<br>        ],</strong><br>        use: [&#39;babel-loader&#39;]<br>      }<br>    ]<br>  },<br>  resolve: {<br>    alias: {<br>      ...<br>      utils: path.join(__dirname, &#39;src/utils&#39;),<br>      <strong>common: path.resolve(__dirname, &#39;../shared/src&#39;),<br>      &#39;react-native&#39;: &#39;react-native-web&#39;</strong><br>    }<br>  }<br>});</pre><p>We need to add our <strong><em>universal component library</em></strong>:</p><pre><strong># packages/web</strong></pre><pre><strong>$</strong> yarn add react-native-web</pre><p>Because our web bundler runs eslint to check our code standards we need to add it to validate our code (you can check this by running yarn start):</p><pre><strong># packages/web</strong></pre><pre><strong>$</strong> cp .eslint.js ../shared</pre><p>If you run yarn start, you should see an error message: unexpected token. This means that our bundler doesn’t know how to process that code. Let’s tell him how:</p><pre><strong># packages/web</strong></pre><pre><strong>$</strong> cp .babelrc ../shared</pre><pre><strong>$</strong> touch babel.config.js</pre><p>This is our last step, we need to tell babel that we have a configuration on our shared project:</p><pre><strong># packages/web/babel.config.js</strong></pre><pre>module.exports = {<br>  babelrcRoots: [<br>    &#39;.&#39;,<br>    &#39;../shared/&#39;<br>  ]<br>};</pre><p>Now everything should work:</p><pre><strong># packages/web</strong></pre><pre><strong>$</strong> yarn start</pre><p>If you edit the shared component, he should update on both — web and mobile. 🎉</p><p>Despite the similarities between web and mobile applications, the differences exponentially grow over time and complexity. This means that if we stop our PoC in the near future we would be thinking about separating our apps.</p><p>Since this is not what we want… How can we solve this?</p><p>What if we could separate our code for web and mobile, but we didn’t need to apply any logic to do it? Yes, you read it right… let me give you a hint: our separated bundling for our shared app wasn’t innocent.</p><p>What I propose is the following:</p><pre><strong># packages/shared</strong></pre><pre><strong>$</strong> mkdir src/components/Separated</pre><pre><strong>$</strong> touch src/components/Separated/index.<strong>native</strong>.js</pre><pre><strong>$</strong> touch src/components/Separated/index.<strong>web</strong>.js</pre><p>Let’s create our components:</p><pre><strong># packages/shared/src/components/Separated/index.native.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { Text, View } from &#39;react-native&#39;;</pre><pre>export const Separated = () =&gt; (<br>  &lt;View style={{ flex: 1, justifyContent: &#39;center&#39;, alignItems: &#39;center&#39; }}&gt;<br>    &lt;Text&gt;Mobile Separated Component!&lt;/Text&gt;<br>  &lt;/View&gt;<br>);</pre><pre><strong># packages/shared/src/components/Separated/index.web.js</strong></pre><pre>import React from &#39;react&#39;;</pre><pre>export const Separated = () =&gt; &lt;div&gt;Web Separated Component!&lt;/div&gt;;</pre><p>Luckily react-native already thought about <a href="https://facebook.github.io/react-native/docs/platform-specific-code">this</a> ahead of us, so we only need to change our component and everything should work:</p><pre><strong># packages/app/App.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { Separated } from &#39;<a href="http://twitter.com/monorepo/shared">@monorepo/shared</a>&#39;;</pre><pre>const App = () =&gt; &lt;Separated /&gt;;</pre><pre>export default App;</pre><p>If you look at your mobile simulator you should see your separated component! 🎉</p><p>However, we still need to tell our web application to load only .web.js:</p><pre><strong># packages/web/webpack.common.js</strong><br>  ...<br>  resolve: {<br>    alias: {<br>      ...<br>    }<strong>,<br>    extensions: [&#39;.web.js&#39;, &#39;.js&#39;]</strong><br>  }</pre><p>Don’t forget to change the component:</p><pre><strong># packages/web/src/container/Landing/index.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { Separated } from &#39;<a href="http://twitter.com/monorepo/shared">@monorepo/shared</a>&#39;;</pre><pre>const Landing = () =&gt; {<br>  return &lt;Separated /&gt;;<br>};</pre><pre>export default Landing;</pre><p>You can now run your app:</p><pre><strong># packages/web</strong></pre><pre><strong>$</strong> yarn start</pre><p>Congratulations you have finished your PoC! 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*0zQXcFSzxpKxAin_2QIMlg.png" /></figure><p>Remember that with this configuration you can even separate your logic code and since you’re using a different build per application you can inject different configuration values. I enjoy this solution because of its flexibility. I hope next time you have a decision to make you dedicate the time to make a PoC and remember:</p><blockquote>A black box is not magic nor less code means simplicity!</blockquote><p>Thank you for your time and I hope you enjoyed the article! 🙏</p><p>You can find the code <a href="https://github.com/rntdrts/monorepo-singleapp">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b63b74e5d2a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>