<?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: Lee</title>
    <description>The latest articles on DEV Community by Lee (@limingcan562).</description>
    <link>https://dev.to/limingcan562</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%2F1046868%2F420de8cd-3b88-4221-b6a2-1387a830aad8.jpeg</url>
      <title>DEV Community: Lee</title>
      <link>https://dev.to/limingcan562</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/limingcan562"/>
    <language>en</language>
    <item>
      <title>I have taken advantage of Excel's functionality to develop a CLI tool to better manage data</title>
      <dc:creator>Lee</dc:creator>
      <pubDate>Fri, 22 Dec 2023 02:28:42 +0000</pubDate>
      <link>https://dev.to/limingcan562/i-have-taken-advantage-of-excels-functionality-to-develop-a-cli-tool-to-better-manage-data-44o6</link>
      <guid>https://dev.to/limingcan562/i-have-taken-advantage-of-excels-functionality-to-develop-a-cli-tool-to-better-manage-data-44o6</guid>
      <description>&lt;p&gt;Hello everyone! Today I would like to introduce this tool I developed - &lt;a href="https://github.com/limingcan562/ejc-cli/"&gt;ejc-cli&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical issues
&lt;/h2&gt;

&lt;p&gt;Sometimes, in order to save back-end development costs, we use &lt;code&gt;json&lt;/code&gt; files to store some data locally, and then use these data for rendering. However:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there are too many pages or modules that require data, the corresponding &lt;code&gt;json&lt;/code&gt; files will also increase. There are too many files and data is too fragmented, making managing so many files difficult&lt;/li&gt;
&lt;li&gt;When the amount of data is large, the data can become very lengthy, and it is difficult to directly find a specific piece of data&lt;/li&gt;
&lt;li&gt;Sometimes &lt;code&gt;json&lt;/code&gt; is not a good way to spread our data when we want to share it with other non developers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why choose Excel to manage data
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;More convenient operation. Excel is a professional office software. Adding, deleting, modifying, and querying data in Excel is of course much more convenient than modifying it in our editor&lt;/li&gt;
&lt;li&gt;More visual. Each sheet can be used as data for a certain page or module; The name of each sheet is the name of our json; Each row of data in the sheet is the data of each of our &lt;code&gt;json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;More convenient management. The data is integrated into an Excel file, making it easier for us to manage the data of all modules or pages&lt;/li&gt;
&lt;li&gt;More suitable for dissemination. &lt;code&gt;json&lt;/code&gt; is not suitable for dissemination among non developers, but Excel is suitable for all people. Not only can non developers modify this Excel, but if you find a problem in Excel, you can also modify it and then sync it to others.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tools are born
&lt;/h2&gt;

&lt;p&gt;Therefore, in order to solve the above problems and combine the comprehensive advantages of Excel, &lt;code&gt;ej-cli&lt;/code&gt; was born, so you only need to focus on managing the Excel file.&lt;/p&gt;

&lt;p&gt;For more convenient use, &lt;code&gt;ejc-cli&lt;/code&gt; does not require you to create a Excel yourself. It already has a Excel built-in, and you only need to execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ejc-cli gt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or save the template file to the specified directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ejc-cli gt './ xlsx_ template/'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, we can obtain a 'Excel' template file, and then replace the data in it with what you want. That's as simple as that.&lt;/p&gt;

&lt;p&gt;Then export the &lt;code&gt;json&lt;/code&gt; data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ejc-cli -i './ xlsx_ template/template.xlsx'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The results are as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aijEZne9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hjn3nq2y5j4he1gkggb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aijEZne9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hjn3nq2y5j4he1gkggb.gif" alt="gif preview" width="800" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mUe0uCJT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ns6cfv203pkajlvvpn0s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mUe0uCJT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ns6cfv203pkajlvvpn0s.png" alt="Usage effect" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  End
&lt;/h2&gt;

&lt;p&gt;I hope this tool can help you. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you like it, you can give it a &lt;code&gt;star&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If you have any suggestions, you can give me a &lt;code&gt;issue&lt;/code&gt; to improve it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more information about &lt;code&gt;ejc-cli&lt;/code&gt;, you can check &lt;a href="https://github.com/limingcan562/ejc-cli/"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>tool</category>
      <category>javascript</category>
      <category>productivity</category>
    </item>
    <item>
      <title>I efficiently learned "webpack development environment configuration" using this idea</title>
      <dc:creator>Lee</dc:creator>
      <pubDate>Fri, 08 Sep 2023 08:33:53 +0000</pubDate>
      <link>https://dev.to/limingcan562/i-efficiently-learned-webpack-development-environment-configuration-using-this-idea-2f2e</link>
      <guid>https://dev.to/limingcan562/i-efficiently-learned-webpack-development-environment-configuration-using-this-idea-2f2e</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;We often see structures like the following when using &lt;code&gt;webpack&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;build
├── webpack.common.js
└── webpack.dev.js
└── webpack.prod.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Often times, &lt;code&gt;webpack&lt;/code&gt; configurations we basically copy and paste without thinking about why we're breaking the config file down into so many. As a result, when we learn to configure &lt;code&gt;webpack&lt;/code&gt; on our own, we often have problems such as the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow builds in development environments&lt;/li&gt;
&lt;li&gt;Blank pages after packaging&lt;/li&gt;
&lt;li&gt;Resources not found&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In fact, these problems, all stem from us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not understanding &lt;code&gt;webpack&lt;/code&gt; to build the development environment, production environment, the two environment configuration of the difference in thinking&lt;/li&gt;
&lt;li&gt;We don't have a clear understanding of the file directories of the two environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So this article is the same as before, will not just copy the configuration over, but hope that &lt;strong&gt;summarize the relevant methods&lt;/strong&gt;, so that we can better learn to configure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Outline
&lt;/h2&gt;

&lt;p&gt;This article focuses on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The core idea of &lt;code&gt;webpack&lt;/code&gt; development environment configuration.&lt;/li&gt;
&lt;li&gt;Completing a development environment configuration based on the core concepts&lt;/li&gt;
&lt;li&gt;Explain some confusing points in the &lt;code&gt;devServer&lt;/code&gt; configuration.&lt;/li&gt;
&lt;li&gt;Summarize the core ideas of &lt;code&gt;webpack&lt;/code&gt; development environment configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Remarks
&lt;/h2&gt;

&lt;p&gt;The article has been uploaded to &lt;a href="https://github.com/limingcan562/learn-webpack-5"&gt;github&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the sake of reading convenience, the article only posted the relevant code, it is recommended to &lt;code&gt;fork&lt;/code&gt; it to see the complete code; or follow along with the article and write the code yourself&lt;/li&gt;
&lt;li&gt;It's not easy to create, if you think it's helpful, welcome to &lt;code&gt;star&lt;/code&gt;🌟!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Development Environment Core Ideas
&lt;/h2&gt;

&lt;p&gt;We mentioned above that we often see directories like the following when using &lt;code&gt;webpack&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;build
├── webpack.common.js
└── webpack.dev.js
└── webpack.prod.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why do we need to break the configuration file into so many, so cumbersome?&lt;/p&gt;

&lt;p&gt;Let's first review, in our actual development, in order to project can be stable and safe on-line, we will generally be divided into several environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Development environment: local development, debugging environment&lt;/li&gt;
&lt;li&gt;Test environment: we complete the local development, the code will be deployed to our test server, for testing&lt;/li&gt;
&lt;li&gt;Production environment: the test passed, the code will be deployed to the official server, the official on-line&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Some of the more rigorous tests will have pre-production environments, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our configuration should be specific to the environment, so of course our configuration should not be the same for different environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Purpose
&lt;/h3&gt;

&lt;p&gt;We need to be clear about what the fundamental purpose of the development environment is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fundamental purpose of the development environment is to quickly locate problems during the development process, to see the effect of debugging faster, and to improve development efficiency.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Especially as a front-end, inseparable from the visual dealings. We can't wait to &lt;code&gt;Ctrl+S&lt;/code&gt;, immediately after the effect, so that we can quickly debug. Imagine if every time we write an effect, we have to wait for a few seconds before we can see it, will this drive us crazy?&lt;/p&gt;

&lt;h3&gt;
  
  
  Ideas
&lt;/h3&gt;

&lt;p&gt;Therefore, in order for us to see the effect debugging as soon as possible, reducing the &lt;code&gt;webpack&lt;/code&gt; compilation process, &lt;strong&gt;development environment, the configuration should be everything from simple&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For styles, we don't need to separate them and insert them directly into the &lt;code&gt;&amp;lt;head /&amp;gt;&lt;/code&gt; with the &lt;code&gt;style-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;media&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt; and other front-end resource files, we don't need to separate them.&lt;/li&gt;
&lt;li&gt;Enable &lt;code&gt;source map&lt;/code&gt; to locate the problem.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;devServer&lt;/code&gt; configuration provided by &lt;code&gt;webpack&lt;/code&gt; for local development.&lt;/li&gt;
&lt;li&gt;Don't compress code.&lt;/li&gt;
&lt;li&gt;Don't use unnecessary &lt;code&gt;loader&lt;/code&gt; and &lt;code&gt;plugins&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Development environment configuration is also different from person to person, this is what I do. But we should remember that development environment, configuration should be kept simple, in order to let us see the results faster, improve development efficiency!&lt;/p&gt;

&lt;h2&gt;
  
  
  Development environment configuration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  First experience
&lt;/h3&gt;

&lt;p&gt;Through the previous explanation, I believe you are familiar with the basic configuration of &lt;code&gt;webpack&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handling &lt;code&gt;css, less&lt;/code&gt;, front-end resources and other files&lt;/li&gt;
&lt;li&gt;Compiling &lt;code&gt;es6+&lt;/code&gt; syntax and &lt;code&gt;api&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Processing &lt;code&gt;html&lt;/code&gt; files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;have mastered the relevant configuration methods, and we also summarized a &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-07"&gt;basic configuration&lt;/a&gt;, not too clear students can look at the first, this basic configuration is still quite important.&lt;/p&gt;

