<?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: Matheus Alves</title>
    <description>The latest articles on DEV Community by Matheus Alves (@matheus4lves).</description>
    <link>https://dev.to/matheus4lves</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%2F1094572%2F7d721db1-ef9a-4d95-b469-3393b51f1446.jpeg</url>
      <title>DEV Community: Matheus Alves</title>
      <link>https://dev.to/matheus4lves</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matheus4lves"/>
    <language>en</language>
    <item>
      <title>How to Install MongoDB with OpenSSL 3 Support on Fedora 39</title>
      <dc:creator>Matheus Alves</dc:creator>
      <pubDate>Mon, 03 Jun 2024 18:14:52 +0000</pubDate>
      <link>https://dev.to/matheus4lves/how-to-install-mongodb-with-openssl-3-support-on-fedora-39-4nii</link>
      <guid>https://dev.to/matheus4lves/how-to-install-mongodb-with-openssl-3-support-on-fedora-39-4nii</guid>
      <description>&lt;p&gt;If you're still having troubles running MongoDB + OpenSSL3 on Fedora, learn how to install it properly by checking my first article as a freeCodeCamp Publication Contributor: &lt;a href="https://www.freecodecamp.org/news/how-to-install-mongodb-with-openssl-3-support-on-fedora-39/"&gt;How to Install MongoDB with OpenSSL 3 Support on Fedora 39&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>fedora</category>
      <category>linux</category>
    </item>
    <item>
      <title>Three Sentences That Changed My Way of Approaching Web Development.</title>
      <dc:creator>Matheus Alves</dc:creator>
      <pubDate>Mon, 04 Dec 2023 15:17:42 +0000</pubDate>
      <link>https://dev.to/matheus4lves/three-sentences-that-changed-my-way-of-approaching-web-development-4hge</link>
      <guid>https://dev.to/matheus4lves/three-sentences-that-changed-my-way-of-approaching-web-development-4hge</guid>
      <description>&lt;p&gt;Web development is not an easy thing and sometimes it can be daunting, even to the point we start to question ourselves with questions like this: "Is it really for me?"&lt;/p&gt;

&lt;p&gt;But I believe that you're probably a capable person who is struggling because you're approaching it the wrong way. Hence, here are a few sentences that helped (and still help) me to become a better developer:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The wisdom they provide can be applied to development in general and even to your life. So, let it settle in your mind!&lt;/p&gt;

&lt;h2&gt;
  
  
  Number #1
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"Trust the process."&lt;br&gt;
― Natalie Sideserf&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Programming is a &lt;em&gt;process&lt;/em&gt;, and a process can be understood as a series of steps that must be taken toward a goal. They cannot be skipped, and trying to do that will probably cause lots of headaches.&lt;/p&gt;

&lt;p&gt;For instance, you may skip one step because it demands a prerequisite knowledge you deem you have but you know you don't. Then, you end up wasting time because, instead of implementing the right solution (not the perfect, a good solution) from the beginning, you keep guessing and trying and finding that most attempts were a failure and, when you come up with something that works, it is a subpar solution.&lt;/p&gt;

&lt;p&gt;Therefore, &lt;em&gt;understand&lt;/em&gt; and &lt;em&gt;trust process&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/@SideserfCakes"&gt;Natalie Sideserf&lt;/a&gt; makes hyper-realistic cakes and she said that when she was talking about how sometimes she felt she would not be able to bring to life one of her cake ideas, but then she remembered that, if she followed the process she learned, she would be able to achieve the desired result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Number #2
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"Before starting, stay calm and slow down, don't just jump at a problem without a plan. Also, take a very logical and rational approach."&lt;br&gt;
— Jonas Schmedtmann&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is something I have to remember constantly, at least up until now. I have to confess that I'm inclined to get anxious when I have to deal with a complex project.&lt;/p&gt;

&lt;p&gt;However, since I started training myself to stay calm, to plan and to break big complex tasks into smaller simpler ones I noticed great improvements.&lt;/p&gt;

&lt;p&gt;Yes, you read it right. This is something that requires training. You won't be able to change the way you approach things overnight. As soon as you note you're approaching a problem the wrong way stop and follow these tips.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/user/jonasschmedtmann/"&gt;Jonas Schmedtmann&lt;/a&gt; describes himself as a&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"[...] full-stack web developer and designer with a passion for building beautiful web interfaces from scratch."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Besides he's been designing and coding for more than a decade. He also has a Master's degree in Engineering.&lt;/p&gt;

&lt;p&gt;With seven courses and almost two million students just on Udemy, I'm sure he applies these tips to his life.&lt;/p&gt;

&lt;h2&gt;
  
  
  Number #3
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"The master has failed more times than the beginner has even tried."&lt;br&gt;
— Stephen McCranie&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A few years ago I was trying to learn Java and found some free courses on the web. I got enrolled in a Udemy course that I believed was offered by Google. In one of the videos one of the instructors cited this phrase.&lt;/p&gt;

&lt;p&gt;It stuck with me for a simple reason: I'm a perfectionist. Besides, I don't know when, but several years ago I came to believe that the student must surpass his teacher.&lt;/p&gt;

&lt;p&gt;This is a good thing. But it can become a terrible mindset where you can't get things done because you can't make them perfect. The truth is: there's no perfection in this world. And that's a good thing if you think about it.&lt;/p&gt;

&lt;p&gt;Therefore, remember that the path to success is full of failures. Learn with them, don't let them let you down. Learn to fail smart. By doing these things you'll become a master!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I'm not sure this is the author of this sentence. &lt;em&gt;Stephen McCranie&lt;/em&gt; is the result of a Google search. The problem is that some results indicate he's a cartoonist and others an author. Because of that, I can't provide any detailed information about him.&lt;/p&gt;

