Building React library using react-lib-scripts
Hi, today I’d like to share my latest open source project for building React library, react-lib-scripts.
It’s a CLI that will help starting development and bundling library, so you can focus on your solution, not the tooling. It’s inspired from react-scripts used by create-react-app which helps React application development.
The CLI itself is made using OCLIF by Heroku, and the bundler is made using Webpack 4. It can also bundle your library styles and images.
Overall, react-lib-scripts only has 2 commands:
- start (starting development flow. This will bundle and watch your library source)
- build (prepare production ready bundle for publishing)
Get started
You can clone the starter repo here:
https://github.com/antonybudianto/react-lib-starterThis is a starter that demonstrates react-lib-scripts usage and also a mono-repo setup using Lerna. You can find example-app and example-lib inside the packages folder.
You can use react-lib-scripts without react-lib-starter/Lerna since they only provide a nice base for development library
Let’s examine:
- example-app (plain create-react-app, uses example-lib)
- example-lib (our React library, uses react-lib-scripts)
Let’s make sure you’re on the root of the starter, and install dependencies:
npm install# for yarn user
yarn
One advantage using Lerna is that you can bootstrap and link your packages dependencies with only one command. Let’s bootstrap it!
npm run bootstrap# for yarn user
yarn bootstrap
It actually runs lerna bootstrap command, and for convenience, I put it as npm script on the starter. This command will bootstrap all your packages (including npm-installing, and linking), now you don’t have to do it manually for each packages.
Development flow
After installing stuff, we’re ready for the development flow.
You can change directory to example-lib and run:
npm start# for yarn user
yarn start
Your library is now ready to be consumed by example-app!
Get Antony Budianto’s stories in your inbox
Join Medium for free to get updates from this writer.
npm start script is an alias to react-lib-scripts start
Important: You need to provide NODE_ENV environment variable before start or build command.
It’s already provided on react-lib-starter via better-npm-run
Now, you can change directory to example-app and run:
npm start# for yarn user
yarn start
You can start library development now. While changing example-lib code, the changes will be updated on example-app too.
Production flow
Okay, so your library is done and ready for publishing.
You can build your production-ready bundle by running:
npm run buildnpm run build script is an alias to react-lib-scripts build
Now your bundle is ready on lib folder.
Next, you must set the following fields on your example-lib’s package.json (only once):
"main": "lib/index.js",
"files": [
"lib",
...
],Also, bump your “version” field too every pre-publish.
Okay, let’s publish it.
npm publishNow your library is live!
react-lib-starter also has
npm run release(alias tolerna publish) which can handle dependencies version update, version bump chooser, git-tagging, and npm-publishing automatically.
FAQ
- Why no test command? Because I thought Jest is already simple enough to use, there’s almost nothing to be abstracted away. Unlike before Jest comes in, it’s a trouble configuring test runner, framework, coverage and stuffs. Now it’s easy as running
jest:) - Rollup? For now, no, because there’s watch issue when using their Node API. I might revisit it after it’s solved.
- Can I add babel plugin/presets or postcss plugins? Yes! Check here
The react-lib-scripts project is still new, and needs improvements, so your feedbacks and contributions will be VERY helpful for the project.
I hope the story will be helpful for all of you. Please post a response for any feedbacks/ideas/questions, and kindly share this to your friends who might need it, and finally, claps on please! 👏👏👏
Thanks for reading!











