<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Crispin Kalarickal's blog]]></title><description><![CDATA[Crispin Kalarickal's blog]]></description><link>https://captain-kentucky.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 04:31:15 GMT</lastBuildDate><atom:link href="https://captain-kentucky.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Lightning-Fast React Setup 2024: How to Build a React App with esbuild in Minutes]]></title><description><![CDATA[Introduction
In the fast-paced world of web development, efficiency and speed are crucial. React, one of the most popular JavaScript libraries for building user interfaces, has empowered developers to create dynamic and responsive web applications. H...]]></description><link>https://captain-kentucky.hashnode.dev/lightning-fast-react-setup-2024-how-to-build-a-react-app-with-esbuild-in-minutes</link><guid isPermaLink="true">https://captain-kentucky.hashnode.dev/lightning-fast-react-setup-2024-how-to-build-a-react-app-with-esbuild-in-minutes</guid><category><![CDATA[React]]></category><category><![CDATA[esbuild]]></category><category><![CDATA[JSX]]></category><category><![CDATA[tsx]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[CSS]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[webpack]]></category><category><![CDATA[ParcelJS]]></category><dc:creator><![CDATA[Crispin Kalarickal]]></dc:creator><pubDate>Fri, 30 Aug 2024 22:35:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1725057040047/92d55a46-eacd-4ba4-be61-7b07181983f2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction</h3>
<p>In the fast-paced world of web development, efficiency and speed are crucial. React, one of the most popular JavaScript libraries for building user interfaces, has empowered developers to create dynamic and responsive web applications. However, as projects grow in complexity, the need for a robust and lightning-fast build process becomes increasingly important.</p>
<p>Enter esbuild, a modern JavaScript bundler and minifier known for its exceptional speed and simplicity. Unlike traditional bundlers like Webpack, esbuild is designed to handle large projects with ease, offering near-instant build times and minimal configuration. As we step into 2024, esbuild is gaining traction as a go-to tool for developers who want to streamline their React development process.</p>
<p>In this guide, I’ll walk you through the steps to set up a React project using esbuild. Whether you’re a seasoned developer looking to optimize your workflow or a beginner eager to learn, this guide will equip you with the knowledge to get your React app up and running in minutes. By the end, you’ll have a high-performance, production-ready React application built with the power of esbuild.</p>
<h3 id="heading-what-is-esbuild">What is esbuild?</h3>
<p>esbuild is a modern JavaScript bundler and minifier that has quickly become a favorite among developers due to its incredible speed and simplicity. Written in Go, esbuild is designed to be fast—blazingly fast. It can handle complex bundling tasks in a fraction of the time it takes for other tools like Webpack or Parcel.</p>
<p>One of the standout features of esbuild is its zero-config approach. Unlike traditional bundlers that often require extensive configuration, esbuild works out-of-the-box for most common use cases. This simplicity makes it an ideal choice for developers who want to get their projects up and running quickly without getting bogged down in configuration details.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1725056043518/fb98fd07-9dba-4974-a0e7-b15f9512678a.png" alt class="image--center mx-auto" /></p>
<p>Here are some key features that set esbuild apart:</p>
<ul>
<li><p><strong>Lightning-Fast Builds:</strong> esbuild is designed to perform bundling tasks at unprecedented speeds, making it ideal for large projects or frequent rebuilds during development.</p>
</li>
<li><p><strong>Minimal Configuration:</strong> With sensible defaults and an intuitive API, esbuild allows you to start building with minimal setup.</p>
</li>
<li><p><strong>Tree Shaking:</strong> esbuild automatically removes unused code from your final bundle, helping you deliver optimized and lightweight applications.</p>
</li>
<li><p><strong>ESM and CommonJS Support:</strong> esbuild fully supports modern ES modules as well as the older CommonJS format, ensuring compatibility with a wide range of libraries and tools.</p>
</li>
<li><p><strong>Integrated TypeScript Support:</strong> If your project uses TypeScript, esbuild can compile your TypeScript files directly, without the need for additional plugins or tools.</p>
</li>
</ul>
<p>In 2024, as developers seek faster and more efficient ways to build web applications, esbuild is emerging as a game-changer. Whether you're working on a small personal project or a large enterprise application, esbuild offers the performance and simplicity needed to keep your development process smooth and efficient.</p>
<h3 id="heading-setting-up-your-development-environment">Setting Up Your Development Environment</h3>
<p>Before you can start building your React application with esbuild, you’ll need to ensure that your development environment is properly set up. In this section, we’ll guide you through the essential steps to get everything ready, from installing the necessary tools to initializing your project.</p>
<ol>
<li><p>Prerequisites</p>
<p> <strong>Node.js and npm/yarn:</strong> Node.js is a JavaScript runtime that allows you to run JavaScript code on your machine. npm (Node Package Manager) or yarn is used to manage packages and dependencies for your project.</p>
</li>
<li><p>Code Editor</p>
<p> While you can use any text editor, <strong>Visual Studio Code</strong> is highly recommended due to its powerful features and extensive ecosystem of extensions.</p>
</li>
</ol>
<h3 id="heading-creating-a-basic-react-project">Creating a Basic React Project</h3>
<p>Let's set up a basic React project:</p>
<ol>
<li><p>Installing Locally</p>
<p> First, navigate to the directory where you want to create your project. Then, run the following command to create a new directory and navigate into it:</p>
<pre><code class="lang-bash"> mkdir esbuild-react-app
 <span class="hljs-built_in">cd</span> esbuild-react-app
