Configuration for create-react-app after ejecting to build a component library

后端 未结 3 1862
悲哀的现实
悲哀的现实 2020-12-28 09:27

I started to build a component library that I want to publish on NPM (and reuse in other apps) by creating a React app using create-react-app. Unfortunately the create

3条回答
  •  醉话见心
    2020-12-28 10:02

    create-react-app is meant for quick setting up and development of apps, not really for development of libraries. For one thing, create-react-app creates an index.html file when building, which is usually not needed by libraries. Libraries require a different set of configuration (much less).

    I faced this problem myself and have written a React component boilerplate for writing and publishing React components: https://github.com/yangshun/react-component-starter, after referencing how popular ES6 libraries were written, such as Redux.

    Pros:

    • Similar to create-react-app, batteries are included
    • It has got all the important areas covered: development, linting, testing, and distributing
    • Minimal configuration
    • Babel for transpilation to ES5 and CommonJS format
    • ESLint included and configured
    • Jest tests examples
    • Webpack configuration that compiles to UMD
    • React Storybook for quick development of the components
    • Support for stylesheet distribution alongside your component

    Cons:

    • Highly opinionated in terms of choice of tooling; they follow the choices set by create-react-app.
    • Have to manually update each package dependencies in future (you'll face this issue if you did eject anyways)

    There is minimal configuration included and setup is easy. The various webpack, Babel and ESLint configurations should be quite understandable.

    Hope this will be helpful to you. Feel free to give suggestions and comments on how it can be improved.

提交回复
热议问题