&lt;p&gt;I hope these sentences help you too. See you in the next post!&lt;/p&gt;

&lt;p&gt;Cover image by &lt;a href="http://www.nyphotographic.com/"&gt;Nick Youngson&lt;/a&gt; &lt;a rel="license" href="https://creativecommons.org/licenses/by-sa/3.0/"&gt;CC BY-SA 3.0&lt;/a&gt; &lt;a href="http://pix4free.org/"&gt;Pix4free&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How are HTML elements stacked by default?</title>
      <dc:creator>Matheus Alves</dc:creator>
      <pubDate>Tue, 15 Aug 2023 18:21:27 +0000</pubDate>
      <link>https://dev.to/matheus4lves/how-html-elements-are-stacked-by-default-41fe</link>
      <guid>https://dev.to/matheus4lves/how-html-elements-are-stacked-by-default-41fe</guid>
      <description>&lt;p&gt;In today's article you'll to learn how HTML elements are stacked by &lt;em&gt;default&lt;/em&gt;. Therefore, I'll NOT teach you how to change the stacking order of elements (you may use the &lt;code&gt;z-index&lt;/code&gt; CSS property for this purpose) but only teach you how things work "naturally".&lt;/p&gt;

&lt;p&gt;I believe understanding how element stacking works by default will make it easier for you to learn how to control it, which I'll cover in a future article.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Important terms&lt;/li&gt;
&lt;li&gt;The default stacking order&lt;/li&gt;
&lt;li&gt;Demo&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Important terms &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;z-axis&lt;/strong&gt;: You may recall from highschool math classes the &lt;em&gt;3D coordinate system&lt;/em&gt;, which is formed by the &lt;em&gt;x&lt;/em&gt;, &lt;em&gt;y&lt;/em&gt; and &lt;em&gt;z&lt;/em&gt; axes. In this system, if you combine two axes you create a two-dimensional &lt;em&gt;plane&lt;/em&gt;. The remaining axis will always be perpendicular to the plane and form with it the 3D coordinate system. Consider de image below&lt;/p&gt;

&lt;a title="I, Sakurambo, CC BY-SA 3.0 &amp;lt;http://creativecommons.org/licenses/by-sa/3.0/&amp;gt;, via Wikimedia Commons" href="https://commons.wikimedia.org/wiki/File:3D_coordinate_system.svg"&gt;&lt;img alt="3D coordinate system" src="https://res.cloudinary.com/practicaldev/image/fetch/s--IPUPLF1h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/3D_coordinate_system.svg/256px-3D_coordinate_system.svg.png" width="256" height="256"&gt;&lt;/a&gt;3D coordinate system