</code></pre>
</li>
<li><p>Initialize a new Node.js project with npm:</p>
<pre><code class="lang-bash"> npm init -y
</code></pre>
</li>
<li><p>Now, install all the necessary dependencies and devDependencies:</p>
<pre><code class="lang-bash"> npm i dotenv esbuild react react-dom typescript
</code></pre>
<pre><code class="lang-bash"> npm i -D @types/react @types/react-dom esbuild-css-modules-plugin esbuild-jest glob prettier shelljs
</code></pre>
</li>
<li><p>Set Up the Project Structure:</p>
<ul>
<li><p>Create the following folder structure:</p>
<pre><code class="lang-plaintext">  esbuild-react-app/
  ├── esbuild/
  |   └── dev.js
  |   └── prod.js
  ├── public/
  │   └── index.html
  |   └── manifest.json
  |   └── favicon.png
  ├── src/
  |   ├── contexts
  |   |   └── HMRContext.tsx
  │   ├── index.tsx
  │   └── App.tsx
  └── package.json
</code></pre>
</li>
<li><p><strong>index.html</strong>: This file will serve as the entry point for your app. Add the following basic HTML structure:</p>
<pre><code class="lang-xml">  <span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>esbuild React App<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"description"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"manifest"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"./manifest.json"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"icon"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"image/x-icon"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"./favicon.png"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"apple-touch-icon"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/apple-touch-icon.png"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"./build/bundle.css"</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"preload"</span> <span class="hljs-attr">as</span>=<span class="hljs-string">"style"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./build/bundle.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">noscript</span>&gt;</span>You need to enable JavaScript to run this app.<span class="hljs-tag">&lt;/<span class="hljs-name">noscript</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"root"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
</li>
<li><p><strong>index.jsx:</strong> In the <code>src</code> folder, create this file to serve as the entry point for your React application</p>
<pre><code class="lang-typescript">  <span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
  <span class="hljs-keyword">import</span> { createRoot } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>;

  <span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'@/App'</span>;
  <span class="hljs-keyword">import</span> HMRProvider <span class="hljs-keyword">from</span> <span class="hljs-string">'./contexts/HMRContext'</span>;

  <span class="hljs-keyword">const</span> root = createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>) <span class="hljs-keyword">as</span> HTMLElement)

  root.render(
    &lt;React.StrictMode&gt;
      &lt;HMRProvider&gt;
        &lt;App /&gt;
      &lt;/HMRProvider&gt;
    &lt;/React.StrictMode&gt;
  )
