Saasify Founder & CEO
Publishing an open source React / Preact component or library to npm can be very rewarding, but getting started with the setup is still more daunting than it should be, especially compared to the relatively easy process of creating a React app via create-react-app or create-preact-app.
Because JS module formats and ES features are all over the place and are unlikely to be normalized anytime soon, npm modules must support both the lowest common denominator format (commonjs) as well as modern ES6 modules without relying on commonplace language features like JSX and class properties that most of the React community takes for granted. For this reason, it’s pretty difficult to take a component you’ve written for an app and publish it directly to npm.
The purpose of this article is to help React authors easily publish their own, high quality component modules.
There are some existing React library boilerplates, but none of them fulfilled the following goals which we set out to accomplish:
It’s particularly important for module authors to include a simple, self-contained example app alongside the module itself, as it serves two useful purposes:
Now that our goals are clearly defined, check out the boilerplate repo we’ll be starting from:
In order to use the boilerplate, we recommend following this walkthrough to start out. Don’t worry if these steps seems complicated, as it’s meant to be very verbose.
Let’s create an example npm module called
react-poop-emoji that exposes a single component,
PoopEmoji 💩. We'll assume an example github username of
github-haxor, where your github username will be used for specifying the repository in
package.json and resolving the example github pages deployment.
Now you’re ready to run a local version of rollup that will watch your
src/ component and automatically recompile it into
dist/ whenever you make changes.
We’ll also be running our
example/ create-react-app that's linked to the local version of your
Now, anytime you make a change to your component in
src/ or to the example application's
create-react-app will live-reload your local dev server so you can iterate on your component in real-time.
When you’re ready to push your component for the first time to github, make sure you’ve customized all your readme and metadata, then initialize your git repo normally:
When you’re ready to publish your module to npm, make sure your dependencies are up-to-date. Any npm module dependencies that you don’t want to be included in the published bundle should be marked as peer dependencies in
package.json and added to the externals array in your rollup config. Note that this boilerplate defaults to setting
prop-types as peer dependencies, which is probably what you want unless you really know what you’re doing.
And finally, we recommend deploying your example to github pages so your users can quickly play around with a live version of your library before installing it.
Deploying to github pages is straightforward. We create a production build of our example
create-react-app that showcases your library and then run
gh-pages to deploy the resulting bundle. This can be done with the command:
npm run deploy
Note that it’s important for your
example/package.json to have the correct
homepage property set, as
create-react-app uses this value as a prefix for resolving static asset URLs.
We recommend adding a link to the resulting github pages example to your readme.
For a deeper explanation, I recommend reading Rich Harris’ article Webpack and Rollup: the same but different. In short, the majority of the community now favors using Rollup for libraries and Webpack for apps. That being said, I believe you should stick with whatever you’re more comfortable with, as there really isn’t that big of a difference between the two as long as you’re comfortable using one versus the other.
create-react-app has become a standard that nearly every react developer is familiar with. Its internal design choices and tradeoffs represent a great deal of collaboration among many of the best developers in the React community.
We feel that by taking advantage of such a standard application framework, module authors can provide the simplest possible example app that both acts as a mature, local development vehicle while iterating on your module as well as being easily publishable as an example showcase.
See this CRA issue for more context around this movement.
I recommend that you piggyback off of create-react-app’s built-in test harness setup for testing your library. That being said, feel free to submit a PR and I’d be happy to add some separate, standardized testing to the boilerplate.
This is legitimately a great question. I’d like to thank myself for asking it 😝
Even though the JS community typically moves too fast for its own good, I believe these types of point-in-time best practice boilerplates still serve a useful learning purpose and jumping off point for both aspiring open source authors and veterans alike. I was personally frustrated that it was so difficult to find a quality, up-to-date starting point after publishing several open source react modules, so I wanted to take what I had learned and give back to the community that has taught me so much.
If you have any suggestions on how to improve this boilerplate or walkthrough, or if something’s out-of-date a month from now, feel free to raise an issue or, even better, submit a PR!
Rollup has excellent Typescript support. For details on how to integrate Typescript with this boilerplate, see this issue.
Here is an example react module created from this guide: react-background-slideshow, a sexy tiled background slideshow for React. It comes with an example create-react-app hosted on github pages and should give you a good idea of the type of module you’ll be able to create starting from this boilerplate.
I’d like to throw a shout out to js.coach, which is my favorite resource for finding high quality, open source React components. If you’re looking for further inspiration or are trying to find a solid solution without reinventing the wheel, chances are there will be some relevant modules listed on their index.
If you find this walkthrough or accompanying boilerplate useful, please ⭐️ ️️ the repo to help other developers find it.
Disclaimer: the author is a software engineer at Facebook, but this article discusses an independent open source project, and my views do not reflect the views of Facebook or its engineering staff as a whole. E.g., I write about this type of stuff for fun 😃
Create your free account to unlock your custom reading experience.