&lt;p&gt;You can think of the &lt;em&gt;xy&lt;/em&gt; plane (the blue layer) as the screen you're looking at right now. Since you're facing the screen (you're perpendicular to the screen) you're on the &lt;em&gt;z&lt;/em&gt; axis.&lt;/p&gt;

&lt;p&gt;Although the image only shows one layer on the &lt;em&gt;xy&lt;/em&gt; plane, the plane can contain an infinite number of &lt;em&gt;overlapping&lt;/em&gt; layers. The overlapping can be total or partial.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stacking&lt;/strong&gt;: Considering the system described above in the context of a web page, &lt;em&gt;stacking&lt;/em&gt; is the total or partial superposition of elements. The higher in the stack, the closer to the user facing the screen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Root element&lt;/strong&gt;: The &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element, which is the top-level element of any HTML document, is also known as the &lt;em&gt;root element&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Positioned element&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A positioned element is an element whose computed position value is either relative, absolute, fixed, or sticky. (In other words, it's anything except static.)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Descendant element&lt;/strong&gt;: A descendant element is an element that is nested within another element, regardless of how deeply nested.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Descendant non-positioned element&lt;/strong&gt;: An element that is a descendant of the root element and that has NOT been positioned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Descendant positioned element&lt;/strong&gt;: An element that is a descendant of the root element and that has been positioned.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Keep in mind that the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element is a container for the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; elements. The former is NOT visible to the user and contains metadata about your page, for instance, it links your page to external style sheets. The latter is a container for the visible portion of your page. Therefore, the descendants of the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element that matters to us are the one inside the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;h2&gt;
  
  
  The default stacking order &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Once you understand the terms described above, understanding the default stacking order is pretty straight forward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At the very bottom of the stack goes the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element.&lt;/li&gt;
&lt;li&gt;Then, its non-positioned descendant elements, in order of appearance in the source code.&lt;/li&gt;
&lt;li&gt;And finally, its positioned descendant elements, in order of appearance in the source code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demo &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=zac8nGU2RkU"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D6SGrub2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://img.youtube.com/vi/zac8nGU2RkU/0.jpg" alt="The default stacking of HTML elements" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; of the page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"static1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Static #1&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"relative"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Relative&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"absolute"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Absolute&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"fixed"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Fixed&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"sticky"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Sticky&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"static2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Static #2&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see from the demo and from the snippet above that both static elements go below the positioned elements when they overlap, even though &lt;em&gt;Static #2&lt;/em&gt; comes last in the source code. This proves that in this imaginary &lt;em&gt;z-axis&lt;/em&gt; positioned elements are closer the user than non-positioned elements.&lt;/p&gt;

&lt;p&gt;The way the positioned elements overlap proves that they are stacked in order of appearance.&lt;/p&gt;

&lt;p&gt;What about the &lt;em&gt;root element&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;⚠️ the next paragraph might blow your mind 🤯&lt;/p&gt;

&lt;p&gt;I haven't set any styles to it, which makes it keep its &lt;em&gt;transparent&lt;/em&gt; &lt;code&gt;background-color&lt;/code&gt;. The white background you see comes from the light your device's screen emits. Don't believe me? See the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-color#formal_definition"&gt;Formal definition&lt;/a&gt; for the &lt;code&gt;background-color&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you set a different &lt;code&gt;background-color&lt;/code&gt; to the &lt;em&gt;root element&lt;/em&gt; you'll confirm that it is below its descendant elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing to consider is that, when we talk about &lt;em&gt;stacking&lt;/em&gt;, we're talking about a tridimensional conceptualization of HTML pages. Elements are stacked on the z-axis, which is an imaginary axis perpendicular to the screen.&lt;/p&gt;

&lt;p&gt;Second, in order to understand how elements are stacked by default, i.e., without the &lt;code&gt;z-index&lt;/code&gt; property, we need to make a distinction between three types of elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The root element (the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element) which is a container for every other element you add to your page.&lt;/li&gt;
&lt;li&gt;Non-positioned elements, which have their computed &lt;code&gt;position&lt;/code&gt; value equals to &lt;code&gt;static&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Positioned elements, which have their computed &lt;code&gt;position&lt;/code&gt; value equals to anything but &lt;code&gt;static&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Third, stacking will only be observable in cases of overlapping between elements.&lt;/p&gt;

&lt;p&gt;Fourth, you may not have noticed, but there's no distinction between the different types of positioned elements. By that I mean that absolutely positioned elements do not have precedence over relatively positioned elements or vice-versa, for example. They have the same "weight" and the one that comes later on the source code will be on top.&lt;/p&gt;

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

&lt;p&gt;Once you understand what &lt;em&gt;stacking&lt;/em&gt; means in the context of web pages and the different types of elements that can be stacked, understanding the default stacking order becomes crystal clear.&lt;/p&gt;

&lt;p&gt;From a philosophical perspective, when you compare the &lt;em&gt;Important terms&lt;/em&gt; section with the &lt;em&gt;The default stacking order&lt;/em&gt; section you can see that learning &lt;em&gt;why&lt;/em&gt; may be more difficult, but it will make learning the &lt;em&gt;how&lt;/em&gt; easier.&lt;/p&gt;

&lt;p&gt;Finally, this article does NOT deal with the stacking of nested elements of a stacked element, nor with how to alter the stacking order, but I promise I'll cover that in a future article 🫡&lt;/p&gt;

&lt;p&gt;This project's source code is available at this &lt;a href="https://github.com/matheus4lves/article-default-stacking-of-html-elements"&gt;repo&lt;/a&gt;. Go ahead and give it a star!&lt;/p&gt;

&lt;p&gt;Did you like this article? Share it with your friends, or leave a comment, or a reaction. Of course, there's nothing stopping you from doing everything 😄&lt;/p&gt;

&lt;p&gt;If you're on HackerNoon, don't forget to subscribe if you want to get my latest articles straight to your inbox.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Set Up Webpack 5 To Work With Static Files</title>
      <dc:creator>Matheus Alves</dc:creator>
      <pubDate>Tue, 01 Aug 2023 22:50:55 +0000</pubDate>
      <link>https://dev.to/matheus4lves/set-up-webpack-5-to-work-with-static-files-52gk</link>
      <guid>https://dev.to/matheus4lves/set-up-webpack-5-to-work-with-static-files-52gk</guid>
      <description>&lt;h1&gt;
  
  
  Set Up Webpack 5 To Work With Static Files
&lt;/h1&gt;

&lt;p&gt;Out of the box webpack only understands JavaScript. However, we can expand its functionality by using &lt;em&gt;loaders&lt;/em&gt; and &lt;em&gt;plugins&lt;/em&gt;. In this tutorial, you’ll learn how to work with static files, namely, HTML, CSS, and some common image types.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Why do we need to to that? &lt;/li&gt;
&lt;li&gt;Important terms &lt;/li&gt;
&lt;li&gt;Understanding the project structure &lt;/li&gt;
&lt;li&gt;Understanding the overall setup &lt;/li&gt;
&lt;li&gt;
Working with HTML 

&lt;ul&gt;
&lt;li&gt;Configuring the html-loader &lt;/li&gt;
&lt;li&gt;Configuring the html-webpack-plugin &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Working with CSS 

&lt;ul&gt;
&lt;li&gt;Configuring the css-loader and the style-loader &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Working with images 

&lt;ul&gt;
&lt;li&gt;Using Asset Modules &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Final touches &lt;/li&gt;

&lt;li&gt;Summary &lt;/li&gt;

&lt;li&gt;Conclusion &lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why do we need to to that? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If you read the intro of this article with attention, you already know the answer: It’s because without any further configuration webpack only understands JavaScript. Therefore, we need to use &lt;em&gt;loaders&lt;/em&gt; and &lt;em&gt;plugins&lt;/em&gt; that allow webpack process and bundle other types of files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Important terms &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I decided to provide not only a definition but also an explanation for the necessity of the loaders and plugin we’re going to use. Feel free to skip this section if you have confidence you understand the terms listed bellow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I’m not following the alphabetical order on purpose. You’ll understand why 😉&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Assets&lt;/em&gt;: At the home page of &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;webpack&lt;/a&gt;, you can see that assets, scripts, images and styles are treated as different things. Nevertheless, at the &lt;a href="https://webpack.js.org/glossary/" rel="noopener noreferrer"&gt;glossary&lt;/a&gt; of the website we read that an&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;asset is a general term for the images, fonts, media, and any other kind of files that are typically used in websites and other applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you can see that in the glossary an &lt;em&gt;image&lt;/em&gt; is an &lt;em&gt;asset&lt;/em&gt;. I prefer to define assets as the resources we use as they are, for example, images, fonts, audio and video. In other words, we don’t code them ourselves, but we use them to build our project.&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: We usually put configuration files (&lt;strong&gt;.gitignore&lt;/strong&gt;, &lt;strong&gt;webpack.config.js&lt;/strong&gt;, etc) in the root directory of the project while the files and assets that are used to build the project are put in a sub-directory called &lt;strong&gt;src&lt;/strong&gt; (short for source) or &lt;strong&gt;app&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I prefer to think of the source as everything we have in our project. Thus, I prefer to call &lt;strong&gt;app&lt;/strong&gt; the sub-directory that contains our files and assets.&lt;/p&gt;

&lt;p&gt;Agree? Disagree? Let me know in the comments 🙂&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Loaders&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Loaders are transformations that are applied to the source code of a module. They allow you to pre-process files as you &lt;code&gt;import&lt;/code&gt; or “load” them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Putting it simple, &lt;em&gt;loaders&lt;/em&gt; allow us to work with file types other than JavaScript.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Plugins&lt;/em&gt;: This is an advanced topic so let’s stick to the following simplified definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;They also serve the purpose of doing &lt;strong&gt;anything else&lt;/strong&gt; that a &lt;a href="https://webpack.js.org/concepts/loaders?ref=hackernoon.com" rel="noopener noreferrer"&gt;loader&lt;/a&gt; cannot do.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;css-loader&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;em&gt;css-loader&lt;/em&gt; interprets &lt;code&gt;@import&lt;/code&gt; and &lt;code&gt;url()&lt;/code&gt; like &lt;code&gt;import&lt;/code&gt;/&lt;code&gt;require()&lt;/code&gt; and will resolve them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To work with CSS, we’ll have to import &lt;strong&gt;.css&lt;/strong&gt; files into &lt;strong&gt;.js&lt;/strong&gt; files. But, as CSS syntax is not valid JavaScript, we must set up the &lt;em&gt;css-loader&lt;/em&gt;, which will interpret &lt;code&gt;@import&lt;/code&gt; and &lt;code&gt;url()&lt;/code&gt; (CSS syntax) &lt;code&gt;import&lt;/code&gt;/&lt;code&gt;require()&lt;/code&gt; (JavaScript syntax), and resolve them.&lt;/p&gt;

&lt;p&gt;“Resolve” here simply means to identify the correct path to the specified file.&lt;/p&gt;

&lt;p&gt;This loader must be used with the &lt;em&gt;style-loader&lt;/em&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;style-loader&lt;/em&gt;: The &lt;em&gt;style-loader&lt;/em&gt; injects CSS into the DOM. While the &lt;em&gt;css-loader&lt;/em&gt; makes webpack understand CSS, it’s this loader's job to apply the CSS to the page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;html-loader&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Exports HTML as string, require references to static resources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What? 😕 Let me explain it to you 😉. The content of an HTML file is written using the &lt;strong&gt;H&lt;/strong&gt;yper*&lt;em&gt;T&lt;/em&gt;&lt;em&gt;ext **M&lt;/em&gt;&lt;em&gt;arkup **L&lt;/em&gt;&lt;em&gt;anguage (hence the name **HTML&lt;/em&gt;&lt;em&gt;), which is a text-based approach for describing how the content is structured. For example, if you have an &lt;code&gt;&amp;lt;h1&amp;gt;Heading level 1&amp;lt;/h1&amp;gt;&lt;/code&gt; inside of your *&lt;/em&gt;.html** file, when a browser parses your file, it will know exactly how to display your heading, &lt;em&gt;because the browser understands the contents of a &lt;strong&gt;.html&lt;/strong&gt; file&lt;/em&gt;. If you have a &lt;code&gt;&amp;lt;img src="/images/logo.png" alt="Logo" /&amp;gt;&lt;/code&gt;, it will understand the &lt;strong&gt;src&lt;/strong&gt; attribute and will locate and display the image.&lt;/p&gt;

&lt;p&gt;I think you already know that we can manipulate the content of an HTML file using JavaScript. For instance, we can create elements, add/remove classes, etc. This is only possible because of the APIs that allow us to do that.&lt;/p&gt;

&lt;p&gt;Even when using APIs, we cannot simply insert the content of an HTML file inside of a JavaScript file. But JavaScript understand strings. Therefore, the &lt;em&gt;html-loader&lt;/em&gt; reads the contents of an &lt;strong&gt;.html&lt;/strong&gt; file and returns it as an string that can be used by JavaScript.&lt;/p&gt;

&lt;p&gt;JavaScript does not understand the HTML &lt;code&gt;src&lt;/code&gt; attribute but it understands the JavaScript &lt;code&gt;require()&lt;/code&gt; statement! That’s why references to static resources like &lt;code&gt;src&lt;/code&gt; are translated into &lt;code&gt;require()&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;Having the HTML string and the requires, JavaScript can manipulate the DOM via APIs.&lt;/p&gt;

&lt;p&gt;If you want to learn more about APIs, see &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs?ref=hackernoon.com" rel="noopener noreferrer"&gt;Client-side web APIs&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;html-webpack-plugin&lt;/em&gt;: To understand the importance of this &lt;em&gt;plugin&lt;/em&gt;, we have to remember that in order to execute JavaScript in the browser,&lt;br&gt;
a. we have to embed our scripts using &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; elements in an HTML or&lt;/p&gt;

&lt;p&gt;b. link to external scripts (by using the &lt;code&gt;src&lt;/code&gt; attribute of &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; elements) from the HTML file.&lt;/p&gt;

&lt;p&gt;In other words, we need an HTML file as an &lt;em&gt;entry point&lt;/em&gt; for executing our scripts (or bundles) in the browser!&lt;/p&gt;

&lt;p&gt;This situation arises the following question: How can we work with an HTML file if webpack only understands JavaScript? I have two answers for this question, since there are two approaches that can be followed for handling this situation:&lt;/p&gt;

&lt;p&gt;The first approach involves manually creating an &lt;strong&gt;.html&lt;/strong&gt; file, adding the bundle(s) manually, and making a copy of this file at the build folder.&lt;/p&gt;

&lt;p&gt;The second approach involves using the &lt;em&gt;html-webpack-plugin&lt;/em&gt; and letting it do all the work for us.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The plugin will generate an HTML5 file for you that includes all your &lt;code&gt;webpack&lt;/code&gt; bundles in the head using &lt;code&gt;script&lt;/code&gt; tags.&lt;/p&gt;

&lt;p&gt;You can either let the plugin generate an HTML file for you, supply your own template using lodash templates or use your own loader.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this tutorial, we’re going to use an HTML template. Therefore, we’ll have to combine this &lt;em&gt;plugin&lt;/em&gt; with the &lt;em&gt;html-loader&lt;/em&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Understanding the project structure &lt;a&gt;&lt;/a&gt;
&lt;/h2&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9it2co2tzfwozkbf794.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9it2co2tzfwozkbf794.jpeg"&gt;&lt;/a&gt;&lt;br&gt;Project structure
  &lt;/p&gt;

&lt;p&gt;In the root directory of the project (our repository) we have some configuration files and a sub-directory named &lt;strong&gt;app&lt;/strong&gt; that contains the files and assets for building the app.&lt;/p&gt;

&lt;p&gt;Here’s a brief description of the role each configuration file plays:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.gitignore&lt;/strong&gt; ignores the node_modules directory and other files we don't want to commit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.prettierrc&lt;/strong&gt; instructs Prettier on how to format our code (OPTIONAL, for those who use &lt;a href="https://prettier.io/" rel="noopener noreferrer"&gt;Prettier&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;package.json&lt;/strong&gt; was automatically created by running &lt;code&gt;npm init -y&lt;/code&gt;. It holds all sorts of information about our project: name, version, dependencies, development dependencies, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;webpack.config.js&lt;/strong&gt; and &lt;strong&gt;webpack.parts.js&lt;/strong&gt; will be used to compose the webpack configuration, a strategy I have learned with SurviveJS, at the &lt;a href="https://survivejs.com/webpack/developing/composing-configuration/" rel="noopener noreferrer"&gt;Composing Configuration&lt;/a&gt; section of the book.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should add this code to your &lt;strong&gt;index.html&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"quote"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"quote__text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Knowledge is freedom&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"quote__img1"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./assets/images/bookshelf.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"A bookshelf illustration"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"1280"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"640"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now, the rest of the files are empty. As for the images, you can download them from the repository: &lt;a href="https://github.com/matheus4lves/article-serve-static-files" rel="noopener noreferrer"&gt;https://github.com/matheus4lves/article-serve-static-files&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The second image will be dynamically added later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the overall setup &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First, we need to install our basic development dependencies (devDependencies). Run the following command: &lt;code&gt;npm install --save-dev webpack webpack-cli webpack-dev-server webpack-merge&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is why we need these packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;webpack&lt;/em&gt; is, guess what, webpack. This is the core package.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;webpack-cli&lt;/em&gt; allows us to further config webpack via CLI (terminal/prompt) and from our scripts.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;webpack-dev-server&lt;/em&gt; provides us with a local development server.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;webpack-merge&lt;/em&gt; facilitates composing configuration by basically merging objects and concatenating arrays, returning an object after the operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;webpack.config.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack-merge&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commonConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./app/assets/scripts/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;_dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bundle.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;configs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;development&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([]),&lt;/span&gt;
  &lt;span class="na"&gt;production&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([]),&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;commonConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;configs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="p"&gt;}]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start by splitting our configuration into three categories: common configuration, development configuration, and production configuration.&lt;/p&gt;

