DEV Community

Cover image for React Library Builder using Rollup.js and Storybook
Flavio Del Grosso
Flavio Del Grosso

Posted on • Updated on

React Library Builder using Rollup.js and Storybook

Creating a Custom React Library

Creating a custom React library can be a great way to share reusable components and functionality across multiple projects. This guide will walk you through the process of setting up and developing a library using the React Library Builder.

Step 1: Set up the Project

First, create a new directory for your library and navigate to it in the terminal. Then, clone the React Library Builder repository using the following command:

git clone https://github.com/flaviodelgrosso/react-library-builder.git
cd react-library-builder
yarn install
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can download the repository as a zip file of the latest release from here and extract it to your new directory.

Once you have the repository downloaded, navigate to the project directory and run the following command to install the necessary dependencies:

yarn install
Enter fullscreen mode Exit fullscreen mode

Step 2: Develop Your Components

Now that your project is set up, you can start developing your custom components. In the src/components folder you can start creating a new component file. For example, if you were creating a button component, you would create a file called Button.tsx in the src/components folder.

As you develop your components, make sure to update the src/index.ts file to include them in your library. This will allow other developers to import and use your components in their own projects.

It's also important to include a YourComponent.story.tsx file for each component you create. This file will allow you to view and test your component in Storybook, a tool for developing and testing UI components.

Step 3: Style Your Components

The React Library Builder supports both SCSS and CSS out of the box, so you can style your components as you normally would. If you want to use CSS modules, you can refer to the rollup-plugin-postcss documentation for more information.

Step 4: Test Your Components

Testing is crucial when developing a library, as it helps ensure that your components work as expected. The React Library Builder uses Vitest and @testing-library/react for testing. You can find an example test file in the Button.spec.tsx file.

To run the tests, use the following command:

yarn test
Enter fullscreen mode Exit fullscreen mode

To run the test and show the coverage, use the following command:

yarn test:coverage
Enter fullscreen mode Exit fullscreen mode

Step 5: Linting

Linting is an important step in maintaining code quality, The React Library Builder uses ESLint and eslint-config-prettier for linting. You can modify linting rules by overriding them in the .eslintrc file.

To run the linter, use the following command:

yarn lint
Enter fullscreen mode Exit fullscreen mode

To automatically fix linting issues, use the following command:

yarn lint:fix
Enter fullscreen mode Exit fullscreen mode

Step 6: Publishing Your Library

When your library is ready, you can publish it to NPM so that others can use it in their own projects. Make sure you have an active account with NPM, and that your .npmrc file is set up correctly. Also, ensure that the repository URL in the package.json file is set to your repository URL. Then, use the following command to release your library:

yarn release
Enter fullscreen mode Exit fullscreen mode

Step 7: Deploy Storybook to GitHub Pages

You can also deploy your Storybook to GitHub Pages to make it easier for others to view and test your components. Make sure the repository URL in the package.json file is set to your repository URL, then use the following command:

yarn deploy
Enter fullscreen mode Exit fullscreen mode

And that's it! You now have a working React library that you can use in your own projects and share with others. Don't forget to refer to the Storybook documentation for information on custom layouts, styling, and more.

Support

Any support, feedback and contributions are always welcome and highly appreciated. If you have any issues or suggestions for new features, please feel free to open an issue on the GitHub repository. Pull requests are also welcome. Thanks for your attention!

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!