<?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: Nasidul Islam</title>
    <description>The latest articles on DEV Community by Nasidul Islam (@nasidulislam).</description>
    <link>https://dev.to/nasidulislam</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%2F337831%2F6ad83b36-f751-475f-aabb-c37ecf15b526.jpeg</url>
      <title>DEV Community: Nasidul Islam</title>
      <link>https://dev.to/nasidulislam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nasidulislam"/>
    <language>en</language>
    <item>
      <title>Javascript Event Handling - Deep Dive</title>
      <dc:creator>Nasidul Islam</dc:creator>
      <pubDate>Tue, 22 Jun 2021 14:00:26 +0000</pubDate>
      <link>https://dev.to/nasidulislam/javascript-event-handling-deep-dive-112m</link>
      <guid>https://dev.to/nasidulislam/javascript-event-handling-deep-dive-112m</guid>
      <description>&lt;p&gt;&lt;em&gt;An unopinionated research (white) paper on front end event handling under the hood.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Overview&lt;/li&gt;
&lt;li&gt;Deep Dive

&lt;ul&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;Vue&lt;/li&gt;
&lt;li&gt;Angular JS&lt;/li&gt;
&lt;li&gt;Svelte&lt;/li&gt;
&lt;li&gt;jQuery - 1.6.4&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Resources&lt;/li&gt;

&lt;/ul&gt;

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

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The article takes an impartial approach at researching event handling in various UI tools. The content is based on official documentation -- &lt;em&gt;NOT&lt;/em&gt; on opinion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The purpose is to understand how the same "problem" was solved across these tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What this article is NOT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This article does not assess the pros and cons -- neither does it recommend one tool over another.&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;The world of Javascript evolves at a breakneck speed. For the longest time, a webpage would consist of a single, monolithic script file that handled everything - starting from enabling interactivity on the page to calling services and rendering content. The pattern has significant drawbacks. Monolithic architectures are difficult to scale and maintain in the long term. Especially at enterprise level where there are several engineers contributing code, a monolithic architecture tends become a spaghetti mess that is hard to debug.&lt;/p&gt;

&lt;p&gt;The inherent nature of Javascript allows engineers to innovate over this pattern and come up with ingenious ways to tackle the drawbacks. There are many, &lt;strong&gt;many&lt;/strong&gt;, &lt;a href="https://github.com/collections/front-end-javascript-frameworks" rel="noopener noreferrer"&gt;front end libraries and frameworks&lt;/a&gt; out there these days, each with its own superpowers and opinionated ways of approaching the problem. As a result, modern day developers are spoilt for choices when it comes to picking a system to built their application.&lt;/p&gt;

&lt;p&gt;Although the list of tools at the disposal of developers is exhaustive, not many have stood the test of time and battle. In this article, we will investigate the ones that have come out (fairly) unscathed, in an attempt to understand how they handle events.&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  Deep Dive
&lt;/h3&gt;

&lt;p&gt;In this section, we will deep dive into several popular, publicly available UI libraries and frameworks to investigate how they handle events. Let’s start with arguably the most popular.&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426547744%2F84Vwi6POw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426547744%2F84Vwi6POw.png" alt="gQGqBYSTN-removebg-preview.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Handling events in React
&lt;/h4&gt;

&lt;p&gt;Event handling in React centers around &lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/ReactBrowserEventEmitter.js" rel="noopener noreferrer"&gt;ReactBrowserEventEmitter&lt;/a&gt;. The very first comment in the source code does a decent job of explaining how it works.&lt;/p&gt;

&lt;p&gt;Summary of &lt;code&gt;ReactBrowserEventEmitter&lt;/code&gt; event handling:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624326293414%2FcQUsVwLKU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624326293414%2FcQUsVwLKU.png" alt="Screen Shot 2021-06-21 at 9.44.12 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Let's dive deep and break each of them down:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Top-level delegation is used to trap most native browser events. This may only occur in the main thread and is the responsibility of &lt;code&gt;ReactDOMEventListener&lt;/code&gt;, which is injected and can therefore support pluggable event sources. This is the only work that occurs in the main thread.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React uses event delegation to handle most of the interactive events in an application. This means when a &lt;code&gt;button&lt;/code&gt; with an &lt;code&gt;onClick&lt;/code&gt; handler is rendered&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;button onClick={() =&amp;gt; console.log('button was clicked')}&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React does not attach an event listener to the &lt;code&gt;button&lt;/code&gt; node. Instead, it gets a reference to the document root where the application is rendered and &lt;a href="https://github.com/facebook/react/blob/8a8d973d3cc5623676a84f87af66ef9259c3937c/packages/react-dom/src/client/ReactDOMComponent.js#L225" rel="noopener noreferrer"&gt;mounts&lt;/a&gt; an event listener there. &lt;strong&gt;React uses a single event listener per event type&lt;/strong&gt; to invoke all submitted handlers within the virtual DOM. Whenever a DOM event is fired, those top-level listeners initiate the actual event dispatching through the React source code — it re-dispatched the event for each and every handler. This can be seen in the source code of &lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/events/EventPluginHub.js" rel="noopener noreferrer"&gt;EventPluginHub&lt;/a&gt;.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; We normalize and de-duplicate events to account for browser quirks. This may be done in the worker thread.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;React normalizes event-types&lt;/strong&gt; so that so that each and every browser, regardless of its underlying engines or whether its old or modern, will have consistent event arguments. This means, across all browsers, devices and operating systems, a &lt;code&gt;click&lt;/code&gt; event will have arguments like this&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;boolean&lt;/strong&gt; altKey&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;boolean&lt;/strong&gt; metaKey&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;boolean&lt;/strong&gt; ctrlKey&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;boolean&lt;/strong&gt; shiftKey&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;boolean&lt;/strong&gt; getModifierState(key)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; button&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; buttons&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; clientX&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; clientY&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; pageX&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; pageY&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; screen&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;number&lt;/strong&gt; screenX&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DOMEventTarget&lt;/strong&gt; relatedTarget&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;: events supported in React, read &lt;a href="https://reactjs.org/docs/events.html#supported-events" rel="noopener noreferrer"&gt;this&lt;/a&gt;.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Forward these native events (with the associated top-level type used to trap it) to &lt;code&gt;EventPluginHub&lt;/code&gt;, which in turn will ask plugins if they want to extract any synthetic events.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React considers the nature of each event and categorizes them into buckets. It has dedicated plugins built to manage events in each bucket. &lt;strong&gt;Each of these plugins are then in turn responsible for extracting and handling the various event types&lt;/strong&gt; in that bucket. For instance, the &lt;code&gt;SimpleEventPlugin&lt;/code&gt; will handle events implemented in common browsers such as mouse and key press events (&lt;a href="https://share.cocalc.com/share/a04c90b3eaea18961287b4f6b5c13a7df2d3f0f1/react/wstein/node_modules/react/lib/SimpleEventPlugin.js?viewer=share" rel="noopener noreferrer"&gt;source&lt;/a&gt;) and &lt;code&gt;ChangeEventPlugin&lt;/code&gt; will handle &lt;code&gt;onChange&lt;/code&gt; events (&lt;a href="https://share.cocalc.com/share/a04c90b3eaea18961287b4f6b5c13a7df2d3f0f1/react/wstein/react-with-addons.js?viewer=share" rel="noopener noreferrer"&gt;source&lt;/a&gt;). The final piece that unifies all the plugins into a single place and re-directs events to each individual plugin is the &lt;code&gt;EventPluginHub&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This opens the door for us to understand how React views events. React introduces the concept of &lt;code&gt;SyntheticEvents&lt;/code&gt;, which React defines as "&lt;em&gt;implementation of the DOM Level 3 Events API by normalizing browser quirks&lt;/em&gt;". Basically, it is a &lt;strong&gt;wrapper around the browser's native event object&lt;/strong&gt; with the same interface — and that it works in identical fashion across all browsers.&lt;/p&gt;