&lt;p&gt;Instead of exporting an object from the module, we export a function which gives us access to the arguments we pass to the command line or to our scripts. We want to do that because we want to get the &lt;code&gt;mode&lt;/code&gt; from an argument.&lt;/p&gt;

&lt;p&gt;If we were to run the development server right now, we would have to run the following command from the CLI: &lt;code&gt;npx webpack serve --config webpack.config.js --mode development&lt;/code&gt;. Instead, we’re going to set up two scripts that will help us develop and build our app.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;scripts&lt;/code&gt; property of your &lt;strong&gt;package.json&lt;/strong&gt; file, add these lines right after the &lt;code&gt;test&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack serve --mode development"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack --mode production"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we want to start the development server we can simple run: &lt;code&gt;npm run dev&lt;/code&gt;. Much simpler, isn’t it?&lt;/p&gt;

&lt;p&gt;For the time being we have the most basic configuration. We defined an array of &lt;code&gt;entry&lt;/code&gt; points (because we can have more than one) and the &lt;code&gt;output&lt;/code&gt; property which tells webpack the name of our bundle and the location it should be emitted to when we build the project.&lt;/p&gt;

&lt;p&gt;Note that instead of exporting an object from the module we export a function, which gives us access to the &lt;code&gt;argv&lt;/code&gt; object. This object contains the arguments that come from the CLI/scripts. Then, we access the &lt;code&gt;mode&lt;/code&gt; property and create the configuration based on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: To keep things simple I'll not touch the development and production configurations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with HTML &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In this project we’re going to generate an &lt;strong&gt;.html&lt;/strong&gt; file based on a &lt;strong&gt;.html&lt;/strong&gt; &lt;a href="https://webpack.js.org/loaders/html-loader/#templating" rel="noopener noreferrer"&gt;template&lt;/a&gt;. We can also use other types of templates, for instance, &lt;a href="https://ejs.co/" rel="noopener noreferrer"&gt;EJS&lt;/a&gt;, &lt;a href="https://handlebarsjs.com/" rel="noopener noreferrer"&gt;handlebars&lt;/a&gt;, etc. We can, by the way, use no template at all, and generate our HTML based on plain JavaScript (As we will do later).&lt;/p&gt;

