I found that users of Create-React-App script have issues with building few projects that are based on the same
"core" library. Let's imagine that you have a ui
directory where you keep all UI React components. And you have
two projects - blog
and shop
.
Now you wish to use the shared UI components in both these projects. But if you will create a symlink to a "raw" source code (where you use ES2015) - you will see that your code can't be imported, because it expects that they should be already compiled.
To solve this problem we need to do few simple steps:
- go to the shared
ui
directory and run here a compiler. It will watch for changes and will recompile files when you will make changes in them. - go to your project directory
blog
orshop
and create a symlink to a directorybuild
that contains the compiled source code. - work with your projcts
shop
andblog
as you do normally. You can import components from yourui
library and your changes in this library will be visible in both your projects.
Before you start, let's update your package.json
file in both your projects (blog and shop) as shown below
(or take some parts from it to adjust your file). And run npm install
inside your shared directory to install
all required libraries to properly compile your source code.
cd ~/projects/ui
npm run build:watch
cd ~/project/blog/src
ln ~/projects/ui/build ui
cd ~/project/blog
npm start
NOTE: Use a normal import like you do for other components in your ReactJS app. For exmaple you can import a button like this:
import Button from './ui/Button';
- You can use a shared library between multiple projects.
- You can publish your shared library as open source, because it will be compiled and anybody can install it via
npm install
(if you wish)
- You always need to run at least two tabs in a terminal and run one process for compiling your shared library, and second one for compiling your exact project.
If you find this information helpful - please share it to your friends.
Also I will be happy if you will add your star to this gist.
Looks that this issue will be fixed soon here: facebook/create-react-app#7993