react-external-lib-boilerplate
v0.1.6
Published
A boilerplate for developing standalone React components with Storybook and publishing them to npm.
Readme
React Standalone Component Boilerplate
A boilerplate for developing standalone React components with Storybook and publishing them to npm.
Features
- Rollup: Provides build with
esm,cjs,.d.ts. - Release-it: Github releases, npmjs publishing
- Storybook: Local development , deployment to Github Pages (optionally).
Out the box support:
- TypeScript: Pre-configured for seamless integration.
- Material-UI: Compatible with MUI v5/v6.
- SDLC: There is a set of github actions, aims to implement
gitflowmodel
Getting Started
Prequirements
NPM publish token
Follow these steps if you're planning to push to npmjs/other registry
Generate an npm token:
- Go to npmjs.com, log in, and navigate to
Access Tokens. - Create a new token of type
Classic, and choose theAutomationoption.
- Go to npmjs.com, log in, and navigate to
Test locally (⚠️ will override ~/.npmrc ⚠️):
# make a backup before testing ci token in local env # backing up # $ cp ~/.npmrc ~/.npmrc.bak $ npm config set //registry.npmjs.org/:_authToken $NPM_PUBLISH_TOKEN $ npm publish # restoring # $ mv ~/.npmrc.bak ~/.npmrcGitHub Actions setup:
Add the
NPM_PUBLISH_TOKENas a secret in your GitHub repository/organization if you want to push releases via GitHub Actions.
Storybook deploying to Github Pages
Go to
Repository Settings->Actions->General->Workflow permissions, enableRead and write permissionsandSave.Go to
Repository Settings->Pages->Build and deployment, and set theSourcetoGitHub Actions.
PR Labels
- Create next labels:
patchminormajor - When creating PR select release type, it will update version as requested (
patchis default if not selected)
- Create next labels:
Installation
To create a new project from this template:
- Click on the
Use this templatebutton in GitHub. - Clone the newly created repository:
git clone [email protected]:yourusername/your-custom-lib.git cd your-custom-lib yarn install yarn storybook yarn build yarn release # make sure you have a valid NPM_PUBLISH_TOKEN # ci env simulate # yarn release --ci --increment=patch
Material UI
For creating custom MUI components, install the needed dependencies
$ yarn add @mui/material @emotion/react @emotion/styled @mui/icons-material @fontsource/robotoStorybook MUI setup (typescript, theme swithcing, etc):
Development
- NO SDLC/GITHUB ACTIONS
- don't create
gitflowbranches likefeature/*,hotfix/*,release/*, so no actions would be triggered yarn build- release-it update:
yarn release(for non-interactive:yarn release --ci --increment=patch)
- manual update:
yarn version --patchyarn publish
- don't create
- SDLC:
gitflow:Push to
feature/*,hotfix/*- triggers
.github/workflows/tests.yml - TODO: create prerelease if there is an open PR from this branch (so the prerelease can be installed via npm, may be helpful for integration tests/qa).
- triggers
Make PR to
development- Set label:
patch,minor,major(necessary for calculating next version) - triggers
.github/workflows/semver-check.ymlwhen saved (checks if label above was set)
- Set label:
When
merged:- triggers
.github/workflows/release.yml- new
tagcreated - new
github releasecreated - new build pushed to
npmjs - new
commitpushed todevelopment(⚠️ fails on protected branches )
- new
- triggers
TODO: support dev/rc tags
Available Scripts
Here are the main scripts you can use:
yarn storybook
yarn test
Runs the test suite using react-scripts.
yarn build
Builds the component library using Rollup, outputting both esm and cjs formats.
yarn release
Prepares and publishes a new version to npm.
Configuration
TypeScript (tsconfig.json)
The provided tsconfig.json contains several options related to generated .d.ts - required by rollup-plugin-dts
"declaration": true"declarationDir": "build/dts""emitDeclarationOnly": true
Known Issues
- Release-it
- NPM Classic Token bypasses 2FA: The token used for automated publishing bypasses two-factor authentication.
- GitHub Actions fail on protected branches: If
git.commit === truein therelease-itconfiguration, therelease.ymlaction will fail on protected branches. To resolve this, disable branch protection for thedefaultbranch. TODO: refactor flow without commiting todefaultbranch
Inspiration
This project was inspired by:
References
- TypeScript library tips: Rollup your types!
- Creating a React Component Library with TypeScript, Storybook & Rollup
- How to Create a React Multi-package UI Library