</code></pre>
</li>
<li><p><strong>App.jsx:</strong> Also in the <code>src</code> folder, create a simple React component to display</p>
<pre><code class="lang-typescript">  <span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

  <span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">return</span> (
      &lt;div&gt;
        &lt;p&gt;Hello World&lt;/p&gt;
      &lt;/div&gt;
    );
  };

  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
</li>
</ul>
</li>
<li><p>Hot Module Replacement (HMR) and Live Reloading</p>
<p> In summary, <code>HMRProvider</code> sets up a real-time file change detection mechanism that automatically reloads the application during development when changes are detected, enhancing the development experience by reflecting updates immediately.</p>
<pre><code class="lang-typescript"> <span class="hljs-keyword">import</span> React, { createContext, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

 <span class="hljs-keyword">type</span> Props = {
     children?: React.ReactNode,
 }

 <span class="hljs-keyword">const</span> HMRContext = createContext(<span class="hljs-literal">null</span>);

 <span class="hljs-keyword">const</span> HMRProvider = <span class="hljs-function">(<span class="hljs-params">{ children }: Props</span>) =&gt;</span> {
     useEffect(<span class="hljs-function">() =&gt;</span> {
         <span class="hljs-keyword">if</span> (process.env.NODE_ENV !== <span class="hljs-string">"production"</span>) {
             <span class="hljs-keyword">const</span> eventSource = <span class="hljs-keyword">new</span> EventSource(<span class="hljs-string">'/esbuild'</span>);
             eventSource.addEventListener(<span class="hljs-string">'change'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
                 <span class="hljs-keyword">const</span> updatedFiles = <span class="hljs-built_in">JSON</span>.parse(event.data);

                 <span class="hljs-keyword">if</span> (updatedFiles.updated.length &gt; <span class="hljs-number">0</span>) {
                     location.reload();
                 }
             });

             eventSource.onerror = <span class="hljs-function">(<span class="hljs-params">error</span>) =&gt;</span> {
                 <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'EventSource error:'</span>, error);
             };

             <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
                 eventSource.close();
             };
         }
     }, []);

     <span class="hljs-keyword">return</span> (
         &lt;HMRContext.Provider value={<span class="hljs-literal">null</span>}&gt;
             {children}
         &lt;/HMRContext.Provider&gt;
     )
 }

 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> HMRProvider