&lt;p&gt;Since we’re generating an static file based on a static file, the basic difference between our &lt;strong&gt;.html&lt;/strong&gt; template and the generated &lt;strong&gt;.html&lt;/strong&gt; file is that the generated file contains an &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag with our bundle.&lt;/p&gt;

&lt;p&gt;To be able to use an &lt;strong&gt;.html&lt;/strong&gt; file as a template, we must make webpack understand HTML. To do so, we’ll set up the &lt;em&gt;html-loader&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Then, we’ll set up the &lt;em&gt;html-webpack-plugin&lt;/em&gt; so that it can generate the final &lt;strong&gt;.html&lt;/strong&gt; for us.&lt;/p&gt;

&lt;p&gt;Summarizing, we’ll read from an &lt;strong&gt;.html&lt;/strong&gt; file to generate a &lt;strong&gt;.html&lt;/strong&gt; file. Again, this is because we’re generating an static file based on a static file. In a more realistic situation, we would generate the &lt;strong&gt;.html&lt;/strong&gt; file based on vanilla JavaScript (plain JavaScript) or based on a JavaScript library/templating language, thus generating static content based on dynamic content.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring the html-loader &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, install the &lt;em&gt;html-loader&lt;/em&gt; by running &lt;code&gt;npm install --save-dev html-loader&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, add this function to &lt;strong&gt;wepback.parts.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loadHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;html$/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html-loader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function returns an object literal that we have to merge to our configuration. Go to &lt;strong&gt;webpack.config.js&lt;/strong&gt; and add this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./webpack.parts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, add a call to &lt;code&gt;loadHTML()&lt;/code&gt; as the second element of the array passed as the argument of the &lt;code&gt;merge&lt;/code&gt; function that is creating the &lt;code&gt;commonConfig&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commonConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./app/assets/scripts/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;_dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bundle.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadHTML&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Webpack now understands HTML.&lt;/p&gt;

&lt;p&gt;If you run the development server and visit the address your project is running at, you’ll be able to see the image even though we haven’t set up webpack to work with images yet. If you want to understand why read about the loader in the &lt;em&gt;Important terms&lt;/em&gt; section of this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring the html-webpack-plugin &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, install the plugin by running &lt;code&gt;npm install --save-dev html-webpack-plugin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, add this function to &lt;strong&gt;webpack.parts.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;generateHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HtmlWebpackPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="p"&gt;})],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, at the top of the same file, don’t forget to require the plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HtmlWebpackPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, call this function right after the call to &lt;code&gt;loadHTML()&lt;/code&gt; in &lt;strong&gt;webpack.config.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateHTML&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./app/index.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t forget to specify the template!&lt;/p&gt;

&lt;p&gt;If you run npm &lt;code&gt;run build now&lt;/code&gt;, you should see an &lt;strong&gt;index.html&lt;/strong&gt; file inside the &lt;strong&gt;build&lt;/strong&gt; directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with CSS &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Two things are required in order to work with CSS:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have to make webpack understand CSS.&lt;/li&gt;
&lt;li&gt;We have to apply the CSS to the page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the first step we need to install and config the &lt;em&gt;css-loader&lt;/em&gt;. For the second we need to do the same with the &lt;em&gt;style-loader&lt;/em&gt;. Since there’s no point in making webpack understand CSS without applying the styles to the page, we’re going to set up these loaders together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring the css-loader and the style-loader &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s start by installing the required packages. Run: &lt;code&gt;npm install --save-dev css-loader style-loader&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, add this to your** webpack.parts.js**:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loadCSS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;css$/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;style-loader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;css-loader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="nx"&gt;Thanks&lt;/span&gt; 
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: this order is important because webpack reads the loaders from right to left.&lt;/p&gt;

&lt;p&gt;As with the other functions we’ve exported from this module, we have to call it from &lt;strong&gt;webpack.config.js&lt;/strong&gt;. Therefore, add the following line right after the call to the &lt;code&gt;generateHTML()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadCSS&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test the configuration, add this to &lt;strong&gt;styles.css&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#00ff00&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this to &lt;strong&gt;index.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../styles/styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run the development server and visit the address the project is running at, you should see that the page has a green background.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with images &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If we wanted to work with images in webpack prior to its fifth version, we would have to set up the &lt;em&gt;file-loader&lt;/em&gt;, but webpack 5 introduced &lt;a href="https://webpack.js.org/guides/asset-modules/" rel="noopener noreferrer"&gt;Asset Modules&lt;/a&gt;, which we’re going to use instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Asset Modules &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;According to the official webpack documentation,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Asset Modules is a type of module that allows one to use asset files (fonts, icons, etc) without configuring additional loaders.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To allow webpack load images that are &lt;em&gt;dynamically&lt;/em&gt; added, add this function to &lt;strong&gt;webpack.config.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;loadImages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.(&lt;/span&gt;&lt;span class="sr"&gt;png|svg|jpg|jpeg|gif&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;asset/resource&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, make a call to this function in &lt;strong&gt;webpack.config.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadImages&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test the configuration, let’s dynamically add the second image to our page:&lt;/p&gt;

&lt;p&gt;Update the &lt;strong&gt;index.js&lt;/strong&gt; to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../styles/styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;brokenHandcuffs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../images/broken-handcuffs.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;quote__img2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;brokenHandcuffs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;alt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Someone's upraised arms bound to the pieces of a handcuff that has just been broken.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;width&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;817&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;height&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;460&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;imgContainer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.quote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;imgContainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Webpack will process this image, which means that the image will be added to the &lt;em&gt;output&lt;/em&gt; directory (&lt;strong&gt;build&lt;/strong&gt;, in our case) and the variable &lt;code&gt;brokenHandcuffs&lt;/code&gt; will contain the final URL of the image after processing.&lt;/p&gt;

&lt;p&gt;If you run &lt;code&gt;npm run build&lt;/code&gt;, you’ll see the &lt;strong&gt;index.html&lt;/strong&gt; file with the images in the &lt;strong&gt;build&lt;/strong&gt; directory. Notice how the name of the images have changed (The final URL matches the name of the images).&lt;/p&gt;

&lt;p&gt;If you run the development server, you should see both images on the page.&lt;/p&gt;

&lt;p&gt;In the next section we’ll update the CSS to make the page a bit prettier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final touches &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Update &lt;strong&gt;styles.css&lt;/strong&gt; to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.quote&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100vh&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.quote__text&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#175f73&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;380px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-55%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.quote__img1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;700px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-50%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.quote__img2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;400px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-50%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get this result&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpnfhd38tpkrkqxq6dr2i.jpeg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpnfhd38tpkrkqxq6dr2i.jpeg"&gt;&lt;/a&gt;&lt;br&gt;Final version of the page
  &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s a list with the main points of this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By default webpack only understands JavaScript. Therefore, if we want to work with static files we need to expand its functionality through &lt;em&gt;loaders&lt;/em&gt; and &lt;em&gt;plugins&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;If we want to work with HTML, we need to set up the &lt;em&gt;html-loader&lt;/em&gt; and the &lt;em&gt;html-webpack-plugin&lt;/em&gt;. The former makes webpack understand HTML and allows us to use a &lt;strong&gt;.html&lt;/strong&gt; file as a template while the latter automatically generates an HTML5 that includes all our bundles in the head using &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags.&lt;/li&gt;
&lt;li&gt;To work with CSS we need to make webpack understand it first by configuring the &lt;em&gt;css-loader&lt;/em&gt;, and then tell webpack to apply our styles to our pages by configuring the &lt;em&gt;style-loader&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Since webpack 5, if we want to work with images that are dynamically added we don’t need to set up additional loaders because webpack provides Asset Modules. We just need to set up the test condition and the proper type of resource.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Thanks to its rich ecosystem of loaders and plugins webpack became much more than a module bundler, allowing us to to things that would previously required a task manager and other tools.&lt;/p&gt;

&lt;p&gt;If you want to expand webpack’s functionality, this is the basic workflow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install the loader or plugin you need.&lt;/li&gt;
&lt;li&gt;Configure it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope this tutorial is useful for you. Thanks for reading! You can find the source code of this project at this repo: &lt;a href="https://github.com/matheus4lves/article-serve-static-files" rel="noopener noreferrer"&gt;https://github.com/matheus4lves/article-serve-static-files&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>node</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>What’s the Difference Between path.join() and path.resolve()?</title>
      <dc:creator>Matheus Alves</dc:creator>
      <pubDate>Mon, 12 Jun 2023 02:55:05 +0000</pubDate>
      <link>https://dev.to/matheus4lves/whats-the-difference-between-pathjoin-and-pathresolve-34ga</link>
      <guid>https://dev.to/matheus4lves/whats-the-difference-between-pathjoin-and-pathresolve-34ga</guid>
      <description>&lt;p&gt;How many times have you come across &lt;code&gt;path.join()&lt;/code&gt; and &lt;code&gt;path.resolve()&lt;/code&gt; in Node.js projects without really understanding the difference between them?&lt;/p&gt;

&lt;p&gt;In my own experience, I’ve seen different projects with the same structure using one over the other and I couldn’t really understand why. After becoming aware of my lack of knowledge, I decided to investigate and share my learnings with you.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;List of Important terms &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Brief recap - how to change directories on terminal/prompt&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What does path.join() do?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What does path.resolve() do?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The key difference&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When should you choose one over the other?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Usage examples&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  List of Important Terms &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I believe once you understand the terms listed below it will be easier for you to grasp the differences between the two methods.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;String:&lt;/strong&gt; A string is a sequence of characters that are interpreted literally (the exact way it has been written) by a script, e.g., &lt;code&gt;"this is a string"&lt;/code&gt;. They must be surrounded by quotation marks if there’s more than one word.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Path:&lt;/strong&gt; A path is simply a &lt;em&gt;string&lt;/em&gt; that identifies the location of a &lt;em&gt;file&lt;/em&gt; or &lt;em&gt;directory&lt;/em&gt; on a computer, e.g., &lt;code&gt;"/home/user/Documents/file.ext"&lt;/code&gt;  or &lt;code&gt;"/home/user/Downloads/"&lt;/code&gt;. I’m going to use the word ‘file’ here for referring to both &lt;em&gt;files&lt;/em&gt; and &lt;em&gt;directories&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Relative path:&lt;/strong&gt; A relative path is a &lt;em&gt;string&lt;/em&gt; that identifies the location of a file relative to the location of another file.&lt;/p&gt;

&lt;p&gt;For instance, let’s assume some user has the following directory structure:&lt;/p&gt;


&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsw05nd69a96siy2jwji.png" alt="Directory structure example"&gt;Directory structure example



