The Best Ways to Bundle React Modules Into Packages

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Building a Company Component Library course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Building a Company Component Library, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course The newline Guide to Building a Company Component Library

The build process for a library has many similarities with applications. We want to take a single entry point, which contains our components, and bundle them together into an output file. When bundling applications, the target environment is a web browser. Webpack is a popular module bundler that helps bundle a wide variety of assets into an output that browsers can understand. For libraries, the target environment is often another development environment, not a browser. This changes the type of assets we can produce and the tools required to build them. In this course, we will be using RULP JS as our module bundler. Originally, there was no open standard for importing and exporting JavaScript modules. Over time, there have been many formats developed which realize this, each with their own unique benefits. Some common formats include Common JS, which is the module system that Node.js has used historically. It's server-side-only, and you'll recognize it with the require and module.ex ports syntax. The next is ES modules, which is the official specification for a standard module system. This is able to work in both server and modern browser environments, and you'll recognize it with import and export syntax. One of the key things with ES modules is that it supports tree-shaking, and tree-shaking allows module bundlers like Webpack and RULP JS, to remove JavaScript that isn't required for your application to run. The other format is Universal Module Definition, or UMD, which allows many different module formats to interact with your library. This is able to work in a server and browser environment, but it's unable to be tree-shaken. When we build our library, we don't have to choose a single module format. We can provide several formats and reference them with unique entry points in our package.json. Some popular entry points include main, module, and browser. Modern bundlers are able to look at the entry points to determine which format to consume. In this course, we're only going to be providing the main and module entry points for Common JS and ES module distributions. Module bundling is important, but we still need to produce JavaScript in a format that browsers can understand. Depending on your minimum browser requirements, some modern JavaScript features may not be available. Babel is a tool that can help transpile our code into a format that older browsers can understand. Babel also allows other tools and libraries to create presets that improve our code. Babel preset React, for example, ensures that our JSX syntax is handled correctly. We will also be using the Style Components Babel plugin to improve our CSS and JS usages in production. We will be using Babel in combination with Rollup to bundle our application. In the upcoming lesson, we will be adding a similar build process to our component library.