<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arpit Malik</title>
    <description>The latest articles on DEV Community by Arpit Malik (@arpitmalik832).</description>
    <link>https://dev.to/arpitmalik832</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1442519%2Fbfaa0d90-e434-470e-a2a3-31b5404525a6.png</url>
      <title>DEV Community: Arpit Malik</title>
      <link>https://dev.to/arpitmalik832</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arpitmalik832"/>
    <language>en</language>
    <item>
      <title>Title: How I Cracked My Coding Interviews with a Simple Pattern-Based DSA Approach</title>
      <dc:creator>Arpit Malik</dc:creator>
      <pubDate>Wed, 13 Aug 2025 08:20:01 +0000</pubDate>
      <link>https://dev.to/arpitmalik832/title-how-i-cracked-my-coding-interviews-with-a-simple-pattern-based-dsa-approach-mn1</link>
      <guid>https://dev.to/arpitmalik832/title-how-i-cracked-my-coding-interviews-with-a-simple-pattern-based-dsa-approach-mn1</guid>
      <description>&lt;p&gt;(and How You Can Too)&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;You’ve probably seen this before:&lt;br&gt;
You open a DSA question, stare at it for 20 minutes, and then… Google the solution.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;The problem?&lt;br&gt;
You’re learning problems, not patterns.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;When I was preparing for my interviews, I realized something game-changing:&lt;br&gt;
Almost every coding problem falls under a repeatable pattern.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Instead of trying to “learn everything,” I focused on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mastering Time Complexity Basics&lt;/li&gt;
&lt;li&gt;Cracking problems with Kadane’s Algorithm&lt;/li&gt;
&lt;li&gt;Moving up to Two Pointers, Sliding Window, Binary Search on Answer&lt;/li&gt;
&lt;li&gt;Then tackling Graphs, Tries, and Competitive Programming problems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Why Patterns Work Better Than Random Practice&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You solve faster because you’ve seen the core idea before.&lt;/li&gt;
&lt;li&gt;You can adapt the pattern to multiple variations.&lt;/li&gt;
&lt;li&gt;You feel confident in interviews — even with new problems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;What I’ve Created for You&lt;/p&gt;

&lt;p&gt;I’ve put together my personal DSA Pattern Notes + 150 Interview Problems in a clean, structured format on Notion.&lt;/p&gt;

&lt;p&gt;This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step-by-step explanations&lt;/li&gt;
&lt;li&gt;Time &amp;amp; space complexity analysis&lt;/li&gt;
&lt;li&gt;Example problems from LeetCode, Codeforces, and InterviewBit&lt;/li&gt;
&lt;li&gt;My CP-ready templates in JavaScript, Java, and C++&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;How to Get It&lt;/p&gt;

&lt;p&gt;I’m making it available for a small one-time payment — because good preparation saves months of job hunting.&lt;br&gt;
You’ll get lifetime access + updates whenever I add new problems.&lt;/p&gt;

&lt;p&gt;🔗 Get the Complete DSA Patterns &amp;amp; Notes &lt;a href="https://www.notion.so/arpitmalik832/DSA-Master-Guide-Introduction-24ef925ab18d80858590d361f00f0864?source=copy_link" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;💬 If you’ve ever felt stuck in your DSA prep, drop a comment with your biggest struggle — I might make my next free guide about it.&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>interview</category>
      <category>careerdevelopment</category>
    </item>
    <item>
      <title>Creating a React and Typescript app with Webpack</title>
      <dc:creator>Arpit Malik</dc:creator>
      <pubDate>Tue, 23 Apr 2024 03:25:46 +0000</pubDate>
      <link>https://dev.to/arpitmalik832/creating-a-react-and-typescript-app-with-webpack-2nc7</link>
      <guid>https://dev.to/arpitmalik832/creating-a-react-and-typescript-app-with-webpack-2nc7</guid>
      <description>&lt;p&gt;&lt;em&gt;In a &lt;a href="https://dev.to/arpitmalik832/creating-a-react-app-with-webpack-602"&gt;previous article&lt;/a&gt;, I went over how you can setup a simple React app with just Webpack and Babel. But what if you also want to enable Typescript for your project?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This article goes over how to add Typescript support to a simple React + Webpack project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Install Typescript and type definitions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first step is to just &lt;a href="https://www.typescriptlang.org/download"&gt;install Typescript&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install typescript --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you'll be required to install the type definitions for all the libraries that you already use. Usually, these would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install @types/node @types/react @types/react-dom @types/jest --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Configure Webpack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For Webpack to be able to process Typescript files, we will first need to install a custom loader.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are several available, but we’ll use &lt;code&gt;ts-loader&lt;/code&gt; for our setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install ts-loader --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to tell Webpack to process TS files as well. For this, we can update the &lt;code&gt;webpack.config.js&lt;/code&gt; file to also support &lt;code&gt;ts&lt;/code&gt; and &lt;code&gt;tsx&lt;/code&gt; extensions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// webpack.config.js