</code></pre>
</li>
<li><p>Creating the esbuild Configuration</p>
<p> Finally, set up esbuild to bundle your React project:</p>
<ul>
<li><p><strong>dev.js</strong></p>
<p>  This configuration sets up a robust development environment with esbuild for a React application. It handles TypeScript, CSS Modules, and various assets while providing features like live reloading, sourcemaps, and automatic browser opening, all aimed at improving the development experience.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">import</span> { exec } <span class="hljs-keyword">from</span> <span class="hljs-string">'child_process'</span>;
  <span class="hljs-keyword">import</span> dotenv <span class="hljs-keyword">from</span> <span class="hljs-string">'dotenv'</span>;
  <span class="hljs-keyword">import</span> esbuild <span class="hljs-keyword">from</span> <span class="hljs-string">'esbuild'</span>;
  <span class="hljs-keyword">import</span> CssModulesPlugin <span class="hljs-keyword">from</span> <span class="hljs-string">'esbuild-css-modules-plugin'</span>;
  <span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;

  dotenv.config();

  <span class="hljs-keyword">const</span> config = {
    <span class="hljs-attr">logLevel</span>: <span class="hljs-string">'info'</span>,
    <span class="hljs-attr">entryPoints</span>: [<span class="hljs-string">'src/index.tsx'</span>],
    <span class="hljs-attr">outfile</span>: <span class="hljs-string">'public/build/bundle.js'</span>,
    <span class="hljs-attr">bundle</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">define</span>: {
      <span class="hljs-attr">NODE_ENV</span>: <span class="hljs-string">'development'</span>,
    },
    <span class="hljs-attr">minify</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">sourcemap</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">plugins</span>: [
      CssModulesPlugin({
        <span class="hljs-attr">force</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">emitDeclarationFile</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">localsConvention</span>: <span class="hljs-string">'camelCaseOnly'</span>,
        <span class="hljs-attr">namedExports</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">inject</span>: <span class="hljs-literal">false</span>,
      }),
    ],
    <span class="hljs-attr">loader</span>: {
      <span class="hljs-string">'.eot'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.woff'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.woff2'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.ttf'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.svg'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.png'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.jpg'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.gif'</span>: <span class="hljs-string">'dataurl'</span>,
    },
  };

  esbuild
    .context(config)
    .then(<span class="hljs-keyword">async</span> (ctx) =&gt; {
      <span class="hljs-keyword">await</span> ctx.watch();
      <span class="hljs-keyword">await</span> ctx.serve({
        <span class="hljs-attr">servedir</span>: <span class="hljs-string">'public'</span>,
        <span class="hljs-attr">port</span>: <span class="hljs-number">3000</span>,
        <span class="hljs-attr">onRequest</span>: <span class="hljs-function">(<span class="hljs-params">{ remoteAddress, method, path, status, timeInMS }</span>) =&gt;</span> {
          <span class="hljs-built_in">console</span>.info(remoteAddress, status, <span class="hljs-string">`"<span class="hljs-subst">${method}</span> <span class="hljs-subst">${path}</span>" [<span class="hljs-subst">${timeInMS}</span>ms]`</span>);
        },
      });
    })
    .catch(<span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
      <span class="hljs-built_in">console</span>.error(e);
      process.exit(<span class="hljs-number">1</span>);
    });

  <span class="hljs-comment">// Open the browser after successful build</span>
  <span class="hljs-keyword">const</span> openBrowser = <span class="hljs-function">(<span class="hljs-params">url</span>) =&gt;</span> {
    <span class="hljs-keyword">switch</span> (process.platform) {
      <span class="hljs-keyword">case</span> <span class="hljs-string">'win32'</span>:
        exec(<span class="hljs-string">`start <span class="hljs-subst">${url}</span>`</span>);
        <span class="hljs-keyword">break</span>;
      <span class="hljs-keyword">case</span> <span class="hljs-string">'darwin'</span>:
        exec(<span class="hljs-string">`open <span class="hljs-subst">${url}</span>`</span>);
        <span class="hljs-keyword">break</span>;
      <span class="hljs-keyword">case</span> <span class="hljs-string">'linux'</span>:
        exec(<span class="hljs-string">`xdg-open <span class="hljs-subst">${url}</span>`</span>);
        <span class="hljs-keyword">break</span>;
      <span class="hljs-keyword">default</span>:
        <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Unsupported platform'</span>);
    }
  };

  openBrowser(<span class="hljs-string">'http://localhost:3000'</span>);