&lt;p&gt;If Downloads is the current working directory (CWD) and the user wants to access a file inside the Documents directory, he would have to navigate up one level, and then go down into the Documents directory. Something like this on POSIX-compliant systems (Linux, MacOS, etc.) &lt;code&gt;cd ../Documents/file.ext&lt;/code&gt; or like this on Windows &lt;code&gt;cd ..\Documents\file.ext&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; On the terminal or prompt we don’t need to surround the string with quotation marks if there are no spaces in the names of the files. Trying to execute &lt;code&gt;cd ../Documents/my file.ext&lt;/code&gt; would result in an error message.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Absolute path:&lt;/strong&gt; Simplifying, an absolute path is the &lt;em&gt;string&lt;/em&gt; that identifies the location of a file relative to the root of the system. For example, instead of trying to access the file from the previous  example via a relative path, the user could’ve specified an absolute path like this &lt;code&gt;cd /home/user/Documents/file.ext&lt;/code&gt;. We can specify an absolute path from any location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Path segment:&lt;/strong&gt; A path segment is a slice of the path. For example, we have &lt;code&gt;/home&lt;/code&gt;, &lt;code&gt;/user&lt;/code&gt; and &lt;code&gt;/Documents&lt;/code&gt; as segments of &lt;code&gt;/home/user/Documents/file.ext&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;__dirname:&lt;/strong&gt; This is a Node.js environment variable that specifies the &lt;em&gt;absolute path&lt;/em&gt; for the &lt;em&gt;directory&lt;/em&gt; which contains the currently executing &lt;em&gt;file&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For instance, if you have &lt;strong&gt;webpack.config.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expected output on the console would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/home/user/Documents/project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;strong&gt;webpack.config.js&lt;/strong&gt; is inside the &lt;code&gt;project&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;In other words, &lt;code&gt;__dirname&lt;/code&gt; is simply the &lt;em&gt;string&lt;/em&gt; of the absolute path to the file’s location.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Path normalization:&lt;/strong&gt; As you saw, there are differences between the &lt;em&gt;strings&lt;/em&gt; used to specify the path on different OSs. Windows uses &lt;code&gt;\&lt;/code&gt; while POSIX-compliant OSs use &lt;code&gt;/&lt;/code&gt; as a directory separator, just for instance. Path normalization happens when the &lt;em&gt;string&lt;/em&gt; is modified so that it conforms to a valid path on the target operating system. If you want to learn more about the steps required to perform path normalization, see &lt;a href="https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Brief Recap - How to Change Directories on Terminal/Prompt &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.&lt;/code&gt; and &lt;code&gt;./&lt;/code&gt; represent the CWD;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;..&lt;/code&gt; and &lt;code&gt;../&lt;/code&gt; represent the parent of the CWD;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/&lt;/code&gt; represents the root directory&lt;/p&gt;

&lt;p&gt;If you put &lt;code&gt;cd&lt;/code&gt; in front of them, they’ll take you to the desired location.&lt;/p&gt;

&lt;p&gt;I said it was going to be brief 😂&lt;/p&gt;

&lt;h2&gt;
  
  
  What does path.join() do? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;path.join()&lt;/code&gt; method joins all given &lt;code&gt;path&lt;/code&gt; segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The syntax is quite simple: &lt;code&gt;path.join(…paths)&lt;/code&gt;, where &lt;code&gt;…paths&lt;/code&gt; is a sequence of &lt;em&gt;segments&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example, calling &lt;code&gt;path.join("path", "/to", "some", "file")&lt;/code&gt;  returns the following string &lt;code&gt;path/to/some/file&lt;/code&gt; in a POSIX-compliant system.&lt;/p&gt;

&lt;p&gt;If you don’t understand what the &lt;code&gt;…&lt;/code&gt; mean, take a look at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;Spread syntax (...)&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does path.resolve() do? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;path.resolve()&lt;/code&gt; method resolves a sequence of paths or path segments into an absolute path.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Almost identical syntax: &lt;code&gt;path.resolve(…paths)&lt;/code&gt;. This method processes the paths &lt;em&gt;from right to left&lt;/em&gt; and when it finds a &lt;code&gt;/&lt;/code&gt; treats it as the root of the system.&lt;/p&gt;

&lt;p&gt;For example, calling &lt;code&gt;path.resolve(__dirname, "/path")&lt;/code&gt; would return &lt;code&gt;/path&lt;/code&gt;, because the second argument has a leading &lt;code&gt;/&lt;/code&gt; and thus will be treated as the root of the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  The key difference &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;path.join()&lt;/code&gt; simply concatenates segments and its return &lt;em&gt;may or may not&lt;/em&gt; result in an absolute path. &lt;code&gt;path.resolve()&lt;/code&gt; &lt;em&gt;always&lt;/em&gt; returns an absolute path, using the target operating system’s root as the root or the first argument with a leading &lt;code&gt;/&lt;/code&gt; as the new root.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should you choose one over the other? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;It depends. Does your tool require an &lt;strong&gt;absolute&lt;/strong&gt; path? Then use &lt;code&gt;path.resolve()&lt;/code&gt;. Do you only need to concatenate segments of paths? Use &lt;code&gt;path.join()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In practice, I can’t remember seeing &lt;code&gt;path.join()&lt;/code&gt; or &lt;code&gt;path.resolve()&lt;/code&gt; being used without &lt;code&gt;__dirname&lt;/code&gt;, which makes both methods result in an absolute path with the OS’s root being the root.&lt;/p&gt;

&lt;p&gt;Again, calling &lt;code&gt;path.resolve(__dirname, "/path")&lt;/code&gt; makes no sense since &lt;code&gt;__dirname&lt;/code&gt; would be totally ignored because the second argument starts with a &lt;code&gt;/&lt;/code&gt;. The following is a valid use case: &lt;code&gt;path.resolve(__dirname, 'dist/assets')&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage examples &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// path.join()
/home/user/some/other/segments/path
/home/user/some/other/segments/path
/home/user/some/other/segments/path
/home/user/some/other/path

//path.resolve()
/home/user/some/other/segments/path
/path
/home/user/some/other/segments/path
/home/user/some/other/path
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, if what you need is an &lt;strong&gt;absolute&lt;/strong&gt; path to the current executing file, you can choose any of the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think that’s enough.&lt;/p&gt;

&lt;p&gt;If you found this article useful, share it with your friends.&lt;/p&gt;

&lt;p&gt;See you next time!&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascrip</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