{
  // ...,
  module: {
    rules: [
      // `js` and `jsx` files are parsed using `babel-loader`
      {
        test: /\.(js|jsx)$/, 
        exclude: /node_modules/,
        use: ["babel-loader"],
      },
      // `ts` and `tsx` files are parsed using `ts-loader`
      { 
        test: /\.(ts|tsx)$/, 
        exclude: /node_modules/,
        use: ["ts-loader"],
      }
    ],
  },
  resolve: {
    extensions: ["*", ".js", ".jsx", ".ts", ".tsx"],    
  },
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Configure Typescript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The Typescript compiler supports several options for how to treat the code, which are defined in a &lt;code&gt;tsconfig.json&lt;/code&gt; file in the root of the project.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s create one for this project as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// tsconfig.json

{
  "compilerOptions": {
    "jsx": "react",
    "noImplicitAny": true,
    "module": "ES6",
    "target": "ES5",
    "outDir": "./build/",
    "preserveConstEnums": true,
    "removeComments": true,
    "sourceMap": true
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the purpose of this tutorial, I have just added the minimal settings that are required for the React + TS + Webpack integration, but you can learn more about all the options available in the &lt;a href="https://www.typescriptlang.org/docs/handbook/tsconfig-json.html"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Start using Typescript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you already have some JS files in your project, now is a good time to change their extensions from js to ts and from jsx to tsx!&lt;/p&gt;

&lt;p&gt;Then, restart the app and you should see everything still working ✨&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thats it!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Creating a React app with Webpack</title>
      <dc:creator>Arpit Malik</dc:creator>
      <pubDate>Mon, 22 Apr 2024 18:46:40 +0000</pubDate>
      <link>https://dev.to/arpitmalik832/creating-a-react-app-with-webpack-602</link>
      <guid>https://dev.to/arpitmalik832/creating-a-react-app-with-webpack-602</guid>
      <description>&lt;p&gt;&lt;em&gt;Simple step by step walk through of setting up a React app with Webpack&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you want to get started with React but don’t want all the bloat of &lt;code&gt;create-react-app&lt;/code&gt; or some other boilerplate. Here's a step by step walk through of how to set to setup React with just Webpack!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What the project will look like when it’s done&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This guide will follow the conventions already established by &lt;code&gt;create-react-app&lt;/code&gt; - e.g. build folder is called &lt;code&gt;build&lt;/code&gt;, static assets are under &lt;code&gt;public&lt;/code&gt;, etc.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At the end, this is the folder structure we’ll have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- build_utils
  - config
    - commonPaths.js
    - env.js
- public
  - index.html
- src
  - App.jsx
  - index.js
.babelrc
package.json
webpack.config.js

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ll go step by step and check that everything works after every stage:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Basic scaffolding: create project folder and serve plain HTML&lt;/li&gt;
&lt;li&gt;Add Webpack and bundle a simple JS file&lt;/li&gt;
&lt;li&gt;Add Babel for ES6 support&lt;/li&gt;
&lt;li&gt;Add React&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Without further ado, let’s get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Base scaffolding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;First step is to create the project folder and add a plain &lt;code&gt;html&lt;/code&gt; file.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To create the project and initialize the &lt;code&gt;package.json&lt;/code&gt;, run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
mkdir react-webpack
cd react-webpack
npm init -y

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, add the main index &lt;code&gt;html&lt;/code&gt; file - it usually sits in the &lt;code&gt;public&lt;/code&gt; directory. So, let's do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
mkdir public
touch public/index.html

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Tip: You can open the project in VSCode by typing &lt;code&gt;code .&lt;/code&gt; in the project folder and manually create the &lt;code&gt;public&lt;/code&gt; folder from there&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;index.html&lt;/code&gt; will just contain the base &lt;code&gt;HTML5&lt;/code&gt; boilerplate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&amp;gt;
    &amp;lt;meta
    name="viewport"
    content="width=device-width,initial-scale=1,minimum-scale=1,user-scalable=no"
    /&amp;gt;
    &amp;lt;meta
    httpEquiv="Cache-Control"
    content="no-cache, no-store, must-revalidate"
    /&amp;gt;
    &amp;lt;title&amp;gt;React + Webpack&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Hello React + Webpack!&amp;lt;/h1&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Tip: In VSCode, if you type &lt;code&gt;html:5&lt;/code&gt; and hit tab, VSCode will create the &lt;code&gt;index.html&lt;/code&gt; contents for you!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let’s check it by just serving the HTML we just created with &lt;code&gt;npm serve&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npx serve public

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it does! If you navigate to &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt;, you should see the html we just added.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Tip: If &lt;code&gt;npx serve public&lt;/code&gt; fails for you with &lt;code&gt;Must use import to load ES Module error&lt;/code&gt;, check your &lt;code&gt;node&lt;/code&gt; version with the help of &lt;code&gt;node -v&lt;/code&gt; and make sure you're using at least Node 16 (latest LTS).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Adding Webpack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For this section, it’s best to just follow the latest official Webpack docs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, install Webpack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install webpack webpack-cli --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's create a simple JS file that we can configure Webpack to bundle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
mkdir src
touch src/index.js

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can just create a div with a hello message and add it to the document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const helloDiv = document.createElement("div");
helloDiv.innerHTML = "Hello from Javascript!";
document.body.append(helloDiv);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we need to configure Webpack by creating a &lt;code&gt;webpack.config.js&lt;/code&gt; file in the root of the project and &lt;code&gt;commonPaths.js&lt;/code&gt; in &lt;code&gt;build_utils/config&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// webpack.config.js

const path = require("path");

const package = require("./package.json");
const commonPaths = require("./build_utils/config/commonPaths");

const isDebug = !process.argv.includes("release");

module.exports = {
  entry: commonPaths.entryPath,
  output: {
    uniqueName: package.name,
    publicPath: "/",
    path: commonPaths.outputPath,
    filename: `${package.version}/js/[name].[chunkhash:8].js`,
    chunkFilename: `${package.version}/js/[name].[chunkhash:8].js`,
    assetModuleFilename: isDebug
      ? `images/[path][name].[contenthash:8][ext]`
      : `images/[path][contenthash:8][ext]`,
    crossOriginLoading: "anonymous",
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// commonPaths.js

const path = require("path");

const PROJECT_ROOT = path.resolve(__dirname, "../../");

module.exports = {
  projectRootPath: PROJECT_ROOT,
  entryPath: path.join(PROJECT_ROOT, "src", "index.js"),
  outputPath: path.join(PROJECT_ROOT, "build"),
  appEntryPath: path.join(PROJECT_ROOT, "src"),
  buildUtilsPath: path.join(PROJECT_ROOT, "build_utils"),
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, in &lt;code&gt;package.json&lt;/code&gt;, add a new &lt;code&gt;build&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
"scripts": {
    "build": "webpack"
},

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's try it out! After running &lt;code&gt;npm run build&lt;/code&gt;, you should see a new folder was created, called &lt;code&gt;build&lt;/code&gt;, with a &lt;code&gt;main.js&lt;/code&gt; file in it!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Tip: Add the &lt;code&gt;build&lt;/code&gt; folder to &lt;code&gt;.gitignore&lt;/code&gt; to not commit it by accident. And if you haven't already, make sure to also ignore the &lt;code&gt;node_modules&lt;/code&gt; folder.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to move the static assets to the bundle. More specifically, we want to also include the &lt;code&gt;index.html&lt;/code&gt; file in the &lt;code&gt;build&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Easiest way to do this is with the &lt;code&gt;HtmlWebpackPlugin&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install html-webpack-plugin --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And update the &lt;code&gt;webpack.config.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const package = require("./package.json");
const commonPaths = require("./build_utils/config/commonPaths");

const isDebug = !process.argv.includes("release");

module.exports = {
  entry: commonPaths.entryPath,
  output: {
    uniqueName: package.name,
    publicPath: "/",
    path: commonPaths.outputPath,
    filename: `${package.version}/js/[name].[chunkhash:8].js`,
    chunkFilename: `${package.version}/js/[name].[chunkhash:8].js`,
    assetModuleFilename: isDebug
      ? `images/[path][name].[contenthash:8][ext]`
      : `images/[path][contenthash:8][ext]`,
    crossOriginLoading: "anonymous",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
      filename: "index.html",
    }),
  ],
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will copy the file under &lt;code&gt;public/index.html&lt;/code&gt;, copy it to the &lt;code&gt;build&lt;/code&gt; folder and inject a link to the bundled JS file (&lt;code&gt;main.js&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Let’s try it out!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm run build
npx serve build

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it works! You should now also see the message “Hello from Javascript” :D&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Webpack dev server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far, it was ok to just use &lt;code&gt;npx serve&lt;/code&gt; to check our app works. But in real life, it's easier to just use the &lt;code&gt;webpack-dev-server&lt;/code&gt;, so let's add that as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install --save-dev webpack-dev-server

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, configure it in the Webpack config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const package = require("./package.json");
const commonPaths = require("./build_utils/config/commonPaths");

const isDebug = !process.argv.includes("release");

const port = process.env.PORT || 3000;

module.exports = {
  entry: commonPaths.entryPath,
  output: {
    uniqueName: package.name,
    publicPath: "/",
    path: commonPaths.outputPath,
    filename: `${package.version}/js/[name].[chunkhash:8].js`,
    chunkFilename: `${package.version}/js/[name].[chunkhash:8].js`,
    assetModuleFilename: isDebug
      ? `images/[path][name].[contenthash:8][ext]`
      : `images/[path][contenthash:8][ext]`,
    crossOriginLoading: "anonymous",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
      filename: "index.html",
    }),
  ],
  devServer: {
    port: port,
    static: {
      directory: commonPaths.outputPath,
    },
    historyApiFallback: {
      index: "index.html",
    },
    webSocketServer: false,
  },
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, then add &lt;code&gt;npm run start&lt;/code&gt; script to &lt;code&gt;package.json&lt;/code&gt; and while we're there, pass in the right &lt;code&gt;--mode&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
  // ...,
  "scripts": {
    "build": "webpack --mode production",
    "start": "webpack serve --mode development"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, check that it works: run &lt;code&gt;npm run start&lt;/code&gt;, open &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; and check the app still works as before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Adding Babel&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is useful for allowing us to use all ES6 features and having them transpiled down to JS versions that all browsers can understand. (If you want to learn more about what Babel &lt;code&gt;preset-env&lt;/code&gt; is and why it's useful, the article by Jacob Lind is a great overview).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, let’s install the required packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm i @babel/core @babel/preset-env babel-loader --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, update the Webpack config to tell it to pass the files through Babel when bundling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const package = require("./package.json");
const commonPaths = require("./build_utils/config/commonPaths");

const isDebug = !process.argv.includes("release");

const port = process.env.PORT || 3000;

module.exports = {
  entry: commonPaths.entryPath,
  output: {
    uniqueName: package.name,
    publicPath: "/",
    path: commonPaths.outputPath,
    filename: `${package.version}/js/[name].[chunkhash:8].js`,
    chunkFilename: `${package.version}/js/[name].[chunkhash:8].js`,
    assetModuleFilename: isDebug
      ? `images/[path][name].[contenthash:8][ext]`
      : `images/[path][contenthash:8][ext]`,
    crossOriginLoading: "anonymous",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
      filename: "index.html",
    }),
  ],
  devServer: {
    port: port,
    static: {
      directory: commonPaths.outputPath,
    },
    historyApiFallback: {
      index: "index.html",
    },
    webSocketServer: false,
  },
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/, // exclude node_modules
        use: ["babel-loader"],
      },
    ],
  },
  resolve: {
    extensions: ["*", ".js"],
  },
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, create the Babel config file — &lt;code&gt;.babelrc&lt;/code&gt;. This is where we configure Babel to apply the &lt;code&gt;preset-env&lt;/code&gt; transform.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// .babelrc

{
  "presets": [
    "@babel/preset-env"
  ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Optionally, update the &lt;code&gt;index.js&lt;/code&gt; to contain some ES6 features that wouldn't work without Babel 😀 (actually they do work in most browsers, but for example IE still doesn't support Array.fill).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Use a feature that needs Babel to work in all browsers :)
// arrow functions + Array fill

const sayHelloManyTimes = (times) =&amp;gt;
  new Array(times).fill(1).map((_, i) =&amp;gt; `Hello ${i + 1}`);

const helloDiv = document.createElement("div");
helloDiv.innerHTML = sayHelloManyTimes(10).join("&amp;lt;br/&amp;gt;");
document.body.append(helloDiv);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, let’s check everything works — run &lt;code&gt;npm run start&lt;/code&gt; and check the app correctly runs:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Add React&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Finally, we can add React 😅&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm i react react-dom --save
npm i @babel/preset-react --save-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, update the &lt;code&gt;.babelrc&lt;/code&gt; file to also apply the &lt;code&gt;preset-react&lt;/code&gt; transform. This is needed, among other things, to support JSX.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// .babelrc

{
  "presets": [
    "@babel/preset-env",
    [
      "@babel/preset-react",
      {
        "runtime": "automatic"
      }
    ]
  ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Tip: Specifying the &lt;code&gt;preset-react&lt;/code&gt; runtime as &lt;code&gt;automatic&lt;/code&gt; enables a feature that no longer requires importing React on top of every file.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Also, we need to update the Webpack config to pass &lt;code&gt;jsx&lt;/code&gt; files through Babel as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const package = require("./package.json");
const commonPaths = require("./build_utils/config/commonPaths");

const isDebug = !process.argv.includes("release");

const port = process.env.PORT || 3000;

module.exports = {
  entry: commonPaths.entryPath,
  output: {
    uniqueName: package.name,
    publicPath: "/",
    path: commonPaths.outputPath,
    filename: `${package.version}/js/[name].[chunkhash:8].js`,
    chunkFilename: `${package.version}/js/[name].[chunkhash:8].js`,
    assetModuleFilename: isDebug
      ? `images/[path][name].[contenthash:8][ext]`
      : `images/[path][contenthash:8][ext]`,
    crossOriginLoading: "anonymous",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
      filename: "index.html",
    }),
  ],
  devServer: {
    port: port,
    static: {
      directory: commonPaths.outputPath,
    },
    historyApiFallback: {
      index: "index.html",
    },
    webSocketServer: false,
  },
   module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/, // exclude node_modules
        use: ["babel-loader"],
      },
    ],
  },
  resolve: {
    extensions: ["*", ".js", ".jsx"],
  },
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let’s create a React component, so we can check that everything works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// src/App.jsx

const App = () =&amp;gt; &amp;lt;h1&amp;gt;Hello from React!&amp;lt;/h1&amp;gt;;

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And add it to the main app file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// index.js

import { createRoot } from "react-dom/client";

import App from "./App.jsx";

const container = document.getElementById("root");
const root = createRoot(container);
root.render(&amp;lt;App /&amp;gt;);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note we’re using the new React 18 syntax with &lt;code&gt;createRoot&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finally, we also update the &lt;code&gt;index.html&lt;/code&gt; to provide a "root" node for the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&amp;gt;
    &amp;lt;meta
    name="viewport"
    content="width=device-width,initial-scale=1,minimum-scale=1,user-scalable=no"
    /&amp;gt;
    &amp;lt;meta
    httpEquiv="Cache-Control"
    content="no-cache, no-store, must-revalidate"
    /&amp;gt;
    &amp;lt;title&amp;gt;React + Webpack&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s check that it works — run &lt;code&gt;npm run start&lt;/code&gt; and you should see "Hello from React!":&lt;/p&gt;

&lt;p&gt;Also, check there are no errors in the console.&lt;/p&gt;

&lt;p&gt;You can also check that the app correctly runs in production, by running &lt;code&gt;npm run build&lt;/code&gt; and then &lt;code&gt;npx serve build&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That’s it!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>webpack</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