&lt;p&gt;ok, then we first use &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-08"&gt;learn-08&lt;/a&gt; this case to experience how to do local development debugging. In order to be as close as possible to our real-life development projects, the case will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;refer to the image in &lt;code&gt;html&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;less&lt;/code&gt; to reference an image&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;es6+&lt;/code&gt; in &lt;code&gt;js&lt;/code&gt; to load images dynamically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use the &lt;code&gt;devServer&lt;/code&gt; configuration item&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For ease of reading, I'm not going to put the source code in the article, you can go to &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-08"&gt;github&lt;/a&gt; to see it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then perform the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;for semantic purposes, we name the &lt;code&gt;webpack&lt;/code&gt; configuration file &lt;code&gt;webpack.dev.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Start &lt;code&gt;webpack&lt;/code&gt; on the command line - &lt;code&gt;npm start&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "webpack server --config ./webpack.dev.js"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CFf48zeu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7cjo97ew3a46o1uolel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CFf48zeu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7cjo97ew3a46o1uolel.png" alt="devserve.png" width="800" height="614"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After the file compilation and processing is complete, no file is outputted&lt;/li&gt;
&lt;li&gt;At this point, an internal server (&lt;code&gt;http://192.168.0.196:8080/&lt;/code&gt;) is turned on. If we are on the same network as our computer, we can access this link from our phone and see the results. This is very efficient for debugging &lt;code&gt;H5&lt;/code&gt; development (this is mainly due to the &lt;code&gt;deveServer&lt;/code&gt; configuration item, which will be explained later on)&lt;/li&gt;
&lt;li&gt;The page is updated in real time after we change the code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The above is pretty much what we want for efficient development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analyzing
&lt;/h3&gt;

&lt;p&gt;After the initial experience, let's take a look at how to configure the development environment based on the development environment configuration ideas summarized above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parsing styles does not require separating out the &lt;code&gt;css&lt;/code&gt; file; we insert the parsed styles directly into the &lt;code&gt;&amp;lt;header /&amp;gt;&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&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|less&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&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="s1"&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="s1"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;less-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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;media&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt; and other front-end resource files, there is no need to subpackage:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&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="nx"&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="s1"&gt;./dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[name]-[chunkhash:5].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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&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="s1"&gt;asset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;dataUrlCondition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// When the image size is &amp;lt; 3kb it will be converted to base64&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;generator&lt;/span&gt;&lt;span class="p"&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="s1"&gt;[name]-[hash:5][ext]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Set the name of the output file&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;m&lt;/span&gt;&lt;span class="se"&gt;?&lt;/span&gt;&lt;span class="sr"&gt;js$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;exclude&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;node_modules|bower_components&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&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="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="s1"&gt;babel-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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Enable &lt;code&gt;source map&lt;/code&gt; to make it easier to locate the problem; and not compress the code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;devtool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inline-cheap-module-source-map&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;ul&gt;
&lt;li&gt;Use the &lt;code&gt;devServer&lt;/code&gt; provided with &lt;code&gt;webpack&lt;/code&gt; for local development:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;devServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;static&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;directory&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="nx"&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="s1"&gt;static&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;No other extra &lt;code&gt;plugins&lt;/code&gt; were installed, only the &lt;code&gt;plugin&lt;/code&gt; that parses &lt;code&gt;html&lt;/code&gt; was installed because it parses &lt;code&gt;html&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&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="nx"&gt;HtmlWebpackPlugin&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;./dist/[name].html&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;./src/index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webpack.dev.config&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we compare the base configuration, we will find that the &lt;strong&gt;development environment configuration is almost the same as the base configuration, except that the &lt;code&gt;devServer&lt;/code&gt; configuration item&lt;/strong&gt; is used more to enable local server debugging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt; webpack.dev.js
&lt;span class="p"&gt;const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
&lt;/span&gt;
module.exports = {
&lt;span class="gi"&gt;+   mode: 'development',
+   devtool: 'inline-cheap-module-source-map',
&lt;/span&gt;    entry: {
        index: './src/index.js'
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name]-[chunkhash:5].js',
    },
    module: {
        rules: [
            {
                test: /.(css|less)$/, 
                use: [
                    'style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/i,
                type: 'asset',
                parser: {
                    dataUrlCondition: {
                        maxSize: 1024 * 3 // When the image size is &amp;lt; 3kb it will be converted to base64
                    }
                },
                generator: {
                    filename: '[name]-[contenthash:5][ext][query]' // Set the name of the output file
                }
            },
            {
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: path.resolve(__dirname, './dist/[name].html'),
            template: path.resolve(__dirname, './src/index.html'),
            title: 'webpack.dev.config',
        })
    ],
&lt;span class="gi"&gt;+   devServer: {
+       static: {
+           directory: path.join(__dirname, 'static'),
+       },
+   }
&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  devServer
&lt;/h2&gt;

&lt;p&gt;Remember our previous case, how we ran our code? We ran it with our own editor (&lt;code&gt;vscode&lt;/code&gt;) to see the results after we had packaged it. If we were that inefficient, we wouldn't be able to go live with our project.&lt;/p&gt;

&lt;p&gt;That's why &lt;code&gt;webpack&lt;/code&gt; provides a very user-friendly configuration item called &lt;code&gt;devServer&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To use it, we need to install the &lt;code&gt;webpack-dev-server&lt;/code&gt; dependency &lt;code&gt;npm i webpack-dev-server -D&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From such a semantic name, we know that it must be a configuration that provides some kind of service in the development environment.&lt;/p&gt;

&lt;p&gt;The service is that this will enable a local service that will compile our configured &lt;code&gt;webpack&lt;/code&gt;, in real time, and save it in memory, so that we can easily debug our code, which serves the purpose of our local development needs to be efficient very well.&lt;/p&gt;

&lt;p&gt;So this configuration item, &lt;strong&gt;only used for our development environment configuration.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  devServer.static
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Documentation Explanation: tells the server where to serve content from. This is only necessary if you wish to serve static files&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Misinterpretation
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;devServe&lt;/code&gt; configuration item is actually written in quite a bit of detail in the documentation, so here's a potentially confusing one - &lt;code&gt;devServe.static&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A lot of people use &lt;code&gt;webpack&lt;/code&gt; and copy and paste, so it's common to see configurations like 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;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&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="nx"&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="s1"&gt;. /dist&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="nl"&gt;devServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;static&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;directory&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="nx"&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="s1"&gt;dist&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the folder we set up for output after packaging is generally &lt;code&gt;dist&lt;/code&gt;, the &lt;code&gt;devServe.static&lt;/code&gt; configuration item specifies that the directory of static files to be served is also &lt;code&gt;dist&lt;/code&gt;. So, we would naturally assume that the contents of the local server access opened by &lt;code&gt;webpack&lt;/code&gt; at this point would be the contents inside the &lt;code&gt;dist&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Therefore, we would incorrectly conclude that &lt;code&gt;devServe.static&lt;/code&gt; serves to set the directory that is accessed by the local server when &lt;code&gt;webpack&lt;/code&gt; is turned on.&lt;/p&gt;

&lt;p&gt;However, we can look at &lt;a href="https://github.com/limingcan562/learn-webpack-5/blob/main/learn-08/webpack.dev.js"&gt;dev.config.js&lt;/a&gt; above, where we set the name of the folder we output after packaging to &lt;code&gt;dist&lt;/code&gt;, and the directory set by &lt;code&gt;devServe.static&lt;/code&gt; is &lt;code&gt;static&lt;/code&gt;. If we follow our misinterpretation above, we should have a &lt;code&gt;404&lt;/code&gt; or a blank page on our server at this point due to the difference in the two folder settings, but we can still run our service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This means that &lt;code&gt;devServe.static&lt;/code&gt; is not setting the directory where the packaged output folder is accessed when &lt;code&gt;webpack&lt;/code&gt; is turned on for the local server.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Correctly understood
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Analysis
&lt;/h4&gt;

&lt;p&gt;Let's move on to the &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-08"&gt;learn-08&lt;/a&gt; case (the &lt;code&gt;static&lt;/code&gt; folder holds the &lt;code&gt;logo.png&lt;/code&gt; image inside).&lt;/p&gt;

&lt;p&gt;We previously wanted to reference the image in question inside &lt;code&gt;js&lt;/code&gt;, which we typically do:&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="c1"&gt;// index.js&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="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Image&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;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Your image 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;We normally &lt;code&gt;require&lt;/code&gt; the image &lt;strong&gt;according to the relative path&lt;/strong&gt;, which is equivalent to a module, so the image will be compiled by &lt;code&gt;webpack&lt;/code&gt; (adding &lt;code&gt;hash&lt;/code&gt; or converting it to &lt;code&gt;base64&lt;/code&gt;), and then written to the final directory (&lt;code&gt;output.path&lt;/code&gt;) that we have set up.&lt;/p&gt;

&lt;p&gt;But when we set up:&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;devServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;static&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;directory&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="nx"&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="s1"&gt;static&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After opening the local server, this line prompts:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Content not from webpack is served from 'your path/static' directory&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First of all, this means that the &lt;code&gt;static&lt;/code&gt; directory we set up will not be processed and packaged by &lt;code&gt;webpack&lt;/code&gt;, so the resources in this folder will not be parsed and compiled by &lt;code&gt;webpack&lt;/code&gt;, and therefore it will not be &lt;code&gt;hashed&lt;/code&gt; or converted to &lt;code&gt;base64&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's take a look at how &lt;code&gt;index.js&lt;/code&gt; from &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-08"&gt;learn-08&lt;/a&gt; uses the resources in &lt;code&gt;static&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="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;loadImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./logo.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am from index.js&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;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#js_content .img_content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;append&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will see that we are introducing the image directly using the &lt;code&gt;./&lt;/code&gt; way to bring in the image, indicating that &lt;code&gt;webpack&lt;/code&gt; has a local server turned on that will map the contents of the &lt;code&gt;static&lt;/code&gt; directory we set up &lt;strong&gt;to the root directory&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you've used &lt;code&gt;vue-cli&lt;/code&gt; before, you'll appreciate that this is what the &lt;code&gt;public&lt;/code&gt; folder in &lt;code&gt;vue-cli&lt;/code&gt; does.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can access the contents of the set &lt;code&gt;devServer.static.directory&lt;/code&gt; folder in the following way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normally &lt;code&gt;devServer.static.directory&lt;/code&gt; is mapped to the root directory, so we access it using &lt;code&gt;http://[devServer.host]:[devServer.port]/[devServer.static.directory]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If you want to change the access path, you can add &lt;code&gt;devServer.static.publicPath&lt;/code&gt; configuration. In this case, you can use &lt;code&gt;http://[devServer.host]:[devServer.port]/[devServer.static.publicPath]/[devServer.static.directory]&lt;/code&gt; for accessing&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Summary
&lt;/h4&gt;

&lt;p&gt;After analyzing the explanation above, we can conclude that the correct understanding of &lt;code&gt;devServer.static&lt;/code&gt; should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a directory for static resources that are not compiled by &lt;code&gt;webpack&lt;/code&gt; (e.g., images, third-party resources, etc.), so it won't be &lt;code&gt;hashed&lt;/code&gt; or &lt;code&gt;base64&lt;/code&gt;; it is a directory.&lt;/li&gt;
&lt;li&gt;When opening a &lt;code&gt;webpack&lt;/code&gt; local server, it is usually mapped to the root directory of our project (you can change the access directory via &lt;code&gt;devServe.static.publicPath&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Since it's not compiled and packaged by &lt;code&gt;webpack&lt;/code&gt;, we usually end up copying &lt;code&gt;devServer.static.directory&lt;/code&gt; to the root of &lt;code&gt;output.path&lt;/code&gt; with &lt;code&gt;copy-webpack-plugin&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It functions much like the &lt;code&gt;public&lt;/code&gt; folder inside &lt;code&gt;vue-cli&lt;/code&gt;, which we can use when we develop via &lt;code&gt;./&lt;/code&gt; or &lt;code&gt;../&lt;/code&gt; to access that resource (depending on the directory relationship).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Core
&lt;/h2&gt;

&lt;p&gt;Okay, so we know from the above explanation of the development environment configuration and &lt;code&gt;devServer&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The development environment configuration is kept simple, so resources such as &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt;, etc. are mapped directly to the root directory (&lt;code&gt;css&lt;/code&gt; is inserted into &lt;code&gt;&amp;lt;head /&amp;gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The development environment &lt;code&gt;webpack&lt;/code&gt; also maps &lt;code&gt;devServer.static.directory&lt;/code&gt; to the root directory when the local server is enabled.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So after running the development environment configuration, our structure directory is roughly as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dist
├── ecj-cli-b6fa8.png
├── index-c466b.js
├── index.html
└── logo.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configuration varies from person to person, &lt;strong&gt;the most important thing is that after we configure the development environment, production environment, the compilation and packaging of output content, directory, a good understanding of the structure of the mind, which is very important&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is very important that we have a good understanding of the directory structure of the compiled and packaged output, which is very important.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can see the structure of the development environment by the following method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;devServer.devMiddleware.writeToDisk: true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http://[devServer.host]:[devServer.port]/webpack-dev-server&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Chrome &lt;code&gt;F12 -&amp;gt; Sources&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Finally
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This article learned how to configure the development environment, the next article will explain the configuration of the production environment before some of the preparations, so that we can better learn the production environment configuration&lt;/li&gt;
&lt;li&gt;The cases covered in the article have been uploaded to &lt;a href="https://github.com/limingcan562/learn-webpack-5"&gt;github&lt;/a&gt;, and you are really welcome to &lt;code&gt;star&lt;/code&gt; or &lt;code&gt;fork&lt;/code&gt; them.&lt;/li&gt;
&lt;li&gt;You can support me by following my &lt;a href="https://github.com/limingcan562"&gt;github&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regarding the content of the article, if you have similarities and differences, feel free to discuss them in the comments section!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>You'll be more productive learning webpack with these methods</title>
      <dc:creator>Lee</dc:creator>
      <pubDate>Tue, 11 Jul 2023 09:42:00 +0000</pubDate>
      <link>https://dev.to/limingcan562/youll-be-more-productive-learning-webpack-with-these-methods-469p</link>
      <guid>https://dev.to/limingcan562/youll-be-more-productive-learning-webpack-with-these-methods-469p</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;As explained in the previous article - &lt;a href="https://dev.to/limingcan562/take-your-webpack-to-the-next-level-by-understanding-these-concepts-h2p"&gt;Take your webpack to the next level by understanding these concepts&lt;/a&gt;, we have a general understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some of the concepts that arise in using &lt;code&gt;webpack&lt;/code&gt; process&lt;/li&gt;
&lt;li&gt;Combined with the concepts of the packaging process, we analyzed the &lt;code&gt;webpack&lt;/code&gt; packaging process, so that we can have a general understanding of the packaging process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We all know that in the actual development process, we use &lt;code&gt;webpack&lt;/code&gt; only to finally output &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;html&lt;/code&gt; and other front-end resources that the browser can run.  &lt;/p&gt;

&lt;p&gt;So, in order to be more close to the actual situation, in this article, we will learn how to configure &lt;code&gt;webpack&lt;/code&gt; with the purpose of &lt;strong&gt;how to output &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;html&lt;/code&gt; and other front-end resources.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Outline
&lt;/h2&gt;

&lt;p&gt;This article explains the following basic configuration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handling &lt;code&gt;css, less&lt;/code&gt; files&lt;/li&gt;
&lt;li&gt;Handling &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt;, &lt;code&gt;audio&lt;/code&gt; and other front-end resource files.&lt;/li&gt;
&lt;li&gt;Compiling &lt;code&gt;es6+&lt;/code&gt; syntax and &lt;code&gt;API&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Processing &lt;code&gt;html&lt;/code&gt; files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;webpack&lt;/code&gt; documentation is pretty detailed, so I don't want to copy and paste it and write it all over again, so it's better to just read the documentation.  &lt;/p&gt;

&lt;p&gt;Learning can't be learned the hard way, there should be some methods and techniques. Therefore, I would like to &lt;strong&gt;summarize some methods&lt;/strong&gt; in the process of explaining the configuration, so that we can better understand the configuration of &lt;code&gt;webpack&lt;/code&gt;. In this way, some similar configurations, we can match them by looking at the documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remarks
&lt;/h2&gt;

&lt;p&gt;The article has been uploaded to &lt;a href="https://github.com/limingcan562/learn-webpack-5"&gt;github&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the sake of reading convenience, the article only posted the relevant code, it is recommended to &lt;code&gt;fork&lt;/code&gt; it to see the complete code; or follow the article together with the side of the knock, so that the impression will be more profound!&lt;/li&gt;
&lt;li&gt;It's not easy to create, if you think it's helpful, welcome to star🌟!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Handling &lt;code&gt;css, less&lt;/code&gt; files
&lt;/h2&gt;

&lt;p&gt;Let's start with the &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-03"&gt;learn-03&lt;/a&gt; case to see how &lt;code&gt;webpack&lt;/code&gt; handles style files.&lt;/p&gt;

&lt;p&gt;We install the appropriate &lt;code&gt;loader&lt;/code&gt; to parse them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;less-loader&lt;/code&gt;: parses &lt;code&gt;less&lt;/code&gt; into &lt;code&gt;css&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;css-loader&lt;/code&gt;: parses &lt;code&gt;css&lt;/code&gt; into valid modules recognized by &lt;code&gt;webpack&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style-loader&lt;/code&gt;: parses &lt;code&gt;css&lt;/code&gt; and inserts it into &lt;code&gt;&amp;lt;header /&amp;gt;&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install less less-loader css-loader style-loader -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then configure it:&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="c1"&gt;// webpack.config.js&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="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|less&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&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="s1"&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="s1"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;less-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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parsing of &lt;code&gt;loader&lt;/code&gt; is performed in reverse order (or right-to-left), so this configuration will be performed in the following order (&lt;code&gt;less-loader&lt;/code&gt; → &lt;code&gt;css-loader&lt;/code&gt; → &lt;code&gt;style-loader&lt;/code&gt;):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;parses &lt;code&gt;less&lt;/code&gt; into &lt;code&gt;css&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;pass the result to &lt;code&gt;css-loader&lt;/code&gt;, which parses it into a valid module recognized by &lt;code&gt;webpack&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;passes the result to &lt;code&gt;style-loader&lt;/code&gt;, which inserts the parsed styles into &lt;code&gt;&amp;lt;header /&amp;gt;&lt;/code&gt;) in the following order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;less&lt;/code&gt;, &lt;code&gt;css&lt;/code&gt;, and entry file code is:&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="c1"&gt;// index.less&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;h1&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="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;margin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="nx"&gt;px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* index.css */&lt;/span&gt;
&lt;span class="nx"&gt;h2&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="nx"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/index.less&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/index.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;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D4dL2CPP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ae6zexgzt3nytkgeu1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D4dL2CPP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ae6zexgzt3nytkgeu1i.png" alt="compile_style" width="800" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll see that the style we wrote was processed successfully and inserted into the &lt;code&gt;&amp;lt;head/&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;We &lt;a href="https://dev.to/limingcan562/take-your-webpack-to-the-next-level-by-understanding-these-concepts-h2p"&gt;previous post&lt;/a&gt; said:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The resource files that we come in via &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;require&lt;/code&gt;, or each file in our project, can be viewed as an individual module&lt;/li&gt;
&lt;li&gt;The role of the &lt;code&gt;Loader&lt;/code&gt; is to convert these modules into valid modules that &lt;code&gt;webpack&lt;/code&gt; recognizes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; humanely exposes the &lt;code&gt;module&lt;/code&gt; configuration item, which is specifically designed to configure the relevant &lt;code&gt;loader&lt;/code&gt; to parse the corresponding &lt;code&gt;module&lt;/code&gt;. So if we want to parse some module used in our source code, we should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;download the appropriate &lt;code&gt;loader&lt;/code&gt; first&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find the &lt;code&gt;module&lt;/code&gt; configuration directly&lt;/strong&gt; and configure it according to the &lt;code&gt;loader&lt;/code&gt; usage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, we can try to configure our own &lt;code&gt;.scss&lt;/code&gt; or &lt;code&gt;.txt&lt;/code&gt; files according to the above summarized methods and ideas.&lt;/p&gt;




&lt;p&gt;OK, we successfully got the styles (&lt;code&gt;css&lt;/code&gt;) we need for our project, next we will see how to deal with images (&lt;code&gt;img&lt;/code&gt;) and other front-end resource files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling front-end resource files
&lt;/h2&gt;

&lt;p&gt;There are a lot of front-end resource files, here, we roughly divide them into two categories first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Common class: &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt;, &lt;code&gt;video&lt;/code&gt; and so on.&lt;/li&gt;
&lt;li&gt;Special category: &lt;code&gt;.csv&lt;/code&gt;, &lt;code&gt;.xml&lt;/code&gt; and so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These resources are also modules as long as they are &lt;code&gt;imported&lt;/code&gt; or &lt;code&gt;required&lt;/code&gt; into our source code. Since they are modules, we need to install the appropriate &lt;code&gt;loader&lt;/code&gt; to parse them, and then configure them in the &lt;code&gt;module&lt;/code&gt; configuration.&lt;/p&gt;

&lt;p&gt;Prior to &lt;code&gt;wepack5&lt;/code&gt;, &lt;strong&gt;For resources of commonly used classes&lt;/strong&gt;, we generally needed to install the following &lt;code&gt;loader&lt;/code&gt; to parse them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;raw-loader&lt;/code&gt;: parses files into strings&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;file-loader&lt;/code&gt;: parses files coming in from &lt;code&gt;import/require()&lt;/code&gt; into a fully referenced &lt;code&gt;url&lt;/code&gt; and sends the file to the output directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;url-loader&lt;/code&gt;: can convert files to &lt;code&gt;base64&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We often use &lt;code&gt;file-loader&lt;/code&gt; and &lt;code&gt;url-loader&lt;/code&gt;. This is because common resources are often introduced into our projects as &lt;code&gt;url&lt;/code&gt;, or converted to &lt;code&gt;base64&lt;/code&gt; in order to reduce &lt;code&gt;http&lt;/code&gt; requests.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;after &lt;code&gt;webpack5&lt;/code&gt;, &lt;code&gt;webpack&lt;/code&gt; has built-in the functions of the above several &lt;code&gt;loaders&lt;/code&gt;, so we can no longer need to install the above &lt;code&gt;loaders&lt;/code&gt;; we can directly use &lt;code&gt;webpack&lt;/code&gt;'s own &lt;a href="https://webpack.js.org/guides/asset-modules/"&gt;asset module&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The resource module built in after &lt;code&gt;webpack5&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;asset/source&lt;/code&gt;: equivalent to &lt;code&gt;raw-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;asset/resource&lt;/code&gt;: equivalent to &lt;code&gt;file-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;asset/inline&lt;/code&gt;: equivalent to a &lt;code&gt;url-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;asset&lt;/code&gt;: more flexible, has both &lt;code&gt;asset/resource&lt;/code&gt; and &lt;code&gt;asset/inline&lt;/code&gt;. If you set a file size limit, the file will be converted to &lt;code&gt;base64&lt;/code&gt; if it doesn't exceed the limit; if you don't set a size limit, it works the same as &lt;code&gt;file-loader&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This is why I love &lt;code&gt;webpack5&lt;/code&gt; so much, because we don't have to install so many messy &lt;code&gt;loaders&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's use the case of &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-04"&gt;learn-04&lt;/a&gt; to see how we can use the resource module to parse our front-end resources.&lt;/p&gt;

&lt;p&gt;Let's add the configuration from the previous chapter on parsing styles together and use the image resources in &lt;code&gt;less&lt;/code&gt; and &lt;code&gt;js&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;We have two images:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;preview.gif&lt;/code&gt;: size &lt;code&gt;349kb&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ecj-cli.png&lt;/code&gt;: size &lt;code&gt;4kb&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// webpack.config.js&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="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|less&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&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="s1"&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="s1"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;less-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="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="s1"&gt;asset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;dataUrlCondition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                      &lt;span class="na"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;// // When the image size is &amp;lt; 10kb it will be converted to base64&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="na"&gt;generator&lt;/span&gt;&lt;span class="p"&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="s1"&gt;[name][ext]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Set the name of the output file&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;Entry file:&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="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/index.less&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="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Image&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;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./assets/ecj-cli.png&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;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#js_logo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;append&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;Style:&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;.bg_logo&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="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1434&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="m"&gt;3px&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="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1212&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;background-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url('./preview.gif')&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&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;Output results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eV8YpRS_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhlkyxxspv0uluz9n95k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eV8YpRS_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhlkyxxspv0uluz9n95k.png" alt="compile_resource" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll see that the style and &lt;code&gt;js&lt;/code&gt; both process the image successfully:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;preview.gif&lt;/code&gt; is larger than &lt;code&gt;10kb&lt;/code&gt;, so instead of being converted to &lt;code&gt;base64&lt;/code&gt;, a full reference path is returned (equivalent to &lt;code&gt;file-loader&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ejc-cli.png&lt;/code&gt; is smaller than &lt;code&gt;10kb&lt;/code&gt; so it was converted to &lt;code&gt;base64&lt;/code&gt; (equivalent to &lt;code&gt;url-loader&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The gif above is a command line tool &lt;a href="https://github.com/limingcan562/ejc-cli"&gt;ejc-cli&lt;/a&gt; that I developed that is very useful in everyday projects. It can convert the &lt;code&gt;Excel&lt;/code&gt; data collected by the docking staff into &lt;code&gt;json&lt;/code&gt; data in a certain format which is needed by our code. It is very convenient for us to develop and maintain the data, and also to interface with other people.  &lt;/p&gt;

&lt;p&gt;If you are interested, you can check out 👉🏻 &lt;a href="https://github.com/limingcan562/ejc-cli"&gt;learn about ejc-cli&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Resources that are imported into our source code via &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;require&lt;/code&gt; are also modules. So you need to download the appropriate &lt;code&gt;loader&lt;/code&gt; and configure it in the &lt;code&gt;module&lt;/code&gt; configuration.&lt;/p&gt;

&lt;p&gt;For common resources (&lt;code&gt;img&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt;, &lt;code&gt;video&lt;/code&gt;, &lt;code&gt;audio&lt;/code&gt;, etc.):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because they are commonly used, &lt;code&gt;webpack5&lt;/code&gt; has built-in functionality to handle them, so that we don't need to install an additional &lt;code&gt;loader&lt;/code&gt;, and we use the &lt;code&gt;asset module&lt;/code&gt; to manage them directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;We generally use &lt;code&gt;asset/resource&lt;/code&gt;, &lt;code&gt;asset/inline&lt;/code&gt;, &lt;code&gt;asset&lt;/code&gt; modules to manage common class resources&lt;/strong&gt;. Because the resources of our common classes are often introduced in our projects, we need to parse them to introduce the full &lt;code&gt;url&lt;/code&gt;, or convert them to &lt;code&gt;base64&lt;/code&gt; in order to reduce the number of &lt;code&gt;http&lt;/code&gt; requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For special classes of resources (&lt;code&gt;.csv&lt;/code&gt;, &lt;code&gt;.xml&lt;/code&gt;, etc.):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can understand that because they are not commonly used, &lt;code&gt;webpack&lt;/code&gt; does not have their functionality built in.&lt;/li&gt;
&lt;li&gt;Therefore, we need to install the appropriate &lt;code&gt;loader&lt;/code&gt; to parse them. For example, if we want to parse a &lt;code&gt;.csv&lt;/code&gt; file, we need to install the &lt;code&gt;csv-loader&lt;/code&gt; to parse it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can try to configure ourselves to handle &lt;code&gt;.video&lt;/code&gt; or &lt;code&gt;font&lt;/code&gt; as summarized above.&lt;/p&gt;




&lt;p&gt;OK, so far, we've got &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;. Let's see how we can get our &lt;code&gt;js&lt;/code&gt; through &lt;code&gt;webpack&lt;/code&gt; processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiling &lt;code&gt;es6+&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To this day, &lt;code&gt;js&lt;/code&gt; is evolving rapidly, and every year some new syntax or new &lt;code&gt;API&lt;/code&gt; appears in order to improve development efficiency and code quality.&lt;/p&gt;

&lt;p&gt;For example, in terms of syntax appeared:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrow functions: &lt;code&gt;()=&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;class syntactic sugar: &lt;code&gt;class People {}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Deconstructed assignment: &lt;code&gt;const [x, y] = [1, 2];&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the &lt;code&gt;API&lt;/code&gt; side again:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Array.prototype.includes()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Object.keys()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But these new additions are not supported on some lower browsers, so we need to use &lt;code&gt;webpack&lt;/code&gt; to compile our &lt;code&gt;es6+&lt;/code&gt; into a version that these lower browsers can support.&lt;/p&gt;

&lt;p&gt;Let's start with the &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-05"&gt;learn-05&lt;/a&gt; case and see how we configure &lt;code&gt;webpack&lt;/code&gt; to compile &lt;code&gt;es6+&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We install the relevant dependencies:&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="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/core"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.22.8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/plugin-transform-runtime"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.22.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.22.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"babel-loader"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^9.1.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"webpack"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.88.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"webpack-cli"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.1.4"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"core-js"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^3.31.1"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Entry files. Uses &lt;code&gt;es6+&lt;/code&gt; syntax such as &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;Promise&lt;/code&gt;, &lt;code&gt;()=&amp;gt;{}&lt;/code&gt; and &lt;code&gt;API&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="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;People&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;Lee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;People&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lee&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;Lee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hi, I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure &lt;code&gt;webpack&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="c1"&gt;// webpack.config.js&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="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;m&lt;/span&gt;&lt;span class="se"&gt;?&lt;/span&gt;&lt;span class="sr"&gt;js$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;exclude&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;node_modules|bower_components&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&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="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="s1"&gt;babel-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;Configure &lt;code&gt;Babel&lt;/code&gt;. Create a new &lt;code&gt;babel.config.js&lt;/code&gt; file in the project root directory:&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="c1"&gt;// babel.config.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;presets&lt;/span&gt; &lt;span class="o"&gt;=&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="s1"&gt;@babel/preset-env&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="na"&gt;useBuiltIns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;usage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;corejs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;core-js/package.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;proposals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;plugins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@babel/plugin-transform-runtime&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="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="nx"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the target browser to be compatible with. Add the &lt;code&gt;browserslist&lt;/code&gt; field to &lt;code&gt;package.json&lt;/code&gt;:&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;"browserslist"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"ie 11"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result of running it in &lt;code&gt;ie 11&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pRVQipwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3icnvu2wofeg8byu2b9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pRVQipwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3icnvu2wofeg8byu2b9.png" alt="compile_es6" width="285" height="80"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;es6+&lt;/code&gt; we wrote runs successfully in &lt;code&gt;ie 11&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise&lt;/code&gt;, the &lt;code&gt;es6+&lt;/code&gt; &lt;code&gt;API&lt;/code&gt;, has also been added to &lt;code&gt;ie 11&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It means that our compilation of &lt;code&gt;es6+&lt;/code&gt; was successful.&lt;/p&gt;

&lt;h3&gt;
  
  
  About &lt;code&gt;Babel&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In the configuration above, you will notice that if we want to compile &lt;code&gt;es6+&lt;/code&gt; with &lt;code&gt;webpack&lt;/code&gt;, we also need to add the &lt;code&gt;babel.config.js&lt;/code&gt; file to the root directory, which is a much more complicated and troublesome step than dealing with other modules. This is because &lt;strong&gt;compiling the core of &lt;code&gt;es6+&lt;/code&gt; requires &lt;code&gt;Babel&lt;/code&gt; to work with it&lt;/strong&gt;. &lt;code&gt;Babel&lt;/code&gt; is a tool for compiling &lt;code&gt;es6+&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since this post is mainly about &lt;code&gt;webpack&lt;/code&gt;, we'll just give a general mention of &lt;code&gt;Babel&lt;/code&gt; here. Here are some common configurations for &lt;code&gt;Babel&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the project we are developing is an &lt;strong&gt;application&lt;/strong&gt; or a &lt;strong&gt;large project&lt;/strong&gt;, we can configure it like this:
&lt;/li&gt;
&lt;/ul&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;presets&lt;/span&gt; &lt;span class="o"&gt;=&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="s1"&gt;@babel/preset-env&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="na"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;useBuiltIns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;entry&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// or useBuiltIns: 'usage',&lt;/span&gt;
            &lt;span class="na"&gt;corejs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3.27.2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;proposals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;plugins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@babel/plugin-transform-runtime&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="c1"&gt;// If use useBuiltIns: 'entry'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;core-js/stable&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If we are trying to develop a &lt;strong&gt;third party library&lt;/strong&gt;, we can configure it like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Babel配置&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;presets&lt;/span&gt; &lt;span class="o"&gt;=&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="s1"&gt;@babel/preset-env&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="na"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;plugins&lt;/span&gt; &lt;span class="o"&gt;=&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="s1"&gt;@babel/plugin-transform-runtime&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="na"&gt;corejs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;proposals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="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="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;js&lt;/code&gt; is &lt;code&gt;imported&lt;/code&gt; or &lt;code&gt;required&lt;/code&gt; into our source code, then it is also a module. So we have to download the corresponding &lt;code&gt;loader&lt;/code&gt; (&lt;code&gt;babel-loader&lt;/code&gt;) and configure it in the &lt;code&gt;module&lt;/code&gt; configuration.&lt;/li&gt;
&lt;li&gt;In addition, we need to configure &lt;code&gt;babel-loader&lt;/code&gt; and, most importantly, &lt;code&gt;Babel&lt;/code&gt; (&lt;code&gt;babel.config.js&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, if we want to "compile &lt;code&gt;es6+&lt;/code&gt;", we need to configure &lt;code&gt;webapck&lt;/code&gt;, and most importantly we need to configure &lt;code&gt;babel.config.js&lt;/code&gt;. &lt;strong&gt;If you don't want to just copy and paste the &lt;code&gt;Babel&lt;/code&gt; configuration, you have to learn about &lt;code&gt;Babel&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Remember what we use &lt;code&gt;webpack&lt;/code&gt; for? To output &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;html&lt;/code&gt; and other front-end resources that the browser can run.&lt;/p&gt;

&lt;p&gt;OK, so far we have processed &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt; through &lt;code&gt;webpack&lt;/code&gt;. Let's take a look at how we can get our &lt;code&gt;html&lt;/code&gt; through &lt;code&gt;webpack&lt;/code&gt; processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Processing &lt;code&gt;html&lt;/code&gt; files
&lt;/h2&gt;

&lt;p&gt;In the above example, we want to see the effect in the browser, we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new &lt;code&gt;html&lt;/code&gt; file in the root directory.&lt;/li&gt;
&lt;li&gt;In the new &lt;code&gt;html&lt;/code&gt; file, we need to bring in the packaged resource files manually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This process is too troublesome, is there a way, we just provide a &lt;code&gt;html&lt;/code&gt; template file, and then through the &lt;code&gt;webpack&lt;/code&gt; compilation, it automatically help us to pack the packaged files into the good. This way, we can run &lt;code&gt;html&lt;/code&gt; directly after packaging and see the effect in the browser.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; is so user-friendly, of course there is.&lt;/p&gt;

&lt;p&gt;Let's start by using the &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-06"&gt;learn-06&lt;/a&gt; example here to see how we can utilize &lt;code&gt;webpack&lt;/code&gt; to achieve the results we described above.&lt;/p&gt;

&lt;p&gt;Install the relevant plugins:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i html-webpack-plugin -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, the &lt;code&gt;webpack&lt;/code&gt; configuration is a little different:&lt;br&gt;
We set the packaged &lt;code&gt;js&lt;/code&gt; storage directory to be the &lt;code&gt;js&lt;/code&gt; folder; and we add a &lt;code&gt;hash&lt;/code&gt; value of &lt;code&gt;5&lt;/code&gt; lengths to the packaged &lt;code&gt;js&lt;/code&gt; name.&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="c1"&gt;// webpack.config.js&lt;/span&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="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html-webpack-plugin&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="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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;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="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="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/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="nx"&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="s1"&gt;./dist&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="s1"&gt;./js/[name]-[chunkhash:5].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;clean&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="nx"&gt;HtmlWebpackPlugin&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;./dist/[name].html&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;./index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HtmlWebpackPlugin&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The structure of the packaged file is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dist
├── index.html
└── js
    └── index-efbfd.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The packaged &lt;code&gt;html&lt;/code&gt; is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HKC5rUl9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cojte4sw78dbed6enysd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HKC5rUl9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cojte4sw78dbed6enysd.png" alt="compile_html" width="562" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our &lt;code&gt;html&lt;/code&gt; and &lt;code&gt;js&lt;/code&gt; are packaged and exported to the &lt;code&gt;dist&lt;/code&gt; folder, which is very convenient for us to deploy (before we created &lt;code&gt;index.html&lt;/code&gt; in the root directory, so there are only &lt;code&gt;img&lt;/code&gt; and &lt;code&gt;js&lt;/code&gt; files in the &lt;code&gt;dist&lt;/code&gt; folder after packaging).&lt;/li&gt;
&lt;li&gt;The packaged &lt;code&gt;html&lt;/code&gt; will automatically bring in our packaged &lt;code&gt;js&lt;/code&gt; file for us.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Analyzing
&lt;/h3&gt;

&lt;p&gt;As we can see from the above example, if we want &lt;code&gt;webpack&lt;/code&gt; to process &lt;code&gt;html&lt;/code&gt; files, we need to configure it in &lt;code&gt;plugins&lt;/code&gt; provided by &lt;code&gt;webpack&lt;/code&gt;. Some of you may wonder why &lt;code&gt;html&lt;/code&gt; is a module and why it is configured in &lt;code&gt;plugins&lt;/code&gt; and not in &lt;code&gt;module&lt;/code&gt;. Let's analyze it.&lt;/p&gt;

&lt;p&gt;All the files of our project can be considered as a module, but whether we configure them in &lt;code&gt;module&lt;/code&gt; or &lt;code&gt;plugins&lt;/code&gt; depends on what we want to use them for. Generally, if we use the module in our own source code, we need a &lt;code&gt;Loader&lt;/code&gt; to parse it**.&lt;/p&gt;

&lt;p&gt;When we explained how to deal with &lt;code&gt;img&lt;/code&gt; and &lt;code&gt;js&lt;/code&gt; above, our purpose &lt;strong&gt;was to parse these files in the source code&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;img&lt;/code&gt;, which we need to convert to &lt;code&gt;base64&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;js&lt;/code&gt;, we need to compile &lt;code&gt;es6+&lt;/code&gt; stuff into &lt;code&gt;es5&lt;/code&gt; and below&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And, our files are introduced into our project by &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;require()&lt;/code&gt;, so of course we need the appropriate &lt;code&gt;Loader&lt;/code&gt; to convert these modules.&lt;/p&gt;

&lt;p&gt;But when dealing with &lt;code&gt;html&lt;/code&gt; files, our purpose is not to parse the &lt;code&gt;html&lt;/code&gt;, we &lt;strong&gt;just want the packaged &lt;code&gt;html&lt;/code&gt; to automatically reference our &lt;code&gt;js&lt;/code&gt; and &lt;code&gt;img&lt;/code&gt;, which is more like using the auto-introduction feature&lt;/strong&gt;; moreover, we don't introduce our &lt;code&gt;html&lt;/code&gt; into our project, so of course we don't need the appropriate &lt;code&gt;Loader&lt;/code&gt; to parse it.&lt;/p&gt;

&lt;p&gt;Remember from our &lt;a href="https://dev.to/limingcan562/take-your-webpack-to-the-next-level-by-understanding-these-concepts-h2p"&gt;previous post&lt;/a&gt; we explained that &lt;code&gt;Loader&lt;/code&gt; is used to transform the modules, and &lt;code&gt;Plugin&lt;/code&gt; is used to enhance the &lt;code&gt;webpack&lt;/code&gt; package when compiling.&lt;/p&gt;

&lt;p&gt;In this case, we are dealing with the &lt;code&gt;html&lt;/code&gt; file, and our goal is to make the packaged &lt;code&gt;html&lt;/code&gt; automatically reference our &lt;code&gt;js&lt;/code&gt; and &lt;code&gt;img&lt;/code&gt;. So we need to configure the &lt;code&gt;plugins&lt;/code&gt; configuration item to enhance the &lt;code&gt;webpack&lt;/code&gt; packaging and compilation functionality.&lt;/p&gt;

&lt;p&gt;Similarly, suppose we want to parse an &lt;code&gt;html&lt;/code&gt; file in our project (in our project, &lt;code&gt;import html from '. /x.html'&lt;/code&gt;), then we have to install the appropriate &lt;code&gt;loader&lt;/code&gt; (&lt;code&gt;html-loader&lt;/code&gt;) and configure it in the &lt;code&gt;module&lt;/code&gt; configuration item.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Whether to configure in the &lt;code&gt;module&lt;/code&gt; or &lt;code&gt;plugins&lt;/code&gt; configuration item depends on the purpose for which we are using these modules.&lt;/p&gt;




&lt;p&gt;At this point, we have successfully configured &lt;code&gt;webpack&lt;/code&gt; to get the &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt;, and &lt;code&gt;html&lt;/code&gt; we need for a project; this is also equivalent to learning the basic configuration of &lt;code&gt;webpack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can use the methods summarized above to handle &lt;code&gt;sass&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt;, &lt;code&gt;video&lt;/code&gt; and other resources on your own to deepen your impression.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning Summary
&lt;/h2&gt;

&lt;p&gt;From the above explanation of some configuration items, we can have the following summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To parse &lt;code&gt;css&lt;/code&gt;, we need to install the &lt;code&gt;loader&lt;/code&gt; and configure it in the &lt;code&gt;module&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To parse &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt;, &lt;code&gt;video&lt;/code&gt;, etc., we don't need to install the &lt;code&gt;loader&lt;/code&gt; (&lt;code&gt;webpack&lt;/code&gt; has built-in functionality to parse them). We usually use &lt;code&gt;asset/resource&lt;/code&gt;, &lt;code&gt;asset/inline&lt;/code&gt;, &lt;code&gt;asset&lt;/code&gt; in &lt;code&gt;module&lt;/code&gt; configuration to parse them.&lt;/li&gt;
&lt;li&gt;To parse &lt;code&gt;js&lt;/code&gt;, we need to install the appropriate &lt;code&gt;loader&lt;/code&gt; (&lt;code&gt;babel-loader&lt;/code&gt;), configure it in the &lt;code&gt;module&lt;/code&gt; configuration, and most importantly, learn about &lt;code&gt;Babel&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To parse the &lt;code&gt;html&lt;/code&gt; file, we need to configure the &lt;code&gt;html-webpack-plugin&lt;/code&gt; plugin in the &lt;code&gt;plugins&lt;/code&gt; configuration field.&lt;/li&gt;
&lt;li&gt;Whether we configure it in the &lt;code&gt;module&lt;/code&gt; or &lt;code&gt;plugins&lt;/code&gt; configuration depends on what we want to do with the modules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the above learning, let's organize and output a complete &lt;code&gt;webpack&lt;/code&gt; base configuration to make it more impressive:&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="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html-webpack-plugin&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="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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;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="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="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/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="nx"&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="s1"&gt;./dist&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="s1"&gt;./js/[name]-[chunkhash:5].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;clean&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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|less&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&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="s1"&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="s1"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;less-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="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="s1"&gt;asset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;dataUrlCondition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="na"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;// When the image size is &amp;lt; 10kb it will be converted to base64&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="na"&gt;generator&lt;/span&gt;&lt;span class="p"&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="s1"&gt;[name]-[hash:5][ext]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Set the name of the output file&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;m&lt;/span&gt;&lt;span class="se"&gt;?&lt;/span&gt;&lt;span class="sr"&gt;js$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;exclude&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;node_modules|bower_components&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&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="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="s1"&gt;babel-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="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="nx"&gt;HtmlWebpackPlugin&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;./dist/[name].html&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="s1"&gt;./src/index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Webpack Basic Configuration&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The full base configuration is at &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-07"&gt;learn-07&lt;/a&gt;, and we recommend that you take a look at the full version, only &lt;code&gt;webpack.config.js&lt;/code&gt; is posted here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;I hope some of the methods summarized in this article for learning purposes will help you learn how to configure &lt;code&gt;webapck&lt;/code&gt; better.&lt;/li&gt;
&lt;li&gt;Later articles will be in-depth configuration. In real projects, most of them are divided into development and production environments, so &lt;strong&gt;we will learn how to configure &lt;code&gt;webpack&lt;/code&gt; differently for different environments of development and production, which is closer to our real projects&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You can support me by following my &lt;a href="https://github.com/limingcan562"&gt;github&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regarding the content of the article, if you have similarities and differences, feel free to discuss them in the comments section!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Take your webpack to the next level by understanding these concepts</title>
      <dc:creator>Lee</dc:creator>
      <pubDate>Mon, 26 Jun 2023 15:03:27 +0000</pubDate>
      <link>https://dev.to/limingcan562/take-your-webpack-to-the-next-level-by-understanding-these-concepts-h2p</link>
      <guid>https://dev.to/limingcan562/take-your-webpack-to-the-next-level-by-understanding-these-concepts-h2p</guid>
      <description>&lt;h2&gt;
  
  
  What is webpack?
&lt;/h2&gt;

&lt;p&gt;It is a packaging tool:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can take the &lt;code&gt;es6+&lt;/code&gt; &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;less&lt;/code&gt;, &lt;code&gt;sass&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt; and many other module resource files in our source code, and compile them through a series of processing to create front-end resources such as &lt;code&gt;css&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;html&lt;/code&gt; that our browsers can recognize and run&lt;/li&gt;
&lt;li&gt;At the same time, it also provides many highly configurable features (&lt;code&gt;tree shaking&lt;/code&gt;, &lt;code&gt;code-splitting&lt;/code&gt;, &lt;code&gt;module hot-replacement&lt;/code&gt;) to help us better optimize the management of our code and resources&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Remarks
&lt;/h2&gt;

&lt;p&gt;The article has been uploaded to &lt;a href="https://github.com/limingcan562/learn-webpack-5"&gt;github&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the sake of reading convenience, the article only posted the relevant code, it is recommended to &lt;code&gt;fork&lt;/code&gt; it to see the complete code; or follow the article together with the side of the knock, so that the impression will be more profound!&lt;/li&gt;
&lt;li&gt;It's not easy to create, if you think it's helpful, welcome to star🌟!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  webpack.config.js
&lt;/h2&gt;

&lt;p&gt;Let's take a look at the general framework of this configuration file to get an idea of what it looks like:&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="c1"&gt;// webpack.config.js&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="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="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;resolve&lt;/span&gt;&lt;span class="p"&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;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;optimization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
  &lt;span class="na"&gt;devServer&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;code&gt;webapck&lt;/code&gt; of course also supports many configuration items, but we usually use the general on these few, other configuration items can see &lt;a href="https://webpack.js.org/configuration/"&gt;documentation&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Attention:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; configuration file, the code inside is free to play around with, and the name of the configuration file doesn't have to be &lt;code&gt;webpack.config.js&lt;/code&gt;, just make sure that &lt;strong&gt;the final output is a &lt;code&gt;webpack&lt;/code&gt; configured object&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="c1"&gt;// myconfig.js&lt;/span&gt;

&lt;span class="c1"&gt;// Various logic codes&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="c1"&gt;// Just make sure to export a `webpack` configured object at the end&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="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="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="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&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;"build": "webpack --config myconfig.js"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Optimization
&lt;/h3&gt;

&lt;p&gt;All the configurations are written in one file, which is not good for our maintenance and development; and when we develop a project, there are multiple environments (local development environment, test environment, production environment), so we usually have a configuration file for different environments, and a configuration file that is common to all environments.&lt;br&gt;&lt;br&gt;
We usually merge the "common configuration file" with the "different environment configuration file", and then &lt;code&gt;webpack&lt;/code&gt; will run the merged configuration file.&lt;/p&gt;

&lt;p&gt;Configuration files are generally available as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Common configuration for development and production environments (&lt;code&gt;webpack.common.js&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Development environment configuration (&lt;code&gt;webpack.dev.js&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Production environment configuration (&lt;code&gt;webpack.pro.js&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Subsequent articles will teach you how to do this configuration method.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  entry
&lt;/h2&gt;

&lt;p&gt;It refers to the starting point where &lt;code&gt;webpack&lt;/code&gt; starts parsing and building the dependency graph. We generally configure &lt;code&gt;entry&lt;/code&gt; in the form of a &lt;code&gt;{key: value}&lt;/code&gt; object, e.g:&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;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="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="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;. /src/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;share&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;. /src/share.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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;We have two entry points in our project &lt;code&gt;index&lt;/code&gt;, &lt;code&gt;share&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;webpack&lt;/code&gt; will start building the modules they depend on from the &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;share&lt;/code&gt; portals, thus forming a dependency graph (more on this later)&lt;/li&gt;
&lt;li&gt;After packaging, the packaged file will be named with &lt;code&gt;key&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  output
&lt;/h2&gt;

&lt;p&gt;It's easy to understand, it sets the name of the compiled file after &lt;code&gt;webpack&lt;/code&gt; packaging and where it should be output to.&lt;/p&gt;

&lt;p&gt;Note that even if we set multiple &lt;code&gt;entry&lt;/code&gt; entries, we can only specify one &lt;code&gt;output&lt;/code&gt; configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Module
&lt;/h2&gt;

&lt;p&gt;The resource files we bring in via &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;require&lt;/code&gt;, or each file in our project, can be seen as a separate module. So, &lt;strong&gt;it can be a &lt;code&gt;js&lt;/code&gt; file, a &lt;code&gt;css&lt;/code&gt; file, or any kind of resource such as an image&lt;/strong&gt;. So we often see the following statement in our development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;import 'index.css'&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;import A from './x.js'&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;import pic from './x.png'&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The combination of modules will in turn form a &lt;code&gt;Chunk&lt;/code&gt;. &lt;code&gt;Chunk&lt;/code&gt; is a very important concept and will be discussed in detail later.&lt;/p&gt;

&lt;p&gt;Reference article: &lt;a href="https://webpack.js.org/concepts/modules/"&gt;Modules&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Loader
&lt;/h2&gt;

&lt;p&gt;As we know from above, each file in the project can be considered as a module, and since we have a variety of file types, we need something that can parse these modules into valid modules that &lt;code&gt;webpack&lt;/code&gt; can recognize and add them to the dependency graph, and that something is &lt;code&gt;Loader&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; is also kind enough to provide us with a &lt;code&gt;module&lt;/code&gt; configuration item, which is specifically used to configure different &lt;code&gt;loaders&lt;/code&gt; to parse different file types (modules). This is because &lt;code&gt;webpack&lt;/code&gt; can only parse &lt;code&gt;js&lt;/code&gt; and &lt;code&gt;json&lt;/code&gt; files by default, so if we want to parse different types of files (modules), we have to install the corresponding &lt;code&gt;loader&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="c1"&gt;// webpack.config.js&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="p"&gt;...,&lt;/span&gt;
 &lt;span class="na"&gt;modules&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="c1"&gt;// Parsing .txt files, using raw-loader&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;/.txt$/&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;raw-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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;loader&lt;/code&gt; has two properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;test&lt;/code&gt; property, which identifies which files will be converted.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;use&lt;/code&gt; property, which defines which &lt;code&gt;loader&lt;/code&gt; should be used when doing the conversion.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What other &lt;code&gt;loaders&lt;/code&gt; are available in &lt;code&gt;webpack&lt;/code&gt; can be seen &lt;a href="https://webpack.js.org/loaders/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Plugin
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Loader&lt;/code&gt; is used to transform modules, &lt;code&gt;Plugin&lt;/code&gt; is used to enhance the functionality of &lt;code&gt;webpack&lt;/code&gt; when it is packaged and compiled. So it generally has functions like packaging optimization, resource management, injection of environment variables, etc.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;webpack&lt;/code&gt; is also kind enough to provide us with a &lt;code&gt;plugin&lt;/code&gt; configuration item, we can just go to &lt;code&gt;plugin&lt;/code&gt; and configure what features we want to enhance, for example, if we define some global variables directly in our project:&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="c1"&gt;// webpack.config.js&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="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="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DefinePlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
   &lt;span class="na"&gt;AUTHOR_NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lee&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="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AUTHOR_NAME&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Lee&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check &lt;a href="https://webpack.js.org/plugins/"&gt;here&lt;/a&gt; to see what other plugins are available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency graph
&lt;/h2&gt;

&lt;p&gt;When we have a file that depends on another file, &lt;code&gt;webpack&lt;/code&gt; treats the file as having a direct "dependency".&lt;/p&gt;

&lt;p&gt;As a simple example, let's say we have three files that have the following relationship:&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="c1"&gt;// main.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am from index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// plugin.js&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am plugin.js&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;Let's analyze:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;main.js&lt;/code&gt;, &lt;code&gt;index.js&lt;/code&gt;, &lt;code&gt;plugin.js&lt;/code&gt; are equivalent to three modules;&lt;/li&gt;
&lt;li&gt;then &lt;code&gt;main.js&lt;/code&gt; introduces &lt;code&gt;index.js&lt;/code&gt;, and &lt;code&gt;index.js&lt;/code&gt; introduces &lt;code&gt;plugin.js&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;so all three files have a cross-reference relationship;&lt;/li&gt;
&lt;li&gt;a graph with references is formed as follows: &lt;code&gt;main.js&lt;/code&gt; → &lt;code&gt;index.js&lt;/code&gt; → &lt;code&gt;plugin.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To summarize:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; will start with &lt;code&gt;entry&lt;/code&gt; and use it as the starting point of the dependency graph; then analyze and process the &lt;code&gt;import&lt;/code&gt; inside &lt;code&gt;entry&lt;/code&gt; and keep recursively querying the dependencies similar to the above example, this process will finally form a graph with dependencies, this graph is the "dependency graph ".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; will then operate further based on this dependency graph.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⭐️ Chunk
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Chunk&lt;/code&gt; is a relatively important concept inside &lt;code&gt;webapck&lt;/code&gt;, if we understand it, we will have a good understanding of the whole process of &lt;code&gt;webpack&lt;/code&gt; packaging, code partitioning&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;In the &lt;code&gt;webpack&lt;/code&gt; packaging process, a module or a group of modules (any of the files in &lt;code&gt;webpack&lt;/code&gt; that we mentioned above can be considered as a module) will be combined into a whole, then this whole can be treated as a &lt;code&gt;Chunk&lt;/code&gt;&lt;/strong&gt;. Generally speaking, the &lt;code&gt;webpack&lt;/code&gt; packaging process has several &lt;code&gt;Chunks&lt;/code&gt; and will end up outputting several &lt;code&gt;js&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;Let's go through the case &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-01"&gt;learn-01&lt;/a&gt;, which is the simplest configuration, the simplest code, so that we can better understand what &lt;code&gt;Chunk&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;We have three &lt;code&gt;js&lt;/code&gt; files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt;: the entry file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async.js&lt;/code&gt;: file for asynchronous introduction&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;vendors.js&lt;/code&gt;: you can use it as some third-party dependency file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The file code and &lt;code&gt;webpack&lt;/code&gt; configuration are as follows:&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="c1"&gt;// webpack.config.js&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="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="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/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;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[name]-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="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./vendors&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="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* webpackChunkName: "async" */&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./async&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am from index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// async.js&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am from async.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// vendors.js&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am from vendors.js&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;When we see this, we can first guess how many files are packed out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analysis
&lt;/h3&gt;

&lt;p&gt;Let's first analyze the packaged files and their structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dist
├── async-bundle.js
└── index-bundle.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A total of two &lt;code&gt;js&lt;/code&gt; files are output.&lt;/p&gt;

&lt;p&gt;From the above, we know that&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each file can be seen as a &lt;code&gt;module&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;webpack&lt;/code&gt; packaging process, a module or a group of modules will be combined into a whole, then this whole can be treated as a &lt;code&gt;Chunk&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We proceed to analyze:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By looking at the &lt;code&gt;dist/index-bundle.js&lt;/code&gt; file, we will find that &lt;strong&gt;it contains &lt;code&gt;js&lt;/code&gt; files&lt;/strong&gt; that are not introduced asynchronously: the entry files &lt;code&gt;index.js&lt;/code&gt;, &lt;code&gt;vendors.js&lt;/code&gt; (and some code that was added by &lt;code&gt;webpack&lt;/code&gt; itself when it was packaged &lt;em&gt;runtime&lt;/em&gt;). This means that the two modules &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;vendors.js&lt;/code&gt; form a &lt;code&gt;Chunk&lt;/code&gt;. Here we call it &lt;code&gt;chunk[initial]&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;By looking at the &lt;code&gt;async-bundle.js&lt;/code&gt; file, it only contains the code for &lt;code&gt;async.js&lt;/code&gt;. This means that &lt;strong&gt;the module introduced asynchronously is split into a separate &lt;code&gt;Chunk&lt;/code&gt;&lt;/strong&gt;. Here we call this &lt;code&gt;Chunk&lt;/code&gt; &lt;code&gt;chunk[no-initial]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;chunk[initial]&lt;/code&gt; and &lt;code&gt;chunk[no-initial]&lt;/code&gt; come from the same entry &lt;code&gt;index.js&lt;/code&gt;, so the two &lt;code&gt;Chunks&lt;/code&gt; combined can be seen as a &lt;code&gt;Chunk&lt;/code&gt; group, or the entry files will form a whole &lt;code&gt;Chunk&lt;/code&gt; group. We call it &lt;code&gt;Chunk[index]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, I believe that by now, you should have a general impression of &lt;code&gt;Chunk&lt;/code&gt; formation, let's run through the process again.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;webpack&lt;/code&gt; is compiled, through our configuration:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it will first find &lt;code&gt;enrty&lt;/code&gt;, there are several &lt;code&gt;entry&lt;/code&gt;, it will use these &lt;code&gt;entry&lt;/code&gt; to form a &lt;code&gt;Chunk&lt;/code&gt; group (&lt;code&gt;Chunk[index]&lt;/code&gt; in the example)&lt;/li&gt;
&lt;li&gt;then analyze these &lt;code&gt;Chunk&lt;/code&gt; group, the entry &lt;code&gt;js&lt;/code&gt; and all the related dependency modules of this entry, to form a &lt;code&gt;chunk&lt;/code&gt; (&lt;code&gt;chunk[initial]&lt;/code&gt; in the example)&lt;/li&gt;
&lt;li&gt;if there is an asynchronous introduction of the module, the module is a separate &lt;code&gt;Chunk&lt;/code&gt; (&lt;code&gt;chunk[no-initial]&lt;/code&gt; in the example)&lt;/li&gt;
&lt;li&gt;finally package the output &lt;code&gt;chunk[initial]&lt;/code&gt; (&lt;code&gt;index-bundle.js&lt;/code&gt;), &lt;code&gt;chunk[no-initial]&lt;/code&gt; (&lt;code&gt;async-bundle.js&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the above example, &lt;code&gt;chunk&lt;/code&gt;, &lt;code&gt;module&lt;/code&gt; are related as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4ZltznpR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1u1ze63gvh575i0d8x3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4ZltznpR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1u1ze63gvh575i0d8x3.png" alt="chunk_pic" width="742" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Form
&lt;/h3&gt;

&lt;p&gt;From the above analysis, we can know that &lt;code&gt;Chunk&lt;/code&gt; has two forms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;initial&lt;/code&gt;: the initial one. Our entry &lt;code&gt;js&lt;/code&gt; and all the related dependency modules of this entry, combined into a collection, can be seen as a &lt;code&gt;Chunk&lt;/code&gt;. (i.e. &lt;code&gt;chunk[initial]&lt;/code&gt; composed of &lt;code&gt;index.js&lt;/code&gt;, &lt;code&gt;vendors.js&lt;/code&gt; above)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;non-initial&lt;/code&gt;: non-initial. Indicates that it is an asynchronously loaded module, if we use something like &lt;code&gt;import('. /A.js')&lt;/code&gt;, this &lt;code&gt;js&lt;/code&gt; will be split into a separate asynchronous &lt;code&gt;Chunk&lt;/code&gt;. (i.e. the &lt;code&gt;chunk[no-initial]&lt;/code&gt; composed of &lt;code&gt;async.js&lt;/code&gt; above)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to generate Chunk
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;With the &lt;code&gt;entry&lt;/code&gt; configuration, a &lt;code&gt;Chunk&lt;/code&gt; group is generated with the entry as a whole (this group will not be packed out, it will just form this &lt;code&gt;Chunk&lt;/code&gt; group)&lt;/li&gt;
&lt;li&gt;Generate &lt;code&gt;initial&lt;/code&gt; &lt;code&gt;Chunk&lt;/code&gt; with our entry &lt;code&gt;js&lt;/code&gt; and all related dependency modules of this entry&lt;/li&gt;
&lt;li&gt;Generate &lt;code&gt;non-initial&lt;/code&gt; &lt;code&gt;Chunk&lt;/code&gt; by asynchronous &lt;code&gt;import()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Generate other &lt;code&gt;chunk&lt;/code&gt; through &lt;code&gt;webpack&lt;/code&gt; powerful &lt;strong&gt;code splitting&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Through the above explanation and analysis, I hope you can have a rough process of &lt;code&gt;Chunk&lt;/code&gt; formation in your mind when you use &lt;code&gt;webpack&lt;/code&gt; in the future, which is very helpful for us to use code splitting.&lt;/p&gt;

&lt;p&gt;Reference article: &lt;a href="https://webpack.js.org/concepts/under-the-hood/"&gt;Revealing internal principles&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bundle
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Bundle&lt;/code&gt; refers to all the products of &lt;code&gt;webpack&lt;/code&gt; after packaging.  &lt;/p&gt;

&lt;p&gt;If our &lt;code&gt;output&lt;/code&gt; configuration output file directory is &lt;code&gt;dist&lt;/code&gt;, our &lt;code&gt;Bundle&lt;/code&gt; is all the products in the &lt;code&gt;dist&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Generally speaking there are a few &lt;code&gt;Chunks&lt;/code&gt; and a few &lt;code&gt;js&lt;/code&gt; &lt;code&gt;bundle&lt;/code&gt; files packaged out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Packing process
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A brief analysis
&lt;/h3&gt;

&lt;p&gt;In order to better understand the concepts parsed above, let's take a look at the &lt;code&gt;webpack&lt;/code&gt; packaging process and see in which processes the concepts above are reflected.&lt;/p&gt;

&lt;p&gt;After we run &lt;code&gt;webpack&lt;/code&gt; in the terminal, it will go through the following process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;read the configuration file we specified (&lt;code&gt;webpack.config.js&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;start with the entry &lt;code&gt;entry&lt;/code&gt;, analyze our &lt;code&gt;Module&lt;/code&gt; and recurse the dependencies between the modules of our entire project&lt;/li&gt;
&lt;li&gt;Load the appropriate &lt;code&gt;Loader&lt;/code&gt;, parse these &lt;code&gt;Modules&lt;/code&gt; into valid modules recognized by &lt;code&gt;webpack&lt;/code&gt; and add them to the &lt;code&gt;Dependency graph&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;the compilation process will trigger several events to execute the configured &lt;code&gt;Plugin&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;the analyzed modules are grouped to form &lt;code&gt;Chunk&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;according to the configuration file (&lt;code&gt;output&lt;/code&gt;), the final &lt;code&gt;Bundle&lt;/code&gt; is output&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The above process can be seen as three phases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialization phase (process 1)&lt;/li&gt;
&lt;li&gt;Compilation phase (process 2 - process 5)&lt;/li&gt;
&lt;li&gt;Output phase (process 6)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The actual packaging process of webpack is of course much more complex, and here, for better understanding, is simplified&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Experience
&lt;/h3&gt;

&lt;p&gt;Let's start from the practical side, with the case &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-02"&gt;learn-02&lt;/a&gt;, and use the actual code to have a deeper experience to understand the &lt;code&gt;webpack&lt;/code&gt; packaging process and the concepts involved.&lt;/p&gt;

&lt;p&gt;Let's take a look at the &lt;a href="https://github.com/limingcan562/learn-webpack-5/tree/main/learn-02"&gt;learn-02&lt;/a&gt; project structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;learn-02
├── index.html
├── package-lock.json
├── package.json
├── project.config.js
└── src
    ├── assets
    │   └── style.less
    ├── index.js
    ├── plugin
    │   ├── common.js
    │   ├── index-vendors.js
    │   └── share-vendors.js
    └── share.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's introduce the corresponding files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;index.html&lt;/code&gt;: used to run our packaged files and see the results&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;project.config.js&lt;/code&gt;: the &lt;code&gt;webpack&lt;/code&gt; configuration file (a separate name to distinguish it from &lt;code&gt;webpack.config.js&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style.less&lt;/code&gt;: style&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt;: entry file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;share.js&lt;/code&gt;: entry file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;common.js&lt;/code&gt;: holds the common methods, which will be referenced &lt;strong&gt;twice&lt;/strong&gt; by the two entry files, respectively&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index-vendors.js&lt;/code&gt;: can be used as some dependencies of &lt;code&gt;index.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;share-vendors.js&lt;/code&gt;: can be used as some dependencies of &lt;code&gt;share.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Relevant code from the following projects:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pgbID0pY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/os2ztqzjkwni68mvqv1w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pgbID0pY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/os2ztqzjkwni68mvqv1w.png" alt="directory" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After we see the configuration of &lt;code&gt;project.config.js&lt;/code&gt;, we can later guess how many files will be output after packaging as well.&lt;/p&gt;

&lt;p&gt;Okay, now let's start the analysis:&lt;/p&gt;

&lt;p&gt;1️⃣ When we run &lt;code&gt;npm run build&lt;/code&gt; in the terminal, &lt;code&gt;webpack&lt;/code&gt; will read the file &lt;code&gt;project.config.js&lt;/code&gt; that we specified&lt;/p&gt;

&lt;p&gt;2️⃣ Starting with &lt;code&gt;entry&lt;/code&gt;, we analyze our module. Our entry has two &lt;code&gt;index&lt;/code&gt;, &lt;code&gt;share&lt;/code&gt;, so this will form two &lt;code&gt;Chunk&lt;/code&gt; groups: &lt;code&gt;Chunk[index]&lt;/code&gt;, &lt;code&gt;Chunk[share]&lt;/code&gt;, and recurse our module's corresponding dependencies.&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;code&gt;index.js&lt;/code&gt; introduces &lt;code&gt;style.less&lt;/code&gt;, so it will load the corresponding &lt;code&gt;Loader&lt;/code&gt;, parse it into a valid module that &lt;code&gt;webpack&lt;/code&gt; can recognize, and add it to the dependency graph. This results in two dependency graphs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A dependency graph consisting of the entry &lt;code&gt;index.js&lt;/code&gt; and its dependencies (&lt;code&gt;index.js -&amp;gt; style.less, common.js, index-vendors.js&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;A dependency graph consisting of the entry &lt;code&gt;share.js&lt;/code&gt; and its dependencies (&lt;code&gt;share.js -&amp;gt; common.js, share-vendors.js&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4️⃣ These modules are then grouped into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;chunk[initial-index]&lt;/code&gt; composed of the entry &lt;code&gt;index.js&lt;/code&gt; and its dependencies;&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;chunk[initial-share]&lt;/code&gt; composed of the entry &lt;code&gt;share.js&lt;/code&gt; and its dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5️⃣ found that our configuration also splits &lt;code&gt;commonjs&lt;/code&gt; independently using code splitting, so it forms a separate &lt;code&gt;chunk[initial-common]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;6️⃣ At this point, &lt;code&gt;webpack&lt;/code&gt; has split into three &lt;code&gt;chunks&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;chunk[initial-index]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;chunk[initial-share]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;chunk[initial-common]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7️⃣ Final output &lt;code&gt;Bundle&lt;/code&gt; according to &lt;code&gt;output&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Again, the actual packing process is certainly much more complex&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Finally
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This article analyzes and explains some of the concepts involved in &lt;code&gt;webpack&lt;/code&gt;, especially the knowledge of &lt;code&gt;Chunk&lt;/code&gt; is more important. After understanding &lt;code&gt;Chunk&lt;/code&gt;, you will definitely have a better understanding of &lt;code&gt;webpack&lt;/code&gt;. Hopefully, after reading this article, we will have a general process in mind when using &lt;code&gt;webpack&lt;/code&gt;, and we will be able to tell how many &lt;code&gt;Chunk&lt;/code&gt;s there are.&lt;/li&gt;
&lt;li&gt;The following article will start to teach you how to configure &lt;code&gt;webpack&lt;/code&gt;, if you are interested you can stay tuned!&lt;/li&gt;
&lt;li&gt;You can support me by following my &lt;a href="https://github.com/limingcan562"&gt;github&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regarding the content of the article, if you have similarities and differences, feel free to discuss them in the comments section!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