</code></pre>
</li>
<li><p><strong>prod.js</strong></p>
<p>  This script provides a comprehensive approach to building and preparing a production-ready React application with esbuild, including handling assets, minification, and ensuring that the HTML file correctly references the output files.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">import</span> dotenv <span class="hljs-keyword">from</span> <span class="hljs-string">'dotenv'</span>;
  <span class="hljs-keyword">import</span> esbuild <span class="hljs-keyword">from</span> <span class="hljs-string">'esbuild'</span>;
  <span class="hljs-keyword">import</span> CssModulesPlugin <span class="hljs-keyword">from</span> <span class="hljs-string">'esbuild-css-modules-plugin'</span>;
  <span class="hljs-keyword">import</span> { glob } <span class="hljs-keyword">from</span> <span class="hljs-string">'glob'</span>;
  <span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
  <span class="hljs-keyword">import</span> sh <span class="hljs-keyword">from</span> <span class="hljs-string">'shelljs'</span>;

  dotenv.config();

  <span class="hljs-keyword">const</span> directory = <span class="hljs-string">'build'</span>;

  <span class="hljs-keyword">if</span> (sh.test(<span class="hljs-string">'-e'</span>, directory)) {
    sh.rm(<span class="hljs-string">'-rf'</span>, directory);
  }
  sh.mkdir(directory);
  sh.cp(<span class="hljs-string">'public/index.html'</span>, directory);
  sh.cp(<span class="hljs-string">'public/manifest.json'</span>, directory);
  sh.cp(<span class="hljs-string">'public/favicon.png'</span>, directory);

  <span class="hljs-keyword">const</span> config = {
    <span class="hljs-attr">logLevel</span>: <span class="hljs-string">'info'</span>,
    <span class="hljs-attr">entryPoints</span>: {
      <span class="hljs-attr">main</span>: <span class="hljs-string">'src/index.tsx'</span>,
    },
    <span class="hljs-attr">platform</span>: <span class="hljs-string">'node'</span>,
    <span class="hljs-attr">bundle</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">define</span>: {
      <span class="hljs-attr">NODE_ENV</span>: <span class="hljs-string">'production'</span>,
    },
    <span class="hljs-attr">minify</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">sourcemap</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">plugins</span>: [CssModulesPlugin()],
    <span class="hljs-attr">loader</span>: {
      <span class="hljs-string">'.eot'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.woff'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.woff2'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.ttf'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.svg'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.png'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.jpg'</span>: <span class="hljs-string">'dataurl'</span>,
      <span class="hljs-string">'.gif'</span>: <span class="hljs-string">'dataurl'</span>,
    },
    <span class="hljs-attr">outdir</span>: directory,
    <span class="hljs-attr">entryNames</span>: <span class="hljs-string">'js/[name]-[hash]'</span>,
    <span class="hljs-attr">assetNames</span>: <span class="hljs-string">'static/[name]-[hash]'</span>,
    <span class="hljs-attr">outExtension</span>: {
      <span class="hljs-string">'.js'</span>: <span class="hljs-string">'.mjs'</span>,
    },
    <span class="hljs-attr">pure</span>: [<span class="hljs-string">'console.log'</span>],
    <span class="hljs-attr">format</span>: <span class="hljs-string">'esm'</span>,
    <span class="hljs-attr">splitting</span>: <span class="hljs-literal">true</span>,
  };

  <span class="hljs-keyword">const</span> run = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">await</span> esbuild.build(config).catch(<span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
      <span class="hljs-built_in">console</span>.error(e);
      process.exit(<span class="hljs-number">1</span>);
    });

    <span class="hljs-keyword">const</span> jsFile = <span class="hljs-keyword">await</span> glob(<span class="hljs-string">`<span class="hljs-subst">${directory}</span>/js/*.?(m)js`</span>, { <span class="hljs-attr">posix</span>: <span class="hljs-literal">true</span> });
    <span class="hljs-keyword">const</span> cssFile = <span class="hljs-keyword">await</span> glob(<span class="hljs-string">`<span class="hljs-subst">${directory}</span>/js/*.css`</span>, { <span class="hljs-attr">posix</span>: <span class="hljs-literal">true</span> });

    <span class="hljs-keyword">if</span> (jsFile.length) {
      jsFile.forEach(<span class="hljs-function">(<span class="hljs-params">js</span>) =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(js);
        sh.sed(<span class="hljs-string">'-i'</span>, <span class="hljs-string">'\./build/bundle\.js'</span>, js.replace(directory, <span class="hljs-string">'.'</span>), <span class="hljs-string">`<span class="hljs-subst">${directory}</span>/index.html`</span>);
      });
    }

    <span class="hljs-keyword">if</span> (cssFile.length) {
      cssFile.forEach(<span class="hljs-function">(<span class="hljs-params">css</span>) =&gt;</span> {
        sh.sed(<span class="hljs-string">'-i'</span>, <span class="hljs-string">'\./build/bundle\.css'</span>, css.replace(directory, <span class="hljs-string">'.'</span>), <span class="hljs-string">`<span class="hljs-subst">${directory}</span>/index.html`</span>);
      });
    }
  };

  run();
