Esbuild bundle with react

Ramkumar Khubchandani
3 min readNov 25, 2023

--

Esbuild is an extremely fast JavaScript bundler and minifier that is designed to be a more modern alternative to existing tools like Webpack and Rollup. It is written in Golang and can be used to bundle JavaScript, TypeScript, and CSS code.

Key features of esbuild:

  • Extreme speed: Esbuild is significantly faster than other bundlers, especially for small to medium-sized projects. It can bundle a typical React application in under 2 seconds.
  • Small bundle size: Esbuild produces smaller bundle sizes compared to other bundlers, which can improve page load times.
  • Simple configuration: Esbuild has a minimal configuration compared to other bundlers, making it easier to set up and use.
  • Tree shaking: Esbuild supports tree shaking, which is a technique that removes unused code from the bundle.
  • Source maps: Esbuild generates source maps, which can be used to debug the bundled code.

Prerequisites:

  • Node.js installed on your system
  • An existing React project with a complex structure

Steps:

  1. Install esbuild:
npm install esbuild

2. Create an esbuild configuration file:

{
"entryPoints": ["src/index.js"],
"bundle": true,
"outfile": "dist/bundle.js",
"sourcemap": true,
"plugins": [
{
name: "react",
inputSpecifier: "react",
resolvePath: "./node_modules/react/dist/react.development.js"
},
{
name: "react-dom",
inputSpecifier: "react-dom",
resolvePath: "./node_modules/react-dom/dist/react-dom.development.js"
}
]
}

This configuration file tells esbuild to bundle the code from src/index.js and output it to a file named bundle.js. It also tells esbuild to use the react and react-dom plugins to resolve the React and ReactDOM imports.

3. Update the package.json file:

{
"scripts": {
"start": "esbuild src/index.js --bundle --outfile=dist/bundle.js --serve",
"build": "esbuild src/index.js --bundle --outfile=dist/bundle.js"
}
}

The start script will start a development server with live reloading. The build script will create a production-ready bundle file.

4. Run the development server:

npm start

This will open the React application in your default browser.

5. Build the production bundle:

npm build

This will create a bundle.js file in the dist directory. This file can be deployed to a production environment.

Additional Considerations for Complex Apps:

For complex React apps, you may need to add additional plugins or configuration options to esbuild. For example, you may need to add plugins to handle CSS preprocessing, code splitting, or minification. You may also need to adjust the entryPoints and outfile options to accommodate a more complex project structure.

Conclusion:

Using esbuild for complex React projects offers several advantages, including:

  • Faster build times: esbuild is still significantly faster than Webpack, even for large and complex projects.
  • Smaller bundle size: esbuild can produce smaller bundle sizes compared to Webpack, especially when using its advanced optimization features.
  • Simpler configuration: esbuild’s configuration is still relatively simple, even for complex projects, when compared to Webpack’s complex configuration.

If you’re looking for a fast, lightweight, and easy-to-use bundler for your complex React projects, esbuild is an excellent choice. With its growing popularity and community, esbuild is likely to become even more powerful and versatile in the future.

Comparison with Webpack

As you can see, the esbuild build process is again much simpler than the webpack build process. This is because esbuild is designed to be a fast and lightweight bundler, while webpack is a more feature-rich and complex tool.

Here is a table that summarizes the key differences between esbuild and webpack in the context of a React project:

Final Comparison Conclusion:

esbuild is a good choice for React projects that require fast build times and do not need the advanced features of webpack. Webpack is a good choice for React projects that require a lot of customization or that use a lot of third-party libraries.

Do you want to learn more like this ?

Follow me or message me on Linkedin.

--

--

Ramkumar Khubchandani
Ramkumar Khubchandani

Written by Ramkumar Khubchandani

Frontend Developer|Technical Content Writer|React|Angular|React-Native|Corporate Trainer|JavaScript|Trainer|Teacher| Mobile: 7709330265|ramkumarkhub@gmail.com

Responses (1)