Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using create-react-app to create a library

I'm trying to create a library of React-Redux and I'm trying to use create-react-app to get the boilerplate code but it include in the build script things like appHtml which are irrelevant.

Is there a way to turn ejected create-react-app to library - specifically I need to not package all js files into one but instead just pass them through babel and produce separate files for each React Component?

like image 714
Ido Ran Avatar asked Sep 25 '16 08:09

Ido Ran


1 Answers

I have managed to build library without ejecting and using internal dependencies of create react app. I've tested it with [email protected]. At first you need to install babel-cli:

npm install --save-dev babel-cli

create .babelrc file with content:

{
  "presets": ["react-app"]
}

Then add script to package.json:

"compile": "NODE_ENV=production babel src --out-dir lib --copy-files",

And finally run:

npm run compile

This will compile all sources from src to lib directory and simply copy the remaining files. Don't forget to declare the main file in package.json (e.g. "main"="lib/index.js").

However there is one caveat. If you are using files which cannot be compiled with babel (e.g. css or fonts) then the users of your library would need to set up appropriate (webpack) loaders.

like image 67
madox2 Avatar answered Sep 25 '22 18:09

madox2