简体   繁体   中英

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?

I have managed to build library without ejecting and using internal dependencies of create react app. I've tested it with react-scripts@0.9.5 . 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 (eg "main"="lib/index.js" ).

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

Edit the following in the package.json

  1. name
  2. description
  3. scripts
  4. author
  5. license
  6. gitRepository

and do npm publish . It'll publish your library in npm

这是迄今为止我发现的最好的..使用 create-react-app 库https://github.com/Rubbby/create-react-library

Maybe you could bootstrap youre projects with js-library-boilerplate . I've use it several times and it's good enough to give it a try. It can be used as a base for the vanila js libs or react libs (use CRA under the hood).

To use it all you need, is to:

  • clone repo
  • make some changes as:


  • Edit LICENSE file

  • Edit package.json information (These will be used to generate the headers for your built files)
  • Edit library: "MyLibrary" with your library's export name in ./config/webpack.config.js
  • Edit ./bin/postinstall (If you would like to display a message on package install)

...and you a ready to go. Also all configs are open so you can adjust it as you require. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM