📦 Publish React npm Package with Rollup
In this tutorial, I’ll walk you through my own process of using Rollup, a popular bundler, to create a React component package, enabling others to easily integrate your components into their projects.
Initialize the repo
Create a new directory called react-lib and initialize a new npm project within it, allowing you to install and manage dependencies for a React library.
1 | mkdir react-lib |
Add dependencies
Add dependencies we need. And Styled-components is a CSS-in-JS library that allows to write CSS in JavaScript code. It provides a powerful API for building reusable and dynamic styled components in React applications.
1 | npm i -D react typescript @types/react |
TypeScript configuration
Initialize a TypeScript project by creating a tsconfig.json file which specifies how TypeScript should compile the project’s source files.
1 | npx tsc --init |
This is an example for tsconfig.json.
1 | { |
ESLint configuration
ESLint is a popular linting tool for JavaScript that helps catch common errors and enforce coding standards, and it can also be configured to catch specific coding issues, such as incorrect syntax or anti-patterns, saving time and prevent bugs from slipping through the cracks.
1 | npm i eslint -D |
Create .eslintignore to exclude certain files from ESLint checks.
1 | test/** |
Add .gitignore
Create .gitignore to exclude certain files from git push.
1 | /node_modules |
Adding components
Write and export your custom component code in the src folder. And here is an example in TypeScript.
src/components/Sum/index.tsx
1
2
3
4
5
6
7
8
9
10
11import React from "react";
import { SumProps } from "./Sum.types";
import StyledSum from "./styles";
const Sum: React.FC<SumProps> = ({ a, b }) => {
const sum = a + b;
return <StyledSum>{a} + {b} = {sum}</StyledSum>;
};
export default Sum;src/components/Sum/Sum.types.ts
1
2
3
4export type SumProps = {
a: number;
b: number;
};src/components/Sum/styles.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import styled from 'styled-components';
const StyledSum = styled.div`
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
font-weight: bold;
padding: 10px;
background-color: #f2f2f2;
border: 1px solid #ccc;
border-radius: 5px;
`;
export default StyledSum;src/components/index.ts
1
export { default as Sum } from './Sum';
src/index.ts
1
export * from './components';
Rollup configuration
Rollup is a module bundler that allows to package your code into modules, making it easier to share and distribute code. It creates a single file from multiple ES6 modules, resulting in smaller output files with faster loading times, and makes it particularly useful for creating libraries and packages, and can improve web application load times, especially on mobile devices.
1 | npm i -D rollup |
Why Rollup
- Rollup uses the ES6 standard format to bundle code.
- It only bundles JavaScript, which makes it fast and generates small bundle sizes.
- It has algorithmic advantages in handling pure code and is suitable for developing JavaScript libraries, it can also be used for bundling application development.
Why not
- When code splitting is needed, and Rollup does not support code splitting.
- If there are many static resources that need to be processed, and the module structure is complex.
- If the project being built requires many dependencies on CommonJS modules.
Packages
- @rollup/plugin-node-resolve: enables to use external libraries and modules. Rollup does not support directly bundling the contents of the node_modules folder, so it needs to be installed.
- @rollup/plugin-commonjs: converts CommonJS modules to ES6 modules, enables to use modules that were not designed for ES6 module syntax. Lodash is not bundled by default as it uses CommonJS and Rollup only processes ES modules, but this package can solve the problem of CommonJS exports and ES module imports.
- @rollup/plugin-typescript: compiles TypeScript files into JavaScript code.
And this is my rollup.config.js.
1 | import resolve from "@rollup/plugin-node-resolve"; |
Add new scripts commands build and build:types to the package.json file.
1 | { |
Run npm run build and npm run build:types to view the bundled results.
1 | npm run build |
Add ts-jest
ts-jest simplifies the process of writing and running tests for TypeScript code, and can also make it easier for other developers to contribute to the project.
1 | npm i jest ts-jest @types/jest -D |
Add testing scripts to the package.json file.
1 | { |
Create a test directory in the root folder to store test files, and run npm run test to view the status of the test cases.
Husky configuration
Use Husky to standardize the format of commit messages, which makes it easier to generate a CHANGELOG through scripts later.
Firstly, add the prepare scripts to the package.json file:
1 | { |
or
1 | npm set-script prepare "husky install" |
Configure the Husky hooks in the package.json file using the husky property.
1 | npx husky add .husky/pre-commit npm test && npm run foo |
Github Action
GitHub Actions allows to automate your entire development workflow, from code changes to production deployment, all within the GitHub environment. The following configuration is mainly to let Github Action help us run lint and test, and when we push a tag, it will help us publish the package to npm and deploy the latest documentation.
1 | name: dry |
Publish
Update the version number of the package, and push any new tags created to the remote repository.
1 | npm version patch/tag |
Retrieve the registry URL that is configured for the local npm installation.
1 | npm config get registry |
Log in to an npm registry with credentials which is necessary in order to publish packages to the registry.
1 | npm adduser |
Check whether the currently named package already exists.
1 | npm info @bk/sum@version |
Publish or delete a package.
1 | npm publish |
How to use
1 | npm i bk-sum |
1 | import { Sum } from '@bk/sum' |
References:
- https://dev.to/siddharthvenkatesh/component-library-setup-with-react-typescript-and-rollup-onj
- https://juejin.cn/post/6950557086916804645#heading-0
- https://juejin.cn/post/6934507948911788045
🔍 Check out my code in github.
📮 If find any errors, please feel free to discuss and correct them: biqingsue@[google email].com.