</code></pre>
</li>
</ul>
</li>
</ol>
<h3 id="heading-description-of-the-build-and-development-configuration-scripts">Description of the Build and Development Configuration Scripts</h3>
<p>These scripts set up and manage the development and production build processes for a React application using esbuild. They provide a comprehensive solution for both local development and preparing a production-ready build. Here’s a detailed breakdown:</p>
<ol>
<li><strong>Development Setup</strong></li>
</ol>
<ul>
<li><p><strong>Environment Configuration:</strong></p>
<ul>
<li><code>dotenv.config()</code> is used to load environment variables from a <code>.env</code> file, allowing for environment-specific settings.</li>
</ul>
</li>
<li><p><strong>esbuild Configuration:</strong></p>
<ul>
<li><p><code>entryPoints</code>: Specifies the entry point for the application as <code>src/index.tsx</code>, indicating where esbuild should start bundling.</p>
</li>
<li><p><code>outfile</code>: Defines the output file for the bundled JavaScript code, located in <code>public/build/bundle.js</code>.</p>
</li>
<li><p><code>bundle</code>: Bundles all dependencies into a single output file.</p>
</li>
<li><p><code>define</code>: Sets <code>NODE_ENV</code> to <code>'development'</code>, enabling development-specific features.</p>
</li>
<li><p><code>minify</code>: Disables minification for easier debugging.</p>
</li>
<li><p><code>sourcemap</code>: Enables source maps to map compiled code back to the original source for debugging purposes.</p>
</li>
<li><p><code>plugins</code>: Uses the <code>CssModulesPlugin</code> to handle CSS Modules, with various configuration options for CSS processing.</p>
</li>
<li><p><code>loader</code>: Configures esbuild to handle various asset types (fonts, images, etc.) by encoding them as data URLs.</p>
</li>
</ul>
</li>
<li><p><strong>Development Server and Live Reloading:</strong></p>
<ul>
<li><p>The script creates an esbuild context for efficient watch mode and incremental builds.</p>
</li>
<li><p>A development server is started, serving content from the <code>public</code> directory on port 3000.</p>
</li>
<li><p><code>onRequest</code>: Logs HTTP requests, showing details like IP address, request method, path, status code, and response time.</p>
</li>
<li><p><strong>Automatic Browser Opening:</strong> After starting the server, the script opens the application in the default web browser using platform-specific commands.</p>
</li>
</ul>
</li>
</ul>
<ol start="2">
<li><strong>Production Build</strong></li>
</ol>
<ul>
<li><p><strong>Directory Management:</strong></p>
<ul>
<li><p>Checks if a <code>build</code> directory exists, removes it if present, and creates a new <code>build</code> directory.</p>
</li>
<li><p>Copies static assets (<code>index.html</code>, <code>manifest.json</code>, <code>favicon.png</code>) to the <code>build</code> directory.</p>
</li>
</ul>
</li>
<li><p><strong>esbuild Configuration:</strong></p>
<ul>
<li><p><code>entryPoints</code>: Specifies <code>src/index.tsx</code> as the entry point for the production build.</p>
</li>
<li><p><code>platform</code>: Set to <code>'node'</code>, though typically <code>'browser'</code> would be used for client-side React applications.</p>
</li>
<li><p><code>bundle</code>: Bundles all dependencies into the output files.</p>
</li>
<li><p><code>define</code>: Sets <code>NODE_ENV</code> to <code>'production'</code>, enabling production-specific optimizations.</p>
</li>
<li><p><code>minify</code>: Enables minification to reduce file size and improve performance.</p>
</li>
<li><p><code>sourcemap</code>: Disables sourcemaps to avoid exposing source code in the production build.</p>
</li>
<li><p><code>plugins</code>: Uses <code>CssModulesPlugin</code> for handling CSS Modules with default settings.</p>
</li>
<li><p><code>loader</code>: Handles various asset types (fonts, images, etc.) as data URLs.</p>
</li>
<li><p><code>outdir</code>: Specifies the output directory as <code>build</code>.</p>
</li>
<li><p><code>entryNames</code> and <code>assetNames</code>: Defines naming conventions for output files with hashed filenames for cache busting.</p>
</li>
<li><p><code>outExtension</code>: Changes JavaScript file extensions from <code>.js</code> to <code>.mjs</code> for ECMAScript modules.</p>
</li>
<li><p><code>pure</code>: Removes <code>console.log</code> statements from the code to clean up the production build.</p>
</li>
<li><p><code>format</code>: Sets the module format to <code>esm</code> (ECMAScript modules).</p>
</li>
<li><p><code>splitting</code>: Enables code splitting to optimize loading of JavaScript files.</p>
</li>
</ul>
</li>
<li><p><strong>Post-Build Adjustments:</strong></p>
<ul>
<li><p>After building, the script uses <code>glob</code> to locate the generated JavaScript and CSS files.</p>
</li>
<li><p>It updates references in <code>index.html</code> to point to the correct paths for the built assets.</p>
</li>
</ul>
</li>
<li><p><strong>Error Handling:</strong></p>
<ul>
<li>The script includes error handling to log and exit the process if the build fails.</li>
</ul>
</li>
</ul>
<p>These scripts together provide a robust setup for both development and production environments, ensuring an efficient workflow with esbuild, from local development with live reloading to optimizing and preparing the application for deployment.</p>
<h3 id="heading-running-and-building-the-project">Running and Building the Project</h3>
<p>Once you’ve set up your React project with esbuild, you need to know how to run your development server and build your project for production. This section will guide you through both processes.</p>
<ul>
<li><p><strong>package.json:</strong></p>
<p>  <code>scripts</code>:</p>
<ul>
<li><p><code>start</code>: Runs the development server by executing the <a target="_blank" href="http://esbuild.config.dev"><code>esbuild/dev</code></a><code>.js</code> script.</p>
</li>
<li><p><code>build</code>: Creates a production build by executing the <a target="_blank" href="http://esbuild.config.prod"><code>esbuild/prod</code></a><code>.js</code> script.</p>
</li>
</ul>
</li>
</ul>
<pre><code class="lang-json">    {
      <span class="hljs-attr">"name"</span>: <span class="hljs-string">"esbuild-react-app"</span>,
      <span class="hljs-attr">"version"</span>: <span class="hljs-string">"0.0.1"</span>,
      <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>,
      <span class="hljs-attr">"private"</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"module"</span>,
      <span class="hljs-attr">"dependencies"</span>: {
        <span class="hljs-attr">"dotenv"</span>: <span class="hljs-string">"^16.3.1"</span>,
        <span class="hljs-attr">"esbuild"</span>: <span class="hljs-string">"^0.23.1"</span>,
        <span class="hljs-attr">"react"</span>: <span class="hljs-string">"^18.2.0"</span>,
        <span class="hljs-attr">"react-dom"</span>: <span class="hljs-string">"^18.2.0"</span>,
        <span class="hljs-attr">"typescript"</span>: <span class="hljs-string">"^5.1.5"</span>
      },
      <span class="hljs-attr">"devDependencies"</span>: {
        <span class="hljs-attr">"@types/react"</span>: <span class="hljs-string">"^18.2.14"</span>,
        <span class="hljs-attr">"@types/react-dom"</span>: <span class="hljs-string">"^18.2.6"</span>,
        <span class="hljs-attr">"concurrently"</span>: <span class="hljs-string">"^8.2.0"</span>,
        <span class="hljs-attr">"esbuild-css-modules-plugin"</span>: <span class="hljs-string">"^3.1.2"</span>,
        <span class="hljs-attr">"esbuild-jest"</span>: <span class="hljs-string">"^0.5.0"</span>,
        <span class="hljs-attr">"glob"</span>: <span class="hljs-string">"^11.0.0"</span>,
        <span class="hljs-attr">"jest"</span>: <span class="hljs-string">"^29.5.0"</span>,
        <span class="hljs-attr">"prettier"</span>: <span class="hljs-string">"^2.8.8"</span>,
        <span class="hljs-attr">"shelljs"</span>: <span class="hljs-string">"^0.8.5"</span>
      },
      <span class="hljs-attr">"scripts"</span>: {
        <span class="hljs-attr">"start"</span>: <span class="hljs-string">"node esbuild/dev.js"</span>,
        <span class="hljs-attr">"build"</span>: <span class="hljs-string">"node esbuild/prod.js"</span>,
        <span class="hljs-attr">"fmt"</span>: <span class="hljs-string">"prettier --write"</span>
      }
    }