&lt;p&gt;For React v16 and earlier, synthetic events utilizes a polling mechanism. This mechanism ensures that the same object instance is used in multiple handlers, but it is being reset with new properties before each and every invocation and is then disposed.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; The &lt;code&gt;EventPluginHub&lt;/code&gt; will then process each event by annotating them with "dispatches", a sequence of listeners and IDs that care about that event.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;In the React ecosystem, a single event listener is attached at the document root for any one event-type&lt;/strong&gt;. Since each event type will most likely have multiple handlers, &lt;strong&gt;React will accumulate the events and their handlers&lt;/strong&gt; (&lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/events/EventPropagators.js#L90" rel="noopener noreferrer"&gt;source&lt;/a&gt;). Then, it will do relevant dispatches, which consist of event handlers and their corresponding fiber nodes. The fiber nodes are nodes in the virtual DOM tree. Fiber nodes are calculated using React’s Reconciliation algorithm, which is its “diffing” algorithm to drive updates on the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;: &lt;a href="https://blog.logrocket.com/deep-dive-into-react-fiber-internals/" rel="noopener noreferrer"&gt;React Fiber Architecture&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;: &lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;React Reconciliation concept&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; The &lt;code&gt;EventPluginHub&lt;/code&gt; then dispatches the events.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The final piece of the puzzle — &lt;strong&gt;the plugin hub goes through the accumulated information and dispatches the events&lt;/strong&gt;, thus invoking the submitted event handlers (&lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/events/EventPluginUtils.js#L77" rel="noopener noreferrer"&gt;source&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple Demo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple click handler demo implementation in React --&amp;gt; &lt;a href="https://codesandbox.io/s/thirsty-wildflower-57x4m?file=/src/App.js" rel="noopener noreferrer"&gt;Link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426809930%2F9nXRxfDqW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426809930%2F9nXRxfDqW.png" alt="nvH99l_qB-removebg-preview.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Handling events in Vue
&lt;/h4&gt;

&lt;p&gt;In Vue, you create &lt;code&gt;.vue&lt;/code&gt; file that contains a &lt;code&gt;script&lt;/code&gt; tag to execute javascript and a &lt;code&gt;template&lt;/code&gt; tag that wraps all markup (both DOM and custom elements). This is a self contained instance of a Vue component that could also contain a &lt;code&gt;style&lt;/code&gt; tag to house the CSS.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Simple DOM events handling&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Vue allows developers to bind events to elements using &lt;code&gt;v-on:&amp;lt;event-name&amp;gt;&lt;/code&gt; or in short, &lt;code&gt;@&amp;lt;event-name&lt;/code&gt; directive, and to store the state of application in a &lt;code&gt;data&lt;/code&gt; prop. &lt;strong&gt;All the event handlers are stored similarly in a &lt;code&gt;methods&lt;/code&gt; prop on the same object&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

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

&amp;lt;template&amp;gt;
    &amp;lt;div id="app"&amp;gt;
        &amp;lt;HelloWorld :msg="msg" /&amp;gt;
        &amp;lt;button @click="greet('World', $event)"&amp;gt;
            click here
        &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
    import HelloWorld from "./components/HelloWorld";

    export default {
        name: "App",
        components: { HelloWorld },
        data: function () {
            return { msg: "Vue" };
        },
        methods: {
            greet: function (message, $event) { this.msg = message; }
        }
    }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application will load with a message of “Hello Vue”. When the button is clicked, the handler will set the message to World and display a “Hello World” message --&amp;gt; &lt;a href="https://codesandbox.io/s/vue-demo-dom-event-handler-wxces?file=/src/App.vue" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;. It is possible to access the original DOM event by passing in an object from the handler reference and accessing in the event handler.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Event Modifiers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Although it is possible to access the DOM event object in the handler by simply passing it in, Vue improves developer experience by allowing to extend event handling by attaching ‘modifiers’ to it. This way, &lt;strong&gt;Vue will handle the modifiers for you instead of the developer calling those modifiers explicitly in their handlers&lt;/strong&gt;. Multiple modifiers can be attached by using a dot delimited pattern. The full list of supported modifiers is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.stop&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.prevent&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.capture&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.self&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.once&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.passive&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus, a simple example would look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* this will trigger the handler method only once */

&amp;lt;button v-on:click.stop.once="clickHandler"&amp;gt;Click me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Link --&amp;gt; &lt;a href="https://codesandbox.io/s/vue-demo-event-modifiers-gbqmo?file=/src/App.vue" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Key Modifiers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Vue has a feature to attach keyboard events in almost identical fashion as regular event handlers&lt;/strong&gt;. It supports a list of aliases with commonly attached keyboard events such as the &lt;code&gt;enter&lt;/code&gt; and &lt;code&gt;tab&lt;/code&gt; keys. The full list of aliases is given below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.enter&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.tab&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.delete&lt;/code&gt; (captures both the "Delete" and "Backspace" keys)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.esc&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.up&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.down&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.left&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.right&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.space&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple example would look like the following&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;!-- only call `vm.submit()` when the `key` is `Enter` --&amp;gt;

&amp;lt;input v-on:keyup.enter="submit"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;LInk --&amp;gt; &lt;a href="https://codesandbox.io/s/vue-demo-keyboard-events-mknml?file=/src/App.vue" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Custom Events&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Vue handles publishing and subscribing to custom events&lt;/strong&gt;. The caveat here is that every component that should listen for events should maintain an explicit list of those custom events. A simple example would look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// emit event
this.$emit('myEvent')

// bind to the event
&amp;lt;my-component v-on:myevent="doSomething"&amp;gt;&amp;lt;/my-component&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike components and props, event names will never be used as variable or property names in JavaScript, so there’s no reason to use camelCase or PascalCase. Additionally, &lt;code&gt;v-on&lt;/code&gt; event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML’s case-insensitivity), so &lt;code&gt;v-on:myEvent&lt;/code&gt; would become &lt;code&gt;v-on:myevent&lt;/code&gt; -- making &lt;code&gt;myEvent&lt;/code&gt; impossible to listen to. Vue JS as a framework recommends using kebab-casing for event names.&lt;/p&gt;

&lt;p&gt;Link --&amp;gt; &lt;a href="https://codesandbox.io/s/vue-demo-custom-events-5bbuh" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426885939%2FEzql0cxbR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426885939%2FEzql0cxbR.png" alt="opjQ6LN2U-removebg-preview.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Angular is one of the first generation, opinionated frameworks that focuses towards building Single Page Applications (SPAs). Although it has gone significant re-invention in recent times, it still falls short in a number of ways when compared to the more modern tools available to developers these days (some of which are discussed in this article). It is still, however, valuable to take a peek at how the framework binds and handles events.&lt;/p&gt;
&lt;h4&gt;
  
  
  Handling events in Angular (4.x and above)
&lt;/h4&gt;