</code></pre>
<ul>
<li><p><strong>Running the Scripts</strong></p>
<p>  Start the Development Server:</p>
<pre><code class="lang-bash">  npm start
</code></pre>
<p>  Build for Production:</p>
<pre><code class="lang-bash">  npm run build
</code></pre>
</li>
</ul>
<p>The <code>package.json</code> file with these scripts allows you to easily manage and automate the development and build processes for your React application.</p>
<p>In this guide, we explored how to set up a React project using esbuild, from initial configuration to running and building the project. esbuild’s speed and efficiency make it an excellent choice for modern JavaScript applications, and integrating it into your React workflow can significantly enhance both development and production processes.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<ol>
<li><p><strong>Setting Up Your Project:</strong></p>
<ul>
<li><p>We manually created a React project and configured esbuild for both development and production environments.</p>
</li>
<li><p>We set up esbuild to handle TypeScript, CSS Modules, and various asset types, tailoring the configuration to our specific needs.</p>
</li>
</ul>
</li>
<li><p><strong>Development Workflow:</strong></p>
<ul>
<li><p>We configured a development server that supports live reloading, allowing us to see changes in real-time without manual refreshes.</p>
</li>
<li><p>We used esbuild’s watch mode to automatically rebuild our project as we worked on it, improving productivity.</p>
</li>
</ul>
</li>
<li><p><strong>Production Build:</strong></p>
<ul>
<li><p>We defined a production build process that optimizes our application by minifying code, generating hashed filenames, and preparing assets for deployment.</p>
</li>
<li><p>We included scripts in <code>package.json</code> to simplify running the development server and building the project.</p>
</li>
</ul>
</li>
</ol>
<p>By following these steps, you can efficiently develop and deploy a React application using esbuild. The setup not only ensures a smooth development experience but also prepares your project for a performant production deployment.</p>
<p>As you continue to build and refine your React application, esbuild will help you streamline your workflow and maintain high performance. Whether you’re working on a small project or scaling up to a larger application, leveraging esbuild’s capabilities can provide a significant boost to your development process.</p>
<p>Feel free to reach out if you have any questions or run into issues while setting up your project. <strong>Happy coding!</strong></p>
<h3 id="heading-additional-resources">Additional Resources</h3>
<ul>
<li><p>Esbuild - <a target="_blank" href="https://esbuild.github.io/">Link</a></p>
</li>
<li><p>React - <a target="_blank" href="https://react.dev/">Link</a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>