&lt;p&gt;Angular has a very specific syntax to bind and handle events. This syntax consists of a target event name within parentheses to the left of an equal sign, and a quoted template statement to the right (&lt;a href="https://angular.io/guide/event-binding" rel="noopener noreferrer"&gt;source&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;A simple example of DOM event binding and handling looks like this&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;button (click)="onSave()"&amp;gt;Save&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When events are being bound, Angular configures an event handler for the target event&lt;/strong&gt; — it can be used with custom events as well. When either the component or the directive &lt;em&gt;raises&lt;/em&gt; the event, the handler executes the &lt;em&gt;template statement&lt;/em&gt;. Then, the template statement performs an action in response to the event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Angular, it is possible to pass an $event object to the function handling the event&lt;/strong&gt;. The shape of the &lt;code&gt;$event&lt;/code&gt; object is determined by the &lt;code&gt;target event&lt;/code&gt;. If the event is a native DOM element event, then the &lt;code&gt;$event&lt;/code&gt; object is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Events" rel="noopener noreferrer"&gt;DOM event object&lt;/a&gt;. Lets look at a simple example (&lt;a href="https://angular.io/guide/event-binding-concepts" rel="noopener noreferrer"&gt;source&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;&amp;lt;input
    [value]="currentItem.name"
    (input)="currentItem.name=$event.target.val"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a couple of things happening here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code binds to the &lt;code&gt;input&lt;/code&gt; event of the &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; element, which allows the code to listen for changes.&lt;/li&gt;
&lt;li&gt;When the user makes changes, the component raises the &lt;code&gt;input&lt;/code&gt; event.&lt;/li&gt;
&lt;li&gt;The binding executes the statement within a context that includes the DOM event object, &lt;code&gt;$event&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Angular retrieves the changed text by following the path &lt;code&gt;$event.target.vaue&lt;/code&gt; and updates the &lt;code&gt;name&lt;/code&gt; property.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the event belongs to a directive or component, &lt;code&gt;$event&lt;/code&gt; has the shape that the directive or component produces.&lt;/p&gt;

&lt;p&gt;Link --&amp;gt; &lt;a href="https://stackblitz.com/angular/pegebmnalav?file=src%2Fapp%2Fapp.component.ts" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426953035%2FCOI55mlu6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613426953035%2FCOI55mlu6.png" alt="qDzthaVmb-removebg-preview.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Handling events in Svelte
&lt;/h4&gt;

&lt;p&gt;In Svelte, you create a &lt;code&gt;.svelte&lt;/code&gt; file that is meant to self contain a component instance with its CSS, JS and HTML, along with any custom elements that are needed.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Simple DOM events handling&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A simple demo for a click handler will look like the following:&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;script&amp;gt;
    let name = 'world';
    function update() { name = 'Svelte'; }
&amp;lt;/script&amp;gt;

&amp;lt;span on:click={update}&amp;gt;Hello { name }&amp;lt;/span&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print &lt;code&gt;Hello World&lt;/code&gt; on load, but will update and print &lt;code&gt;Hello Svelte&lt;/code&gt; when user clicks on &lt;code&gt;h1&lt;/code&gt; -&amp;gt; &lt;a href="https://svelte.dev/repl/af38f740da8c4733817a26328ba7d061?version=3.31.0" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;. This is the general pattern in which DOM events such as &lt;code&gt;click&lt;/code&gt;, &lt;code&gt;mousemove&lt;/code&gt;, etc are implemented in Svelte (it supports inline handlers as well).&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Event Modifiers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;The system allows  developers to add pipe delimited modifiers to the event&lt;/strong&gt;, such as &lt;code&gt;preventDefault&lt;/code&gt; and &lt;code&gt;stopPropagation&lt;/code&gt;. The handler function is able to accept an &lt;code&gt;event&lt;/code&gt; argument that also has access to these modifiers, but Svelte offers an improvement in developer experience by offering these shorthands. An example would look like the following:&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;script&amp;gt;
    function handleClick() { alert('This alert will trigger only once!'); }
&amp;lt;/script&amp;gt;

&amp;lt;button on:click|once={ handleClick }&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, the pattern looks like &lt;code&gt;on:&amp;lt;event-name&amp;gt;|modifier1|modifier2|...&lt;/code&gt; -&amp;gt; &lt;a href="https://svelte.dev/repl/a5d264f4ace9462faf39b2a592e97295?version=3.31.0" rel="noopener noreferrer"&gt;REPL&lt;/a&gt;. The full list of modifiers is below (&lt;a href="https://svelte.dev/tutorial/event-modifiers" rel="noopener noreferrer"&gt;source&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;preventDefault&lt;/code&gt; - calls &lt;code&gt;event.preventDefault()&lt;/code&gt; before running the handler. Useful for client-side form handling&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;stopPropagation&lt;/code&gt; - calls &lt;code&gt;event.stopPropagation()&lt;/code&gt;, preventing the event from reaching the next element&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;passive&lt;/code&gt; -  improves scrolling performance on touch/wheel events (Svelte will add it automatically where its safe to do so)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nonpassive&lt;/code&gt; - explicitly set &lt;code&gt;passive: false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;capture&lt;/code&gt; - fires the handler during the &lt;em&gt;capture&lt;/em&gt; phase instead of the &lt;em&gt;bubbling&lt;/em&gt; phase (&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture" rel="noopener noreferrer"&gt;MDN docs&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;once&lt;/code&gt; - remove the handler after the first time it runs&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;self&lt;/code&gt; - only trigger handler if &lt;code&gt;event.target&lt;/code&gt; is the element itself&lt;/li&gt;
&lt;/ul&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Dispatching events&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;In Svelte, a parent component can update state based on data dispatched from a child component&lt;/strong&gt; using a function called &lt;code&gt;createEventDispatcher&lt;/code&gt;. The function allows the child component to emit a data object at a user defined key. The parent component can then do as it pleases with it -&amp;gt; &lt;a href="https://svelte.dev/repl/2212634b19314d2c9e157dffd73edd8f?version=3.31.0" rel="noopener noreferrer"&gt;REPL&lt;/a&gt; (open console to see dispatched data object).&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Event forwarding&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The caveat to component events is that it does not &lt;em&gt;bubble&lt;/em&gt;. Thus, &lt;strong&gt;if a parent component needs to listen on an event that is emitted by a deeply nested component, all the intermediate components will have to &lt;em&gt;forward&lt;/em&gt; that event&lt;/strong&gt;. Event forwarding is achieved by adding the custom data key on each wrapper component as we traverse up Svelte DOM. Finally, the parent component where the event needs to be handles implements a handler for it -&amp;gt; &lt;a href="https://svelte.dev/repl/49b1b14aef8f4bff8fab771394ae876c?version=3.32.3" rel="noopener noreferrer"&gt;REPL&lt;/a&gt; (open console to see demo).&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Actions&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The final piece in Svelte event handling is the implementation of &lt;code&gt;actions&lt;/code&gt;. &lt;strong&gt;Actions are element level functions that are useful for adding custom event handlers&lt;/strong&gt;. Similar to transition functions, an action function receives a &lt;code&gt;node&lt;/code&gt; and some optional parameters and returns an action object. That object can have a &lt;code&gt;destroy&lt;/code&gt; function, which is called when the element is unmounted -&amp;gt; &lt;a href="https://svelte.dev/repl/79f3cd81b76d42909ec69e042c74abd5?version=3.31.0" rel="noopener noreferrer"&gt;REPL&lt;/a&gt; (borrowed from Svelte official resources).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;: &lt;a href="https://svelte.dev/tutorial/basics" rel="noopener noreferrer"&gt;Svelte official tutorials&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;: &lt;a href="https://lihautan.com/compile-svelte-in-your-head-part-1/#adding-event-listeners" rel="noopener noreferrer"&gt;Compile Svelte in your head&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613427022227%2Fd7lL9dbqQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1613427022227%2Fd7lL9dbqQ.png" alt="PzwQ9zuu8-removebg-preview.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Handling events in jQuery
&lt;/h4&gt;

&lt;p&gt;The primary benefit of using jQuery is that it makes DOM traversal and manipulation quite convenient. Since most browser events initiated by users are meant to provide UI feedback, this feature handy. Under the hood, jQuery uses a powerful "selector" engine called &lt;a href="https://github.com/jquery/sizzle" rel="noopener noreferrer"&gt;Sizzle&lt;/a&gt;. Sizzle is a pure JS-CSS selector engine designed to be dropped in to any host library.&lt;/p&gt;

&lt;p&gt;Let’s look at the programming model and categories of how jQuery binds and handles events. The “source” links provided is the official documentation of the APIs and has additional information of how they work:&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Browser Events&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/category/events/browser-events/" rel="noopener noreferrer"&gt;Browser Events&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;jQuery is able to handle the following browser events out of the box.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.error()&lt;/code&gt;: Bind an event handler to the "error" JS event (&lt;a href="https://api.jquery.com/error/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.resize()&lt;/code&gt;: Bind an event handler to the "resize" JS event, or trigger the on an element (&lt;a href="https://api.jquery.com/resize/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.scroll()&lt;/code&gt;: Bind an event handler to the "scroll" JS event, or trigger the event on an element (&lt;a href="https://api.jquery.com/scroll/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Document Loading&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/category/events/document-loading/" rel="noopener noreferrer"&gt;Document Loading&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;jQuery provides a short list of out of the box APIs to handle events related to initial page load&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;jQuery.holdReady()&lt;/code&gt;: Holds or releases the execution of jQuery's ready event (&lt;a href="https://api.jquery.com/jQuery.holdReady/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jQuery.ready()&lt;/code&gt;: A Promise-like object that resolves when the document is ready (&lt;a href="https://api.jquery.com/jQuery.ready/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.load()&lt;/code&gt;: Bind an event handler to the "load" JS event (&lt;a href="https://api.jquery.com/load-event/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.ready()&lt;/code&gt;: Specify a function to execute when the DOM is fully loaded (&lt;a href="https://api.jquery.com/ready/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.unload()&lt;/code&gt;: Bind an event handler to the "unload" JS event (&lt;a href="https://api.jquery.com/unload/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Form Events&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/category/events/form-events/" rel="noopener noreferrer"&gt;Form Events&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;jQuery provides a decent list of out of the box APIs to handle commonly occurring form events&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.blur()&lt;/code&gt;: Bind an event handler to the “blur” JS event, or trigger that event on an element (&lt;a href="https://api.jquery.com/blur/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.change()&lt;/code&gt;: Bind an event handler to the “change” JS event, or trigger that event on an element (&lt;a href="https://api.jquery.com/change/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.focus()&lt;/code&gt;: Bind an event handler to the “focus” JS event, or trigger that event on an element (&lt;a href="https://api.jquery.com/focus/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.focusin()&lt;/code&gt;: Bind an event handler to the “focusin” JS event (&lt;a href="https://api.jquery.com/focusin/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.focusout()&lt;/code&gt;: Bind an event handler to the “focusout” JS event (&lt;a href="https://api.jquery.com/focusout/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.select()&lt;/code&gt;: Bind an event handler to the “select” JS event, or trigger that event on an element (&lt;a href="https://api.jquery.com/select/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.submit()&lt;/code&gt;: Bind an event handler to the “submit” JS event, or trigger that event on an element (&lt;a href="https://api.jquery.com/submit/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Keyboard Events&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/category/events/keyboard-events/" rel="noopener noreferrer"&gt;Keyboard Events&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following are out of the box APIs provided by jQuery to handle keyboard events&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.keydown()&lt;/code&gt;: Bind an event handler to the "keydown" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/keydown/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.keypress()&lt;/code&gt;: Bind an event handler to the "keypress" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/keypress/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.keyup()&lt;/code&gt;: Bind an event handler to the "keyup" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/keyup/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Mouse Events&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/category/events/mouse-events/" rel="noopener noreferrer"&gt;Mouse Events&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is where jQuery begins to shine as far as event handling is concerned. It offers a large suite of mouse event binders out of the box for developers to use.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.click()&lt;/code&gt;: Bind an event handler to the "click" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/click/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.dblclick()&lt;/code&gt;: Bind an event handler to the "dblclick" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/dblclick/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.contextmenu()&lt;/code&gt;: Bind an event handler to the "contextmenu" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/contextmenu/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mousemove()&lt;/code&gt;: Bind an event handler to the "mousemove" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mousemove/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mouseout()&lt;/code&gt;: Bind an event handler to the "mouseout" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mouseout/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mouseover()&lt;/code&gt;: Bind an event handler to the "mouseover" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mouseover/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mouseup()&lt;/code&gt;: Bind an event handler to the "mouseup" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mouseup/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.toggle()&lt;/code&gt;: Bind an event handler to the "toggle" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/toggle-event/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.hover()&lt;/code&gt;: Bind an event handler to the "hover" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/hover/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mousedown()&lt;/code&gt;: Bind an event handler to the "mousedown" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mousedown/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mouseenter()&lt;/code&gt;: Bind an event handler to the "mouseenter" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mouseenter/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mouseleave()&lt;/code&gt;: Bind an event handler to the "mouseleave" JS event, or trigger that event on an an element (&lt;a href="https://api.jquery.com/mouseleave/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; The Event Object&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Souce: &lt;a href="https://api.jquery.com/category/events/event-object/" rel="noopener noreferrer"&gt;Event Object&lt;/a&gt;, &lt;a href="https://learn.jquery.com/events/inside-event-handling-function/" rel="noopener noreferrer"&gt;Inside Event Handling Function&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Event handlers in jQuery accepts the event object as the first argument. This object has access to various properties and modifiers. Here is a list of the more commonly occurring ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;event.currentTarget()&lt;/code&gt;: The current DOM element within the event handling bubbling phase (&lt;a href="https://api.jquery.com/event.currentTarget/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event.target()&lt;/code&gt;: The DOM element that initiated the event (&lt;a href="https://api.jquery.com/event.target/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event.data()&lt;/code&gt;: Optional data object passed to the handler when the current executing handler is bound (&lt;a href="https://api.jquery.com/event.data/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event.preventDefault()&lt;/code&gt;: If this method is called, the default action of the event will not be triggered (&lt;a href="https://api.jquery.com/event.preventDefault/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event.stopPropagation()&lt;/code&gt;: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event (&lt;a href="https://api.jquery.com/event.stopPropagation/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Information below this point is related to jQuery versions later than &lt;code&gt;1.6.4&lt;/code&gt;&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; The &lt;code&gt;.on()&lt;/code&gt; Event Handler Attachment API&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/on/" rel="noopener noreferrer"&gt;The &lt;code&gt;.on()&lt;/code&gt; Event Handler Attachment API&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Modern versions of jQuery provides an all-encompassing API to handle events -- the &lt;code&gt;.on()&lt;/code&gt;. This API is designed to bind almost all of the events listed above with one single stroke. It is the recommended way to bind events (&lt;a href="https://learn.jquery.com/events/handling-events/" rel="noopener noreferrer"&gt;according to official documentation&lt;/a&gt;) from jQuery - 1.7 version and onwards. A few syntax examples can be seen below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Markup to be used for all examples that follow
&amp;lt;div class='outer'&amp;gt;
    &amp;lt;span class='inner'&amp;gt;Any content&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&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;// Exhibit A: the simple click handler, targeting the inner span
$('.outer .inner').on('click', function(event) {
    console.log(event);
    alert( 'inner span was clicked!!' );
});

// Exhibit B: attaching separate handlers to different event types
$('.outer .inner').on({
    mouseenter: function() {
        console.log( 'hovered over a span' );
    },
    mouseleave: function() {
        console.log( 'mouse left a span' ); 
    },
    click: function() {
        console.log( 'clicked a span' );    
    }
});

// Exhibit C: attaching the same handler to different event types
$('.outer .inner').on('click', function() {
    console.log( 'The span was either clicked or hovered on' );
});

// Exhibit D: Event delegation --&amp;gt; binding events to elements that don't exist yet
$('.outer .inner').on('click', '&amp;lt;selector-of-element-that-dont-exist-yet&amp;gt;', function() {
    console.log( 'The element was clicked' );
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;blockquote&gt;
&lt;p&gt;--&amp;gt; Other Event Handler Attachment APIs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://api.jquery.com/category/events/event-handler-attachment/" rel="noopener noreferrer"&gt;Event Handler Attachment&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.on()&lt;/code&gt; API is arguably the most popular API offered by jQuery. Apart from it, there are other interfaces jQuery has out of the box that provides a useful suite of functionality. The following is a list of the most commonly occurring ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;one()&lt;/code&gt;: Attach a handler to an event for the elements. The handler is executed at most once per element per event type (&lt;a href="https://api.jquery.com/one/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;off()&lt;/code&gt;: Remove an event handler (&lt;a href="https://api.jquery.com/off/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;trigger()&lt;/code&gt;: Execute all handlers and behaviors attached to the matched elements for the given event type (&lt;a href="https://api.jquery.com/trigger/" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;br&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/collections/front-end-javascript-frameworks" rel="noopener noreferrer"&gt;List of front end JS frameworks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;React

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/ReactBrowserEventEmitter.js" rel="noopener noreferrer"&gt;ReactBrowserEventEmitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/react/blob/8a8d973d3cc5623676a84f87af66ef9259c3937c/packages/react-dom/src/client/ReactDOMComponent.js" rel="noopener noreferrer"&gt;ReactDOMComponent&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/events.html#supported-events" rel="noopener noreferrer"&gt;Synthetic Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/events/EventPluginHub.js" rel="noopener noreferrer"&gt;EventPluginHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://share.cocalc.com/share/a04c90b3eaea18961287b4f6b5c13a7df2d3f0f1/react/wstein/node_modules/react/lib/SimpleEventPlugin.js?viewer=share" rel="noopener noreferrer"&gt;SimpleEventPlugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://share.cocalc.com/share/a04c90b3eaea18961287b4f6b5c13a7df2d3f0f1/react/wstein/react-with-addons.js?viewer=share" rel="noopener noreferrer"&gt;ChangeEventPlugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/events/EventPropagators.js" rel="noopener noreferrer"&gt;EventPropagators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/events/EventPluginUtils.js" rel="noopener noreferrer"&gt;EventPluginUtils&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;Reconciliation Algorithm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.logrocket.com/deep-dive-into-react-fiber-internals/" rel="noopener noreferrer"&gt;React Fiber Architecture&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Svelte

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://svelte.dev/tutorial/basics" rel="noopener noreferrer"&gt;Svelte official tutorials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://lihautan.com/compile-svelte-in-your-head-part-1/#adding-event-listeners" rel="noopener noreferrer"&gt;Compile Svelte in your head&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Vue

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://vuejs.org/v2/guide/events.html" rel="noopener noreferrer"&gt;Event handling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vuejs.org/v2/guide/events.html#Event-Modifiers" rel="noopener noreferrer"&gt;Event Modifiers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vuejs.org/v2/guide/events.html#Key-Modifiers" rel="noopener noreferrer"&gt;Keyboard Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vuejs.org/v2/guide/components-custom-events.html" rel="noopener noreferrer"&gt;Custom Events&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Angular

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://angular.io/guide/event-binding" rel="noopener noreferrer"&gt;Event handling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://angular.io/guide/event-binding-concepts" rel="noopener noreferrer"&gt;Event binding concepts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;jQuery

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/jquery/sizzle" rel="noopener noreferrer"&gt;Sizzle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.jquery.com/category/events/browser-events/" rel="noopener noreferrer"&gt;Browser Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.jquery.com/category/events/document-loading/" rel="noopener noreferrer"&gt;Document Loading&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.jquery.com/category/events/form-events/" rel="noopener noreferrer"&gt;Form Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.jquery.com/category/events/keyboard-events/" rel="noopener noreferrer"&gt;Keyboard Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.jquery.com/category/events/mouse-events/" rel="noopener noreferrer"&gt;Mouse Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.jquery.com/category/events/event-object/" rel="noopener noreferrer"&gt;Event Object&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.jquery.com/events/inside-event-handling-function/" rel="noopener noreferrer"&gt;Inside Event Handling Function&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webpack</category>
      <category>babel</category>
    </item>
    <item>
      <title>Advanced React + Webpack 4 + Babel 7 Application Setup</title>
      <dc:creator>Nasidul Islam</dc:creator>
      <pubDate>Mon, 13 Apr 2020 19:53:29 +0000</pubDate>
      <link>https://dev.to/nasidulislam/advanced-react-webpack-4-babel-7-application-setup-5ggi</link>
      <guid>https://dev.to/nasidulislam/advanced-react-webpack-4-babel-7-application-setup-5ggi</guid>
      <description>&lt;p&gt;The world of building user interfaces can be a complex landscape to navigate. The sheer number of tools that are at the disposal of a developer is overwhelming. In my last  &lt;a href="https://nasidulislam.hashnode.dev/beginners-guide-to-minimal-react-webpack-4-babel-7-project-setup-ck7m7erad00ec8ss1xzmfntqm" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; , we discussed a few of those tools (React, Webpack and Babel) and went over the basics of what they are and how they work. More over, we also learnt how we can stitch them together to build an application code base from scratch that is suitable for development.  &lt;/p&gt;

&lt;p&gt;The application that was pieced together has minimal features. It does not allow us to test the code we're writing, among other things, and it's certainly not suitable for deploying to production.  &lt;strong&gt;In this guide&lt;/strong&gt;, we will build on top of the setup we have and take it further&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn Dev + Production environment configs&lt;/li&gt;
&lt;li&gt;Add test frameworks&lt;/li&gt;
&lt;li&gt;Sass&lt;/li&gt;
&lt;li&gt;ESLint&lt;/li&gt;
&lt;li&gt;Static assets (images, SVG icons, font icons, font families)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;The introduction segments can be skipped. Click  here  to jump straight to the step by step guide .&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment configuration
&lt;/h2&gt;

&lt;p&gt;An application consists of features and every feature has a life cycle --- from it being &lt;code&gt;developed&lt;/code&gt;, then going through &lt;code&gt;testing&lt;/code&gt; and finally being deployed to &lt;code&gt;production&lt;/code&gt;, it lives on different environments (&lt;em&gt;envs&lt;/em&gt;). The environments serve different purposes and therefore, their needs vary accordingly.&lt;/p&gt;

&lt;p&gt;For instance, we don't care about performance or optimization in &lt;em&gt;dev&lt;/em&gt; env, neither do we care about minifying the code. Often, we enable tools in dev env that helps us write code and debug it, like source maps, linters, etc. On the other hand, on &lt;em&gt;prod&lt;/em&gt; env, we absolutely care about stuff like application performance and security, caching, etc. The tools we are going to use while walking through this guide is not going to play with all the items we discussed here, however, we will go through basics (and some more) of how environment configuration works and why it is useful.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Test Frameworks
&lt;/h2&gt;

&lt;p&gt;A test framework provides us with a platform and a set of rules that allows us to test the code we're writing. Any application that is intended to be deployed for users must be tested. Here is why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It helps reduce the number of bugs&lt;/strong&gt; --- and if we write new tests for the ones that &lt;em&gt;do&lt;/em&gt; come up, we greatly minimize the chance of that particular bug re-appearing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It gives us confidence when attempting to refactor code&lt;/strong&gt;. A failing test would mean the refactored code did not satisfy that particular scenario.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improves code quality&lt;/strong&gt;, because developers are bound to write code that is &lt;em&gt;testable&lt;/em&gt;, although writing good tests is an entirely different (and &lt;em&gt;extremely&lt;/em&gt; valuable) skill of its own&lt;/li&gt;
&lt;li&gt;All the reasons above &lt;strong&gt;reduce overall cost of development&lt;/strong&gt; in the long run (fewer bugs, better code quality, etc.)&lt;/li&gt;
&lt;li&gt;Well written tests becomes a form of documentation in itself of the code for which the test is being written.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The frameworks come in various different flavors --- and they all have their pros and cons. For our purposes, we will use two of the more popular frameworks, &lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; to test functional JS and &lt;a href="https://enzymejs.github.io/enzyme/" rel="noopener noreferrer"&gt;Enzyme&lt;/a&gt; to test our &lt;strong&gt;React&lt;/strong&gt; components.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Sass
&lt;/h2&gt;

&lt;p&gt;As the application grows in size, it starts to present maintainability and scalability concerns for developers. CSS is one such area where the code can get real messy real fast.  &lt;a href="https://sass-lang.com/" rel="noopener noreferrer"&gt;Sass&lt;/a&gt;  is a tool that helps us in this regard:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compiles to CSS&lt;/strong&gt;, so the end result is familiar code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It allows nesting selectors&lt;/strong&gt;. This enables developers to write cleaner and fewer lines of code &lt;em&gt;and&lt;/em&gt; opens the door for more maintainable stylesheets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It allows for creating variables, mixins&lt;/strong&gt;, further promoting maintainability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conditional CSS&lt;/strong&gt;, exciting stuff !!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is industry approved&lt;/strong&gt; --- performant and formidable community support.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No reason to not use a tool that will surely improve our development workflow, right?&lt;/p&gt;

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

&lt;h2&gt;
  
  
  ESLint
&lt;/h2&gt;

&lt;p&gt;Another point of concern as the code base begins to grow is ensuring high standards of code quality. This is especially more important when there are multiple teams or developers working on the same code base.  &lt;a href="https://eslint.org/" rel="noopener noreferrer"&gt;ESLint&lt;/a&gt;  saves the day here --- it enforces common coding standards, or &lt;em&gt;style guides&lt;/em&gt;, for all devs to follow. There are many industry approved style guides out there, for instance  &lt;a href="https://google.github.io/styleguide/htmlcssguide.html" rel="noopener noreferrer"&gt;Google&lt;/a&gt; and  &lt;a href="https://github.com/airbnb/javascript" rel="noopener noreferrer"&gt;AirBnB&lt;/a&gt;. For our purposes, we will use the AirBnB style guide.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Static Assets
&lt;/h2&gt;

&lt;p&gt;This encompasses all the pretty stuff that will be used in the application --- custom fonts, font icons, SVGs and images. They are placed in a &lt;code&gt;public&lt;/code&gt; folder, although an argument can be made for a different setup.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Please note:&lt;/strong&gt; The rest of the guide builds on top of the  &lt;a href="https://nasidulislam.hashnode.dev/beginners-guide-to-minimal-react-webpack-4-babel-7-project-setup-ck7m7erad00ec8ss1xzmfntqm" rel="noopener noreferrer"&gt;last piece&lt;/a&gt;  I wrote. You can either follow that first before proceeding here, &lt;em&gt;or&lt;/em&gt; do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ensure that that you have node version 10.15.3 or above. Open up your terminal and type in &lt;code&gt;node -v&lt;/code&gt; to check. If the version does not match the requirements, grab the latest from  &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;here&lt;/a&gt; .&lt;/li&gt;
&lt;li&gt;Once you're good with the above, grab the  &lt;a href="https://github.com/all-things-javascript/minimal-react-webpack-babel" rel="noopener noreferrer"&gt;repo&lt;/a&gt; and follow the installation instructions in the &lt;code&gt;README&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After installing the dependencies using &lt;code&gt;npm install&lt;/code&gt;, run &lt;code&gt;npm start&lt;/code&gt; to compile the code and spin up the dev server. At this point, you should see a new browser tab open, rendering a &lt;code&gt;hello world&lt;/code&gt; component. Make sure you're inside the repository directory that you just "git cloned" before trying out the command.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;em&gt;After having gone through the basics of the tools we're about to use and setting up our base repo, we can finally move forward to the guide.&lt;/em&gt;&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Assuming  &lt;a href="https://github.com/all-things-javascript/minimal-react-webpack-babel" rel="noopener noreferrer"&gt;repo&lt;/a&gt; has been successfully downloaded, open it up in a text-editor of your choice. You should see a file called &lt;code&gt;webpack.config.js&lt;/code&gt;. This is where webpack configs currently live in its entirety.  &lt;/p&gt;

&lt;p&gt;In order to separate &lt;em&gt;production&lt;/em&gt; and &lt;em&gt;development&lt;/em&gt; builds, we will create separate files to host their configs, and another file will contain settings that are common between them, in the interest of keeping our code DRY.&lt;/p&gt;

&lt;p&gt;Since there will be at least 3 config files involved, they will need to &lt;code&gt;merge&lt;/code&gt; with each other at compile time to render the application. To do this, we need to install a utility package called &lt;code&gt;webpack-merge&lt;/code&gt; to our dev dependencies.&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-merge --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then rename &lt;code&gt;webpack.config.js&lt;/code&gt; to &lt;code&gt;webpack.common.js&lt;/code&gt;. As the name implies, this will contain the common configs. We will create &lt;strong&gt;two&lt;/strong&gt; more files&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;webpack.production.js&lt;/code&gt; --- to contain production env settings&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;webpack.development.js&lt;/code&gt; --- to contain development env settings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While we're on the subject of configuring webpack builds, we will take the opportunity to install a couple of npm packages that will help with our tooling and optimize our builds.&lt;/p&gt;

&lt;p&gt;First, we will install a package called  &lt;a href="https://github.com/johnagan/clean-webpack-plugin" rel="noopener noreferrer"&gt;CleanWebpackPlugin&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 clean-webpack-plugin --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Webpack puts the output bundles and files in the &lt;code&gt;/dist&lt;/code&gt; folder, because that is what we've configured it to do. Over time, this folder tends to become cluttered as we do a build every time (through hot reloading) we make a code change and save. Webpack struggles to keep track of all those files, so It is good practice to clean up the &lt;code&gt;/dist&lt;/code&gt; folder &lt;em&gt;before&lt;/em&gt; each build in order to ensure the proper output files are being used. &lt;code&gt;CleanWebpackPlugin&lt;/code&gt; takes care of that.&lt;/p&gt;

&lt;p&gt;We will install another package called  &lt;a href="https://www.npmjs.com/package/path" rel="noopener noreferrer"&gt;path&lt;/a&gt;. It will allow us to programmatically set entry and output paths inside 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 path --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Now that we have the necessary packages in place to configure a clean, optimized webpack build, lets change &lt;code&gt;webpack.common.js&lt;/code&gt; to contain the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebPackPlugin({
            template: "./src/index.html",
            filename: "./index.html",
        })
    ]
};

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

&lt;/div&gt;



&lt;p&gt;Add the following lines to &lt;code&gt;webpack.development.js&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;const merge = require('webpack-merge');
const common = require('./webpack.common');

module.exports = merge(common, {
    mode: 'development',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist',
        hot: true
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... and these lines to &lt;code&gt;webpack.production.js&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;const merge = require('webpack-merge');
const common = require('./webpack.common');

module.exports = merge(common, {
    mode: 'production'
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a few changes here from its previous iteration that requires explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;webpack.common.js&lt;/code&gt; 

&lt;ul&gt;
&lt;li&gt;Note that we've added an &lt;code&gt;output&lt;/code&gt; property. It renames the bundle file and defines the path to where it can be found.&lt;/li&gt;
&lt;li&gt;We no longer have the dev server definition in here.&lt;/li&gt;
&lt;li&gt;We are making use of &lt;code&gt;CleanWebpackPlugin&lt;/code&gt; to clean up &lt;em&gt;dist&lt;/em&gt; folder&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;webpack.development.js&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The dev server definition has been moved to this file, naturally&lt;/li&gt;
&lt;li&gt;We have enabled &lt;code&gt;source maps&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;webpack.production.js&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;It only contains &lt;em&gt;mode&lt;/em&gt; definition at the moment, but opens up the door to add additional tinkering later on.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;That was a lot of information! We have accomplished a significant step towards setting up the project. Although I have tried my best to explain the concepts and code changes, I would advise additional reading into each of these topics to get a complete grasp. Webpack is a beast --- it might be a stretch even for the smartest developer to completely understand everything in the first read through.&lt;/p&gt;

&lt;p&gt;Let's move on to the next step.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;We will add test frameworks to our code base in this step! There are two frameworks we need to add, one to test functional JS and the other to test React components. They are called Jest and Enzyme, respectively. Once we configure that, we will write a small, uncomplicated JS module and React component to try them out.&lt;/p&gt;

&lt;p&gt;We will set them up and work with them in separate steps. Let's get started!&lt;/p&gt;

&lt;p&gt;We will install &lt;code&gt;Jest&lt;/code&gt; first as a dev dependency, since it is a test framework and it has no use in the production bundle. To install,&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 jest --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will add a file called &lt;code&gt;jest.config.js&lt;/code&gt; to the root directory of our codebase that will dictate how we want to configure our tests. This is the  &lt;a href="https://jestjs.io/docs/en/23.x/configuration" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;  page for Jest that contains details of every piece of configuration --- it is worth giving a read.&lt;/p&gt;

&lt;p&gt;We will not need all the pieces, thus I have condensed the necessary pieces to write our own custom config file. It contains detailed comments on what each piece is doing. This is what  &lt;code&gt;jest.config.js&lt;/code&gt; file will look like for the project we're configuring&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
    // All imported modules in your tests should be mocked automatically
    // automock: false,

    // Stop running tests after the first failure
    // bail: false,

    // Respect "browser" field in package.json when resolving modules
    // browser: false,

    // The directory where Jest should store its cached dependency information
    // cacheDirectory: "C:\\Users\\VenD\\AppData\\Local\\Temp\\jest",

    // Automatically clear mock calls and instances between every test
    clearMocks: true,

    // Indicates whether the coverage information should be collected while executing the test
    // collectCoverage: false,

    // An array of glob patterns indicating a set of files for which coverage information should be collected
    collectCoverageFrom: ['src/tests/*.test.js'],

    // The directory where Jest should output its coverage files
    coverageDirectory: 'src/tests/coverage',

    // An array of regexp pattern strings used to skip coverage collection
    coveragePathIgnorePatterns: [
      "\\\\node_modules\\\\"
    ],

    // A list of reporter names that Jest uses when writing coverage reports
    coverageReporters: [
      "json",
      "text",
      "lcov",
      "clover"
    ],

    // An object that configures minimum threshold enforcement for coverage results
    coverageThreshold: {
        "global": {
            "branches": 80,
            "functions": 80,
            "lines": 80
        }
    },

    // Make calling deprecated APIs throw helpful error messages
    errorOnDeprecated: false,

    // Force coverage collection from ignored files using an array of glob patterns
    // forceCoverageMatch: [],

    // A path to a module which exports an async function that is triggered once before all test suites
    // globalSetup: null,

    // A path to a module which exports an async function that is triggered once after all test suites
    // globalTeardown: null,

    // A set of global variables that need to be available in all test environments
    // globals: {},

    // An array of directory names to be searched recursively up from the requiring module's location
    // moduleDirectories: [
    //   "node_modules"
    // ],

    // An array of file extensions your modules use
    moduleFileExtensions: ['js', 'json', 'jsx'],

    // A map from regular expressions to module names that allow to stub out resources with a single module
    // moduleNameMapper: {},

    // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
    // modulePathIgnorePatterns: [],

    // Activates notifications for test results
    // notify: false,

    // An enum that specifies notification mode. Requires { notify: true }
    // notifyMode: "always",

    // A preset that is used as a base for Jest's configuration
    // preset: null,

    // Run tests from one or more projects
    // projects: null,

    // Use this configuration option to add custom reporters to Jest
    // reporters: undefined,

    // Automatically reset mock state between every test
    resetMocks: false,

    // Reset the module registry before running each individual test
    // resetModules: false,

    // A path to a custom resolver
    // resolver: null,

    // Automatically restore mock state between every test
    restoreMocks: true,

    // The root directory that Jest should scan for tests and modules within
    // rootDir: null,

    // A list of paths to directories that Jest should use to search for files in
    // roots: [
    //   "&amp;lt;rootDir&amp;gt;"
    // ],

    // Allows you to use a custom runner instead of Jest's default test runner
    // runner: "jest-runner",

    // The paths to modules that run some code to configure or set up the testing environment before each test
    // setupFiles: ['&amp;lt;rootDir&amp;gt;/enzyme.config.js'],

    // The path to a module that runs some code to configure or set up the testing framework before each test
    // setupTestFrameworkScriptFile: '',

    // A list of paths to snapshot serializer modules Jest should use for snapshot testing
    // snapshotSerializers: [],

    // The test environment that will be used for testing
    testEnvironment: 'jsdom',

    // Options that will be passed to the testEnvironment
    // testEnvironmentOptions: {},

    // Adds a location field to test results
    // testLocationInResults: false,

    // The glob patterns Jest uses to detect test files
    testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)+(spec|test).js?(x)'],

    // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
    testPathIgnorePatterns: ['\\\\node_modules\\\\'],

    // The regexp pattern Jest uses to detect test files
    // testRegex: "",

    // This option allows the use of a custom results processor
    // testResultsProcessor: null,

    // This option allows use of a custom test runner
    // testRunner: "jasmine2",

    // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
    testURL: 'http://localhost:3030',

    // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
    // timers: "real",

    // A map from regular expressions to paths to transformers
    // transform: {},

    // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
    transformIgnorePatterns: ['&amp;lt;rootDir&amp;gt;/node_modules/'],

    // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
    // unmockedModulePathPatterns: undefined,

    // Indicates whether each individual test should be reported during the run
    verbose: false,

    // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
    // watchPathIgnorePatterns: [],

    // Whether to use watchman for file crawling
    watchman: true,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to our configuration, our tests should live inside a directory called &lt;code&gt;tests&lt;/code&gt; inside &lt;code&gt;/src&lt;/code&gt;. Let's go ahead and create that --- and while we're on the subject of creating directories, lets create &lt;em&gt;three&lt;/em&gt; in total that will allow us to set ourselves up for future steps of the guide&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;tests&lt;/code&gt; - directory that will contain our tests&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;core/js&lt;/code&gt; - we will place our functional JS files here, the likes of helper, utils, services, etc.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;core/scss&lt;/code&gt; - this will contain browser resets, global variable declarations. We will add these in a future piece.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright, we are making progress !! Now that we have a sweet test setup, let's create a simple JS module called &lt;code&gt;multiply.js&lt;/code&gt; inside &lt;code&gt;core/js&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;const multiply = (a, b) =&amp;gt; {
    return a* b;
};

export default multiply;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... and write tests for it, by creating a file called &lt;code&gt;multiply.spec.js&lt;/code&gt; inside &lt;code&gt;tests&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import multiply from '../core/js/multiply';

describe('The Multiply module test suite', () =&amp;gt; {
    it('is a public function', () =&amp;gt; {
        expect(multiply).toBeDefined();
    });

    it('should correctly multiply two numbers', () =&amp;gt; {
        const expected = 6;
        const actual1 = multiply(2, 3);
        const actual2 = multiply(1, 6);

        expect(actual1).toEqual(expected);
        expect(actual2).toEqual(expected);
    });

    it('should not multiply incorrectly', () =&amp;gt; {
        const notExpected = 10;
        const actual = multiply(3, 5);

        expect(notExpected).not.toEqual(actual);
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final piece of configuration is to add a script in our &lt;code&gt;package.json&lt;/code&gt; that will run all our tests. It will live inside the &lt;code&gt;scripts&lt;/code&gt; property&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "test": "jest",
    "build": "webpack --config webpack.production.js",
    "start": "webpack-dev-server --open --config webpack.development.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we run &lt;code&gt;npm run test&lt;/code&gt; in our terminal (inside the root directory of the project), it will run all our tests and produce and output like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1586373323504%2FhHuACv12J.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1586373323504%2FhHuACv12J.png" alt="Screen Shot 2020-04-08 at 3.15.12 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can keep adding more modules and test suites in similar manner.&lt;/p&gt;

&lt;p&gt;Let's move on to the next step !&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;It's time to install Enzyme and test our React components! We need to install a version of Enzyme that corresponds to the version of React we're using, which is &lt;strong&gt;16&lt;/strong&gt;. In order to do that, we need to do the following, keeping in mind that this tool will also be installed as a dev dependency because like Jest, the test framework does not need to be compiled to production bundle&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 enzyme enzyme-adapter-react-16 --save dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will create &lt;code&gt;enzyme.config.js&lt;/code&gt; at the root directory of the project, similar to what we did for Jest. This is what that file should look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you go take a look at line 119 in &lt;code&gt;jest.config.js&lt;/code&gt;, you will see that we have done ourselves a favor by preparing for this moment where we setup Enzyme to work with Jest. All that needs to be done is uncomment line 119 and our setup will be complete!&lt;/p&gt;

&lt;p&gt;Let's write a test for the &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; component to see if what we've set up is working. Create a directory called &lt;code&gt;components&lt;/code&gt; inside &lt;code&gt;tests&lt;/code&gt; --- this will hold all the tests for the components you will write in the future. The separate directory is created to keep functional and component tests separate. This segregation can be done in any way, as long as all the tests live inside the &lt;code&gt;src/tests&lt;/code&gt; directory. It will help in the future when the app starts to grow.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;src/tests/components&lt;/code&gt; directory, create a file called &lt;code&gt;App.spec.js&lt;/code&gt; and add the following lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { shallow} from 'enzyme';

import App from '../../components/App';

describe('The App component test suite', () =&amp;gt; {
    it('should render component', () =&amp;gt; {
        expect(shallow(&amp;lt;App /&amp;gt;).contains(&amp;lt;div&amp;gt;Hello World&amp;lt;/div&amp;gt;)).toBe(true);
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run our test script in the terminal, you will see this test is running and passing !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1586459620589%2FdHmcc2BOz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1586459620589%2FdHmcc2BOz.png" alt="Screen Shot 2020-04-09 at 3.13.21 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note:&lt;/strong&gt; &lt;em&gt;In step 2 and 3, we have simply set up Jest and Enzyme to work together in our code base. To demonstrate that the setup is working, we have written two overly simple tests. The art of writing good tests is an entirely different ball game and these tests should not be taken as any form of guide/direction.&lt;/em&gt;&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 4
&lt;/h2&gt;

&lt;p&gt;In this part of the guide, we will configure our code base to lend &lt;code&gt;.scss&lt;/code&gt; support. However, before we can learn to run, we need to learn to walk --- that means we will have to get css to load first.&lt;/p&gt;

&lt;p&gt;Let's go grab the necessary npm 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 install css-loader style-loader --save-dev
&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;npm install node-sass sass-loader --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In the explanation block below, you can click the names of the tools that appear like &lt;code&gt;this&lt;/code&gt; to visit their official documentation.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://webpack.js.org/loaders/css-loader/" rel="noopener noreferrer"&gt;&lt;code&gt;css-loader&lt;/code&gt;&lt;/a&gt; is a webpack plugin that interprets and resolves syntax like &lt;code&gt;@import&lt;/code&gt; or &lt;code&gt;url()&lt;/code&gt; that are used to include &lt;code&gt;.scss&lt;/code&gt; files in components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://webpack.js.org/loaders/style-loader/" rel="noopener noreferrer"&gt;&lt;code&gt;style-loader&lt;/code&gt;&lt;/a&gt; is a webpack plugin that injects the compiled css file in the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/sass/node-sass" rel="noopener noreferrer"&gt;&lt;code&gt;node-sass&lt;/code&gt;&lt;/a&gt; is a Node.js library that binds to a popular stylesheet pre-processor called  &lt;a href="https://github.com/sass/libsass" rel="noopener noreferrer"&gt;&lt;code&gt;LibSass&lt;/code&gt;&lt;/a&gt; . It lets us natively compile &lt;code&gt;.scss&lt;/code&gt; files to css in a node environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://webpack.js.org/loaders/sass-loader/" rel="noopener noreferrer"&gt;&lt;code&gt;sass-loader&lt;/code&gt;&lt;/a&gt; is a webpack plugin that will allow us to use Sass in our project.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have installed the necessary npm packages, we need to tell webpack to make use of them. Inside &lt;code&gt;webpack.common.js&lt;/code&gt;, add the following lines in the &lt;code&gt;rules&lt;/code&gt; array just below where we're using &lt;code&gt;babel-loader&lt;/code&gt; and &lt;code&gt;html-loader&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;{
    test: /\.s[ac]ss$/i,
    use: [
        // Creates `style` nodes from JS strings
        'style-loader',
        // Translates CSS into CommonJS
        'css-loader',
        // Compiles Sass to CSS
        'sass-loader',
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The setup is complete ! Let's write some sass !!&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;src/components&lt;/code&gt; directory, create a file called &lt;code&gt;App.scss&lt;/code&gt; and add the following lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#app-container {
    letter-spacing: 1px;
    padding-top: 40px;

    &amp;amp; &amp;gt; div {
        display: flex;
        font-size: 25px;
        font-weight: bold;
        justify-content: center;
        margin: 0 auto;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The explanation of sass syntax is beyond the scope of this article. This is an excellent  &lt;a href="https://blog.teamtreehouse.com/the-absolute-beginners-guide-to-sass" rel="noopener noreferrer"&gt;resource&lt;/a&gt;  for beginners to learn more in depth.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, save the file and boot up the project by running  &lt;code&gt;npm run start&lt;/code&gt;. The application should load with the style rules we just wrote.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 5
&lt;/h2&gt;

&lt;p&gt;It's time to install ESLint. Similar to what we've been doing so far, we need to install a few npm packages and then add a config file to our code base. This is a tool that is needed purely for development purpose, so we will install it as a dev dependency.&lt;/p&gt;

&lt;p&gt;Let's get started !&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 eslint eslint-config-airbnb-base eslint-plugin-jest --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;eslint-config-airbnb-base&lt;/code&gt; is the airbnb style guide we're asking &lt;code&gt;eslint&lt;/code&gt; to apply on our project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;eslint-plugin-jest&lt;/code&gt; is the eslint plugin for &lt;code&gt;jest&lt;/code&gt; test framework.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The airbnb style guide has peer dependencies that needs to be installed as well. You can input&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm info "eslint-config-airbnb@latest" peerDependencies
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in your terminal and list them, however, to install, do the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx install-peerdeps --dev eslint-config-airbnb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to create a file called &lt;code&gt;.eslintrc.json&lt;/code&gt; (note the &lt;code&gt;.&lt;/code&gt; at the beginning, indicating it's a hidden file)at the root directory of the project, similar to how the other config files (webpack, jest, enzyme, babel) have been added,&lt;/p&gt;

&lt;p&gt;... and add these lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "extends": "airbnb",
    "plugins": ["jest"],
    "env": {
      "browser": true,
      "jest": true
    },
    "rules": {
      "arrow-body-style": [2, "always"],
      "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
      "no-unused-expressions": "off",
      "max-len": "off",
      "import/no-extraneous-dependencies": "off",
      "react/destructuring-assignment": "off",
      "react/prop-types": "off"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The  &lt;a href="https://eslint.org/docs/user-guide/configuring#top" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; is a good read if you're looking to understand in details how configuring ESLint works. The most pertinent lines of code in that file is the &lt;code&gt;rules&lt;/code&gt; object --- here we're basically overriding &lt;em&gt;some&lt;/em&gt; of the rules from the style guide to suit the specific needs of our project. These are not set in stone, so please feel free to play with them to best suit &lt;em&gt;your&lt;/em&gt; needs, but it's probably not a good idea to override &lt;em&gt;too many&lt;/em&gt; of the rules --- that defeats the purpose of using a style guide in the first place.&lt;/p&gt;

&lt;p&gt;Let's add a script to &lt;code&gt;package.json&lt;/code&gt; that will apply the airbnb style guide to our code base. We need to tell Eslint what files and/or directories we would like it to scan --- so we will tell it to scan all JS files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"lint": "eslint '**/*.js' --ignore-pattern node_modules"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you run &lt;code&gt;npm run lint&lt;/code&gt; in your terminal, eslint will scan the file types and patterns specified in the script and display a list of issues. Fair warning, the project will have &lt;em&gt;quite a few&lt;/em&gt; errors, but if you're using popular code editors like IDEA products, Visual Studio Code, Sublime, etc, they provide out of the box support to fix &lt;em&gt;most&lt;/em&gt; of these issues in one quick stroke (format document).&lt;/p&gt;

&lt;p&gt;*If the large number of errors is proving to be a hindrance to your learning, please feel free to uninstall ESLint by running &lt;code&gt;npm uninstall eslint eslint-config-airbnb-base eslint-plugin-jest --save-dev&lt;/code&gt; *&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 6
&lt;/h2&gt;

&lt;p&gt;We're almost done with setting up our project --- the finish line is within our sights !! In this last step, we will configure our project to make use of various static assets like images, SVGs, icons and custom typefaces.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom Typefaces
&lt;/h3&gt;

&lt;p&gt;Any respectable front end setup should have varying fonts displaying information on the page. The weight of the font, along with its size, is an indicator of the context of the text being displayed --- for instance, page or section headers tend to be &lt;em&gt;larger&lt;/em&gt; and &lt;em&gt;bolder&lt;/em&gt;,  while helper texts are often &lt;em&gt;smaller&lt;/em&gt;, &lt;em&gt;lighter&lt;/em&gt; and may even be in &lt;em&gt;italics&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There are multiple ways of pulling in custom fonts into an application. Large enterprise code bases usually buy licenses to fonts and have its static assets as part of the the server that hosts the application. The process to do that is slightly complicated --- we need a dedicated piece to walk through that.&lt;/p&gt;

&lt;p&gt;The most convenient way of using custom fonts is to use a public domain library that has a large collection and hosted on a CDN (Content Delivery Network), like  &lt;a href="https://fonts.google.com/" rel="noopener noreferrer"&gt;Google Fonts&lt;/a&gt;. It is convenient because all we need to do is, select a couple of fonts we like and then simply embed their &lt;code&gt;url&lt;/code&gt; in our static markup &lt;code&gt;index.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;...and we're good to go !! So let's get started. For our purposes, we shall use &lt;code&gt;Roboto Mono&lt;/code&gt; font family.&lt;/p&gt;

&lt;p&gt;Open up &lt;code&gt;index.html&lt;/code&gt; and paste the following stylesheet &lt;code&gt;link&lt;/code&gt; in the &lt;code&gt;head&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;&amp;lt;link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're done. Now we can use &lt;code&gt;font-family: 'Roboto Mono'&lt;/code&gt; in any of our &lt;code&gt;.scss&lt;/code&gt; files. We can use any number of fonts in this way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Images
&lt;/h3&gt;

&lt;p&gt;Images, like fonts, are an essential part of a front end setup. In order to enable our project to utilize images in the application, we need to install a loader for webpack. This step is identical to what we've done multiple times in this guide --- install the loader and add a few lines to webpack config to make use of 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 install url-loader --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... then add the following lines to the &lt;code&gt;rules&lt;/code&gt; array in &lt;code&gt;webpack.common.js&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;...
{
  test: /\.(jpg|png)$/,
  use: {
    loader: 'url-loader',
  },
},
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The project is now ready to use images of type &lt;code&gt;.jpg&lt;/code&gt; and &lt;code&gt;.png&lt;/code&gt; . To demonstrate, create a &lt;code&gt;public/images&lt;/code&gt; folder at the root directory of the project. Then add &lt;em&gt;any&lt;/em&gt; image to the subdirectory &lt;code&gt;images&lt;/code&gt;. For our purposes, I downloaded a free image from Unsplash and named it &lt;code&gt;coffee.png&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we will create a directory inside &lt;code&gt;src/components&lt;/code&gt; called Image --- then create the &lt;code&gt;Image&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Image.js&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;import React from 'react';

const Image = (props) =&amp;gt; {
  return (
    &amp;lt;img
      src={props.src}
      alt={props.alt}
      height={props.height}
      wdth={props.wdth}
    /&amp;gt;
  );
};

export default Image;

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

&lt;/div&gt;



&lt;p&gt;Then, import both the &lt;code&gt;Image&lt;/code&gt; component and the actual image &lt;code&gt;coffee.png&lt;/code&gt; in &lt;code&gt;App.js&lt;/code&gt;. At this point, we will have to make minor edits to the &lt;code&gt;App.js&lt;/code&gt; to use the image&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import './App.scss';

// component imports
import Image from './Image/Image';

// other imports
import coffee from '../../public/images/coffee.png';

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;span&amp;gt;Hello World&amp;lt;/span&amp;gt;
      &amp;lt;Image
        src={coffee}
        alt="hero"
        height="400"
        width="400"
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default App;

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

&lt;/div&gt;



&lt;p&gt;Now, if you start the application, you will see the image is being loaded on the page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That concludes our step by step guide to setting up a modern React project from scratch. There was a lot of information to digest here, but to think of it, we have also come a long way from the minimal setup we did earlier. I hope the guide has been helpful in learning some key concepts in the area of modern front end setup tooling.&lt;/p&gt;

&lt;p&gt;The future pieces I have planned for this series are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn the basics of containerization and how to deploy this project in a container.&lt;/li&gt;
&lt;li&gt;Add bonus features to our project, like JS docs, comprehensive test runner outputs (with colors and coverage percentages !), more &lt;code&gt;package.json&lt;/code&gt; scripts and global scss stylesheets like resets and variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please feel free to leave a comment and share among your friends. I will see you in the next piece !&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The repo for the advanced setup can be found  &lt;a href="https://github.com/all-things-javascript/advanced-react-webpack-babel" rel="noopener noreferrer"&gt;here&lt;/a&gt; .&lt;/em&gt;&lt;/p&gt;

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

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://webpack.js.org/guides/production/" rel="noopener noreferrer"&gt;Webpack Environment Variables and Configuration&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://webpack.js.org/guides/output-management/" rel="noopener noreferrer"&gt;Webpack Output Management&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="https://webpack.js.org/loaders/sass-loader/" rel="noopener noreferrer"&gt;Sass-loader&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://blog.teamtreehouse.com/the-absolute-beginners-guide-to-sass" rel="noopener noreferrer"&gt;The Absolute Beginners Guide to Sass&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://eslint.org/docs/user-guide/configuring#top" rel="noopener noreferrer"&gt;ESLint configuration&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://developers.google.com/fonts/docs/getting_started" rel="noopener noreferrer"&gt;Google Web Fonts - Get Started&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>webpack</category>
      <category>babel</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Beginners Guide to Minimal React + Webpack 4 + Babel 7 Project Setup</title>
      <dc:creator>Nasidul Islam</dc:creator>
      <pubDate>Fri, 13 Mar 2020 15:48:15 +0000</pubDate>
      <link>https://dev.to/nasidulislam/beginners-guide-to-minimal-react-webpack-4-babel-7-project-setup-4dh0</link>
      <guid>https://dev.to/nasidulislam/beginners-guide-to-minimal-react-webpack-4-babel-7-project-setup-4dh0</guid>
      <description>&lt;p&gt;React has become the &lt;a href="https://2019.stateofjs.com/front-end-frameworks/"&gt;leading frontend library of choice&lt;/a&gt;  among developers in recent years. The best way to learn and practice any programming language tool is to build stuff with it --- in order to so we need to setup a coding playground. The fastest way to get started with such a project is to bootstrap it with &lt;code&gt;create-react-app (CRA)&lt;/code&gt; . The reason why such a thing exists is because modern Javascript tooling has become an incredibly vast space (hilariously explained  &lt;a href="https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f"&gt;here&lt;/a&gt; ). Starter kits such as CRA helps us avoid navigating that landscape and focus on coding the fun stuff. However, it is still essential as developers to know what are some of the key pieces of configuring a project. We will learn just that in this step-by-step guide for a minimal React project setup.&lt;/p&gt;

&lt;h4&gt;
  
  
  We will learn the following in this tutorial:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;basic concepts of Webpack and leverage that to setup our project&lt;/li&gt;
&lt;li&gt;what is Babel, what does it do and why do we need it&lt;/li&gt;
&lt;li&gt;how to integrate React into our project&lt;/li&gt;
&lt;li&gt;how to enable hot module reload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started by learning the basics of the tools we will use today to configure our first project.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  React JS
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt;  is a javascript library that has brought about a paradigm shift when it comes to building user interfaces. Developed and maintained by Facebook, it views the world through the lens of small, reusable components. Imagine a Lego set, where we put together blocks to create a structure. Similarly, React components don't mean a whole lot on their own, but when assembled in a systematic way through the hands of an experienced developer, it can become a powerful tool to build beautiful and complex user experiences.&lt;/p&gt;

&lt;h5&gt;
  
  
  Key Features
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Simple, reusable components&lt;/li&gt;
&lt;li&gt;Declarative. Code is more readable and easier to debug. &lt;/li&gt;
&lt;li&gt;One way data binding. Data strictly flows from parent component to children component(s)&lt;/li&gt;
&lt;li&gt;Virtual DOM. Algorithms calculate the "diff" between current and updated DOM (through user interaction, data received from API calls, etc) to only re-render portions of the view that needs to be updated.&lt;/li&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  Webpack
&lt;/h4&gt;

&lt;p&gt;The official site for  &lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt; describes itself to be a static module bundler --- but I believe it is selling itself short. Yes, it is &lt;em&gt;that&lt;/em&gt; in the simplest sense, but it can do so much more. Webpack is an incredibly configurable tool that packages all the front end code (js, css, templating engines, static assets) and creates bundles that the browser can download to give your application functionality.&lt;/p&gt;

&lt;h5&gt;
  
  
  Key Features
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Module bundler. Packages front end code into bundles for browser to download.&lt;/li&gt;
&lt;li&gt;Code splitting. The bundles can be broken down further based on the page, functionality etc, so that the browser downloads as less code as possible to run the application.&lt;/li&gt;
&lt;li&gt;Tree shaking. If configured to do so, it will not package dead/unreachable code in the bundle.&lt;/li&gt;
&lt;li&gt;Loaders and plugins. Allows us to extend on the out of the box functionality provided by Webpack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hot Module Replacement&lt;/strong&gt; (HMR). During development, Webpack can swap out JS or CSS modules on the fly to update the UI without refreshing the page.&lt;/li&gt;
&lt;li&gt;Environment specific configurations. For instance, we don't want to minify code in development environment but we absolutely want to do so in production.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  Babel
&lt;/h4&gt;

&lt;p&gt;Babel is a javascript transpiler --- it compiles modern JS code into older versions so that older browsers can execute them. The latest versions of most browsers can parse through newer JS features, but legacy browsers can't. We don't want our application to fail to run on those browsers, and &lt;em&gt;that&lt;/em&gt; is where Babel saves the day.&lt;/p&gt;

&lt;h5&gt;
  
  
  Key Features
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Compiles modern JS code into older versions of JS that older browsers can execute.&lt;/li&gt;
&lt;li&gt;Allows plugins presets to extend on top of its out of the box functionality.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Alright, now that we've learned the core concepts of the tools we're about to use, let's write some code!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note:&lt;/strong&gt; &lt;em&gt;It's worth mentioning here that the &lt;em&gt;node&lt;/em&gt; version in your machine must be 10.15.3 or higher. Check by typing &lt;code&gt;node -v&lt;/code&gt; in your terminal. If you need an update, or don't have node installed at all, you can get the installer &lt;a href="https://nodejs.org/en/"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Step 1:
&lt;/h4&gt;

&lt;p&gt;Open up your terminal and navigate to a directory where you would like to create your repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/Desktop/projects
mkdir minimal-react-webpack-babel &amp;amp;&amp;amp; cd $_
mkdir -p src
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will create a directory named "minimal-react-webpack-babel" where our code will live and a "src" subdirectory inside it.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Step 2:
&lt;/h4&gt;

&lt;p&gt;We need to initialize &lt;strong&gt;package.json&lt;/strong&gt; at this point&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will create the file in one go, with most fields being empty (like description and author) and other set to default (like version set to 1.0.0). You can come back later to fill them out appropriately.  &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;package.json&lt;/strong&gt; should look like this now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "minimal-react-webpack-babel",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

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



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

&lt;h4&gt;
  
  
  Step 3:
&lt;/h4&gt;

&lt;p&gt;Let's install &lt;strong&gt;webpack&lt;/strong&gt; and &lt;strong&gt;webpack-cli&lt;/strong&gt; in our project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;Pre &lt;em&gt;v4.0.0&lt;/em&gt; of Webpack used to look for a configuration file to run, but since version 4.0.0, Webpack is able to bundle packages out of the box.  &lt;/p&gt;

&lt;p&gt;In a later tutorial, I plan to demonstrate a more advanced setup for Webpack (environment specific configs, linters, test runners, etc) that will build on top of what we're learning today. We will leave it in its bare bones state for now.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Step 4:
&lt;/h4&gt;

&lt;p&gt;Time to install &lt;strong&gt;react&lt;/strong&gt; and &lt;strong&gt;react-dom&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react react-dom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;React should be installed as a regular dependency and not as &lt;code&gt;devDependencies&lt;/code&gt; because we need react available at application runtime and packaged into the production bundle of the application.&lt;/p&gt;

&lt;p&gt;Alright, that was quick and easy! Let's move on.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Step 5:
&lt;/h4&gt;

&lt;p&gt;We have to install &lt;strong&gt;Babel&lt;/strong&gt; here, along with other necessary presets.&lt;br&gt;
&lt;/p&gt;

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



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: React is written using modern ES6 features that older browsers cannot execute. Therefore, we need to somehow convert the modern syntax into ES5. Webpack cannot perform this action --- this is where Babel steps in to bridge the gap. This process is called &lt;em&gt;transpiling&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;We installed three dependencies using the command above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;babel/core babel-loader&lt;/code&gt; : the interface between babel and webpack. It allows them to work with each other to produce the final bundle.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;babel/preset-env&lt;/code&gt;: a preset responsible for transpiling ES6 (or above) to ES5.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;babel/preset-react&lt;/code&gt;: a present responsible for compiling JSX to regular JS. It is possible to forego installing this dependency, but then we will not be able to write our React components using JSX.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Great, now that we understand that, let's configure &lt;em&gt;babel&lt;/em&gt; to use the presets we just installed. First, we will need to create a file called &lt;code&gt;.babelrc&lt;/code&gt; at the root of the project --- this will contain the config rules.&lt;br&gt;
&lt;/p&gt;

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



&lt;p&gt;This is going to be a hidden file. Open up the file using:&lt;br&gt;
&lt;/p&gt;

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



&lt;p&gt;... and add the following lines and hit save:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;h4&gt;
  
  
  Step 6:
&lt;/h4&gt;

&lt;p&gt;Let's install a &lt;em&gt;dev-server&lt;/em&gt; now. Technically, it is possible move forward without the dev-server, but that would mean we will have to &lt;em&gt;build&lt;/em&gt; our project every time we make code changes to it. That is not the world we want to live in --- we want our application to update as soon as we save our code changes. This process is called &lt;strong&gt;Hot Module Replacement&lt;/strong&gt;. In addition to installing the dev-server, slight tweak to the webpack config will enable this feature for us.&lt;br&gt;
&lt;/p&gt;

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



&lt;p&gt;Now, in order to have webpack &lt;em&gt;build&lt;/em&gt; our code, we will need to add a script inside the &lt;code&gt;scripts&lt;/code&gt; property of &lt;strong&gt;package.json&lt;/strong&gt;. This will compile our code in &lt;code&gt;production&lt;/code&gt; mode.&lt;/p&gt;

&lt;p&gt;We will also add a &lt;em&gt;start&lt;/em&gt; script that will spin up our dev server and enable seamless development environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --open --mode development"
  },
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;h4&gt;
  
  
  Step 7:
&lt;/h4&gt;

&lt;p&gt;The last dependencies we need to install in our project are &lt;code&gt;html-webpack-plugin&lt;/code&gt; and &lt;code&gt;html-loader&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

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



&lt;ul&gt;
&lt;li&gt; &lt;a href="https://webpack.js.org/plugins/html-webpack-plugin/"&gt;html-webpack-plugin&lt;/a&gt;  will generate the HTML from the React components we're about to write.&lt;/li&gt;
&lt;li&gt; &lt;a href="https://webpack.js.org/loaders/html-loader/"&gt;html-loader&lt;/a&gt; exports the HTML as string &lt;em&gt;and&lt;/em&gt; can minimize it if we ask it to.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  Step 8:
&lt;/h4&gt;

&lt;p&gt;We're done with installing dependencies!! What we need to do now is create an HTML file where React will mount our application. Let's call it &lt;code&gt;index.html&lt;/code&gt; and put it inside the &lt;code&gt;src&lt;/code&gt; directory we created earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;title&amp;gt;Minimal React + Webpack 4 + Babel 7 project setup&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;div id="app-container"&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; &lt;/p&gt;

&lt;h4&gt;
  
  
  Step 9:
&lt;/h4&gt;

&lt;p&gt;At this point in time, we will add minimal config rules to &lt;strong&gt;webpack&lt;/strong&gt;. We will do so by creating a file called &lt;code&gt;webpack.config.js&lt;/code&gt; and put &lt;em&gt;all&lt;/em&gt; our rules there. In a future tutorial, we will build on top of what we are learning today and create &lt;em&gt;separate&lt;/em&gt; config files for development and production environments. This will allow us to segregate environment specific rules, promoting scalability and maintainability.&lt;/p&gt;

&lt;p&gt;Let's create that file at project root&lt;br&gt;
&lt;/p&gt;

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



&lt;p&gt;... and add the following lines to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
    devServer: {
        contentBase: './dist',
        hot: true
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/index.html",
            filename: "./index.html"
        })
    ]
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: These lines are telling &lt;strong&gt;webpack&lt;/strong&gt; to do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;devServer&lt;/em&gt; contains config rules for the server instance we will run to host our application using dev-server. &lt;code&gt;hot: true&lt;/code&gt; enables hot module replacement.&lt;/li&gt;
&lt;li&gt;pipe &lt;em&gt;all&lt;/em&gt; files with an extension of &lt;code&gt;.js&lt;/code&gt; or &lt;code&gt;.jsx&lt;/code&gt; through &lt;strong&gt;babel-loader&lt;/strong&gt;, with the exception of files inside &lt;code&gt;node_modules&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;use the html plugin and loader we installed in the previous step to generate HTML from React components and the front end packaged code bundle &lt;em&gt;and&lt;/em&gt; inject the bundle in a &lt;code&gt;&amp;lt;script/&amp;gt;&lt;/code&gt; tag in the HTML.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  Step 10:
&lt;/h4&gt;

&lt;p&gt;We've reached the end of our setup process -- we're now ready to write some React !!&lt;/p&gt;

&lt;p&gt;Let's create a &lt;em&gt;components&lt;/em&gt; directory inside &lt;code&gt;src&lt;/code&gt;, and write a simple &lt;code&gt;App.js&lt;/code&gt; component that prints "Hello World".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';

const App = () =&amp;gt; {
    return (
        &amp;lt;div&amp;gt;Hello World&amp;lt;/div&amp;gt;
    )
};

export default App;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also need to create &lt;code&gt;index.js&lt;/code&gt; inside &lt;code&gt;src&lt;/code&gt; directory --- and import the component we just wrote.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from "react-dom";
import App from './components/App.js';

const $container = document.getElementById("app-container");
$container ? ReactDOM.render(&amp;lt;App /&amp;gt;, $container) : false;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The process is complete --- we've a React project using babel and webpack all on our own!! Now, if we run &lt;code&gt;npm run start&lt;/code&gt;, webpack will spin up the dev server and open up our application in a browser window. Additionally, since we've enabled hot module replacement, if we make code changes to our application (edit existing "hello world" component, add new components, etc), we will immediately see our application update in the browser!&lt;/p&gt;

&lt;p&gt;The finished repo is  &lt;a href="https://github.com/all-things-javascript/minimal-react-webpack-babel"&gt;here&lt;/a&gt;.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There is little doubt that &lt;code&gt;create-react-app&lt;/code&gt; is the quickest and most convenient way to setup a react project (at the moment). However, it is an incredibly valuable skill to know how modern javascript tooling works --- and how we can piece these tools together to setup a front end project from scratch.&lt;/p&gt;

&lt;p&gt;I hope this step by step guide has helped you understand some of that. Thank you for reading and stay tuned for more !!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webpack</category>
      <category>babel</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
