<?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>Forem: Sergey Melyukov</title>
    <description>The latest articles on Forem by Sergey Melyukov (@smelukov).</description>
    <link>https://forem.com/smelukov</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%2F336301%2F7ceb74dd-9dc5-4d61-b400-dfd3f6de554e.jpeg</url>
      <title>Forem: Sergey Melyukov</title>
      <link>https://forem.com/smelukov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/smelukov"/>
    <language>en</language>
    <item>
      <title>Brand new toolkit to analyze and validate your webpack bundle</title>
      <dc:creator>Sergey Melyukov</dc:creator>
      <pubDate>Tue, 14 Sep 2021 17:40:31 +0000</pubDate>
      <link>https://forem.com/smelukov/brand-new-toolkit-to-analyze-and-validate-your-webpack-bundle-456p</link>
      <guid>https://forem.com/smelukov/brand-new-toolkit-to-analyze-and-validate-your-webpack-bundle-456p</guid>
      <description>&lt;p&gt;Hi! 🖐&lt;br&gt;
In this post I would like to share Statoscope with you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/statoscope/statoscope" rel="noopener noreferrer"&gt;Statoscope&lt;/a&gt; is a modern tooklit for analyzing and validating your bundle.&lt;br&gt;
You can try Statoscope by simply dropping the stats of your build into &lt;a href="https://statoscope.tech" rel="noopener noreferrer"&gt;https://statoscope.tech&lt;/a&gt; or using a &lt;a href="https://github.com/statoscope/statoscope/tree/master/packages/webpack-plugin" rel="noopener noreferrer"&gt;webpack plugin&lt;/a&gt; that will generate a UI-based report about your build.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffju7ji268bsat38zhpx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffju7ji268bsat38zhpx9.png" alt="2021-09-14_20-55-13"&gt;&lt;/a&gt; &lt;br&gt;
This report will show you full information about your build, NPM packages duplicates, and even the download speed of your bundle to a client browser. With this UI report, you can make your own requests to stats and generate your own reports about your bundle. This is very similar to python notebook, but for webpack stats and is possible thanks to a &lt;a href="https://github.com/discoveryjs/jora#readme" rel="noopener noreferrer"&gt;special query language&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also, Statoscope is able to validate your build on CI, there is a special &lt;a href="https://github.com/statoscope/statoscope/tree/master/packages/cli" rel="noopener noreferrer"&gt;CLI tool&lt;/a&gt; for this. You can make sure that, for example, the time of building your bundle or loading the bundle to a client browser does not exceed the limit you have defined.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gu3wphn3pdi5q8jygdc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gu3wphn3pdi5q8jygdc.png"&gt;&lt;/a&gt;&lt;br&gt;
This is very useful on CI&lt;br&gt;
Also, you can generate a UI-report with validation result.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fafszia0pzx4aeddc3db8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fafszia0pzx4aeddc3db8.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You could learn more on Github or ask your question here or with Github issue.&lt;br&gt;
Also, &lt;a href="https://github.com/statoscope/statoscope/projects/2" rel="noopener noreferrer"&gt;there are many plans&lt;/a&gt; to improve Statoscope features.&lt;/p&gt;

&lt;p&gt;Your feedback is so precious 😊&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>statoscope</category>
      <category>validator</category>
      <category>stats</category>
    </item>
    <item>
      <title>Webpack 5: Progress percentage calculation</title>
      <dc:creator>Sergey Melyukov</dc:creator>
      <pubDate>Mon, 17 Feb 2020 15:41:07 +0000</pubDate>
      <link>https://forem.com/smelukov/webpack-5-progress-percentage-calculation-345o</link>
      <guid>https://forem.com/smelukov/webpack-5-progress-percentage-calculation-345o</guid>
      <description>&lt;p&gt;When you build a huge project with webpack, you might notice that progress percentage may get stuck on 66-68% for a long time. This is because by default webpack calculates progress percentage as the ratio of built modules count and total modules count. It's ok at first sight, but the problem is that the total modules count is unknown in advance. Any loader may add a few modules at any moment. Therefore the total modules count is changing during the build and it causes progress percentage jerking.&lt;br&gt;
To solve this problem you can specify an approximate quantity of modules in your 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="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ProgressPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;modulesCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It means that you need to start the build one time, get the total module count somehow and set this value to the plugin settings and then update it manually during the time.&lt;br&gt;
Another solution is to write a script that will count all the files in the project folder and then pass it value to the &lt;code&gt;modulesCount&lt;/code&gt;-field. This is based on the assumption that one module corresponds to one file, and this is not always the case. Moreover, we can't count &lt;code&gt;node_modules&lt;/code&gt; correctly because not all of these may be using in your project.&lt;br&gt;
These are dubious solutions...&lt;/p&gt;

&lt;p&gt;A year and a half ago, during migrating a huge project to webpack I was thinking that calculating of a progress percentage for the big projects should based on the entry points, not on the modules. There are a lot of entry points in huge projects. Most likely you will split a huge codebase into many entry points, not a single entry point. I made a PR to webpack. Its meaning was to calculate a progress percentage as the ratio of the number of the built entry points to the total number of entry points. The main benefit of this approach is that the total number of the entry points is known in advance and doesn't change during the build.&lt;br&gt;
This PR was merged half taken - it means that the ratio &lt;code&gt;built/total&lt;/code&gt; will be shown only in a console and will not affect percentage calculation: &lt;code&gt;252/420&lt;/code&gt; - it means that we have 420 entry points and only 252 was built at this moment.&lt;/p&gt;

&lt;p&gt;A couple of days ago another my PR was merged to webpack 5. Its meaning was to choose a percentage calculation strategy. By default, it calculates by number of modules as before. But with &lt;code&gt;percentBy&lt;/code&gt;-options you can change it. For example, to calculate the progress percentage totally by the number of entry points you may write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ProgressPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;percentBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;entries&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;But it's not the end. while I was writing this post, another idea that seemed to be obvious came to my mind.&lt;br&gt;
I was thinking that we can save total modules count to persistent cache at the end of a build, restore it before a next build will start and initialize &lt;code&gt;modulesCount&lt;/code&gt;-options of the plugin by this value.&lt;br&gt;
The first compilation will warm the cache, and the next compilations will use and update this value and will show a more realistic percentage, even if we calculate the percentage by modules count.&lt;br&gt;
It is unlikely that between the builds the codebase will change so that the number of modules will be changed so dramatically, well, or it is unlikely that this will happen too often.&lt;/p&gt;

&lt;p&gt;This PR was merged today and you can try it with &lt;code&gt;beta.14&lt;/code&gt; (not released yet) 🎉&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>webpack5</category>
    </item>
    <item>
      <title>Webpack 5 - Asset Modules</title>
      <dc:creator>Sergey Melyukov</dc:creator>
      <pubDate>Sun, 16 Feb 2020 10:47:36 +0000</pubDate>
      <link>https://forem.com/smelukov/webpack-5-asset-modules-2o3h</link>
      <guid>https://forem.com/smelukov/webpack-5-asset-modules-2o3h</guid>
      <description>&lt;p&gt;Hello. This post starts a series of articles about the new features of coming webpack 5. Why do I want to tell about the webpack? At least because I take an active part in its development and constantly delve its source code. In this article, I want to tell you about the Asset Modules - an experimental feature of webpack 5, which makes it possible to throw out a few habitual loaders, but not to cut these functionalities.&lt;/p&gt;

&lt;p&gt;Let's imagine that we need to bundle a page with some images and styles.&lt;/p&gt;

&lt;h2&gt;
  
  
  With webpack 4
&lt;/h2&gt;

&lt;p&gt;Webpack 4 configuration for this purpose may look like this:&lt;br&gt;
&lt;strong&gt;&lt;code&gt;webpack.config.js&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;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;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;svg$/&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;file-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;svgo-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="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$/&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="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;src/index.js&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;src/styles.css&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.logo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("/images/logo.svg")&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&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="n"&gt;cover&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;75px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;65px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dist/main.js
/dist/eb4c5fa504857.svg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the result all the svg-files will be handled by &lt;a href="https://github.com/svg/svgo"&gt;svgo&lt;/a&gt; and emitted into a bundle output path by &lt;a href="https://github.com/webpack-contrib/file-loader"&gt;file-loader&lt;/a&gt;. A styles will be transformed into:&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;.logo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("eb4c5fa504857.svg")&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&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="n"&gt;cover&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;75px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;65px&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;But later we may want to inline the images into css to optimize our page. Let's replace &lt;code&gt;file-loader&lt;/code&gt; to &lt;a href="https://github.com/webpack-contrib/url-loader"&gt;url-loader&lt;/a&gt; for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /\.svg$/,
        use: [
&lt;span class="gd"&gt;-         'file-loader',
&lt;/span&gt;&lt;span class="gi"&gt;+         'url-loader',
&lt;/span&gt;          'svgo-loader'
        ]
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;A bundled css will look like this:&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="gd"&gt;-   background: url("eb4c5fa504857.svg") no-repeat;
&lt;/span&gt;&lt;span class="gi"&gt;+   background: url("data:image/svg+xml;base64,....") no-repeat;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we would want to inline only small svg (e.g, less than 8kb) and emit the rest files into the output directory. &lt;code&gt;url-loader&lt;/code&gt; has the &lt;code&gt;limit&lt;/code&gt;-option for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /\.svg$/,
        use: [
&lt;span class="gd"&gt;-         'url-loader',
&lt;/span&gt;&lt;span class="gi"&gt;+         'url-loader?limit=8192',
&lt;/span&gt;          'svgo-loader'
        ]
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that only a small svg (less than 8kb) will be inlined, the rest svg-files will be emitted into the output directory, &lt;code&gt;url-loader&lt;/code&gt; will implicitly use &lt;code&gt;file-loader&lt;/code&gt; for it.&lt;/p&gt;

&lt;p&gt;The issue is solved... but wait a minute, with webpack 5 and its Asset Modules feature this issue may be resolved easier, without &lt;code&gt;url-loader&lt;/code&gt; and &lt;code&gt;file-loader&lt;/code&gt; (&lt;code&gt;url-loader&lt;/code&gt; implicitly use it for the files with size less than specified in &lt;code&gt;limit&lt;/code&gt;-option).&lt;/p&gt;

&lt;h2&gt;
  
  
  With webpack 5
&lt;/h2&gt;

&lt;p&gt;First, we need to explicitly specify that we want to use the Asset Modules. Let's specify it in our configuration:&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="p"&gt;module.exports = {
&lt;/span&gt;  // ...
&lt;span class="gi"&gt;+ experiments: {
+   asset: true
+ }
&lt;/span&gt;&lt;span class="err"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Asset Modules is an experimental feature at this moment and we are collecting a feedback from the users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, we just need to mark our svg-files as &lt;code&gt;asset&lt;/code&gt; and that's it. All that I was described related with &lt;code&gt;file-loader&lt;/code&gt; and &lt;code&gt;url-loader&lt;/code&gt; above will work out of the box, without any loaders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /\.svg$/,
&lt;span class="gd"&gt;-       use: [
-         'url-loader?limit=8000',
-         'svgo-loader'
-       ]
&lt;/span&gt;&lt;span class="gi"&gt;+       type: 'asset',
+       use: 'svgo-loader'
&lt;/span&gt;      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, for the modules that match a rule with &lt;code&gt;type: 'asset'&lt;/code&gt; the next logic will be applied: If a module size is less than 8kb (by default), then inline it to the bundle, otherwise emit its file to the output directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that &lt;code&gt;use&lt;/code&gt;-property is also taken into account (if specified)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's not the end, there are some other module-types besides &lt;code&gt;asset&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  asset/inline
&lt;/h3&gt;

&lt;p&gt;Just like the &lt;code&gt;url-loader&lt;/code&gt;. All the modules that match a rule with &lt;code&gt;type: 'asset/inline'&lt;/code&gt; will be inlined to the bundle as data-url:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /\.svg$/,
&lt;span class="gd"&gt;-       type: 'asset',
&lt;/span&gt;&lt;span class="gi"&gt;+       type: 'asset/inline',
&lt;/span&gt;        use: 'svgo-loader'
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also we can specify a custom data-url generator for &lt;code&gt;type: 'asset/inline'&lt;/code&gt;.&lt;br&gt;
For example, we can use &lt;a href="https://github.com/tigt/mini-svg-data-uri"&gt;mini-svg-data-uri&lt;/a&gt; for the svg-files. It transforms a svg as the data-url but without base64. It reduces a bundle size a little bit:&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="gi"&gt;+ const miniSVGDataURI = require('mini-svg-data-uri');
&lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt; ...
      {
        test: /\.svg$/,
        type: 'asset/inline',
&lt;span class="gi"&gt;+       generator: {
+         dataUrl(content) {
+           content = content.toString();
+           return miniSVGDataURI(content);
+         }
+       },
&lt;/span&gt;        use: 'svgo-loader'
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our css will be transformed into this:&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="gd"&gt;-   background: url("data:image/svg+xml;base64,....") no-repeat;
&lt;/span&gt;&lt;span class="gi"&gt;+   background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg'....") no-repeat;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We even can use a custom data-url generator and the &lt;code&gt;use&lt;/code&gt;-property&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  asset/resource
&lt;/h3&gt;

&lt;p&gt;Just like the &lt;a href="https://github.com/webpack-contrib/file-loader"&gt;file-loader&lt;/a&gt;. All the modules that match a rule with &lt;code&gt;type: 'asset/resource'&lt;/code&gt; will be emitted into the output directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /\.svg$/,
&lt;span class="gd"&gt;-       type: 'asset/inline',
&lt;/span&gt;&lt;span class="gi"&gt;+       type: 'asset/resource',
&lt;/span&gt;&lt;span class="gd"&gt;-       generator: {
-         dataUrl(content) {
-           content = content.toString();
-           return miniSVGDataURI(content);
-         }
-       },
&lt;/span&gt;        use: 'svgo-loader'
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output path for Asset Modules
&lt;/h4&gt;

&lt;p&gt;By default, all the modules with type &lt;code&gt;asset/resource&lt;/code&gt; are emitting into the root of the output directory (&lt;code&gt;dist&lt;/code&gt; by default). But with &lt;code&gt;output.assetModuleFilename&lt;/code&gt; you can change this behaviour:&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="p"&gt;module.exports = {
&lt;/span&gt;&lt;span class="gi"&gt;+ output: {
+   assetModuleFilename: 'assets/[name][ext]'
+ },
&lt;/span&gt;  // ...
&lt;span class="err"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dist/main.js
/dist/assets/logo.svg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By the way if we replace &lt;code&gt;[name]&lt;/code&gt; to &lt;code&gt;[hash]&lt;/code&gt; then we get a perfect base for assets long term caching:&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="p"&gt;module.exports = {
&lt;/span&gt;  output: {
&lt;span class="gd"&gt;-    assetModuleFilename: 'assets/[name][ext]'
&lt;/span&gt;&lt;span class="gi"&gt;+    assetModuleFilename: 'assets/[hash][ext]'
&lt;/span&gt;  },
  // ...
&lt;span class="err"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dist/main.js
/dist/assets/eb4c5fa504857.svg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also we can chage &lt;code&gt;assetModuleFilename&lt;/code&gt; only for specific asset-rule. E.g. we may emit the svg-icons into the &lt;code&gt;dist/icons&lt;/code&gt; and the rest asset-modules into the &lt;code&gt;dist/assets&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /\.svg$/,
        type: 'asset/resource',
&lt;span class="gi"&gt;+       generator: {
+         filename: 'icons/[hash][ext]'
+       },
&lt;/span&gt;        use: 'svgo-loader'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dist/main.js
/dist/assets/fd441ca8b6d00.png
/dist/icons/eb4c5fa504857.svg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  asset/source
&lt;/h3&gt;

&lt;p&gt;Just like &lt;a href="https://github.com/webpack-contrib/raw-loader"&gt;raw-loader&lt;/a&gt;. All the modules that match a rule with &lt;code&gt;type: 'asset/source'&lt;/code&gt; will be inlined without any transformations (as is):&lt;br&gt;
&lt;strong&gt;&lt;code&gt;file.txt&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;webpack.config.js&lt;/code&gt;&lt;/strong&gt;&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="p"&gt;module.exports = {
&lt;/span&gt;       // ...
      {
        test: /\.svg$/,
        type: 'asset/resource',
        generator: {
          filename: 'icons/[hash][ext]'
        },
        use: 'svgo-loader'
      },
&lt;span class="gi"&gt;+     {
+       test: /\.txt$/,
+       type: 'asset/source'
+     },
&lt;/span&gt;      // ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;/strong&gt;&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="p"&gt;import './styles.css';
&lt;/span&gt;&lt;span class="gi"&gt;+ import txt from './file.txt';
&lt;/span&gt;
+ console.log(txt); // hello world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/dist/main.js
/dist/icons/eb4c5fa504857.svg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  asset
&lt;/h3&gt;

&lt;p&gt;Combines &lt;code&gt;asset/resource&lt;/code&gt; and &lt;code&gt;asset/inline&lt;/code&gt;. It chooses a strategy by next logic: if a module size is greater than 8kb (by default), then it uses &lt;code&gt;asset/resource&lt;/code&gt; strategy and &lt;code&gt;asset/inline&lt;/code&gt; otherwise.&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="p"&gt;module.exports = {
&lt;/span&gt;       // ...
      {
        test: /\.svg$/,
&lt;span class="gd"&gt;-       type: 'asset/resource',
&lt;/span&gt;&lt;span class="gi"&gt;+       type: 'asset'
&lt;/span&gt;&lt;span class="gd"&gt;-        generator: {
-          filename: 'icons/[hash][ext]'
-        },
&lt;/span&gt;        use: 'svgo-loader'
      },
      {
        test: /\.txt$/,
        type: 'asset/source'
      },
      // ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The limit for using &lt;code&gt;asset/inline&lt;/code&gt; strategy may be overrided:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;      {
        test: /.svg$/,
        type: 'asset',
&lt;span class="gi"&gt;+       parser: {
+         dataUrlCondition: {
+           maxSize: 20 * 1024 // 20kb
+         }
+       },
&lt;/span&gt;        use: 'svgo-loader'
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Totally: webpack 5 Asset Modules makes possible to throw out some habitual loaders because its features works out of the box.&lt;br&gt;
The full example may be found &lt;a href="https://github.com/wdxlab/articles/tree/master/webpack-asset-modules"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When webpack 5 will come out?
&lt;/h2&gt;

&lt;p&gt;We have no known date. At the moment when I writing this guide, webpack 5 has &lt;code&gt;beta.13&lt;/code&gt; version and a user's feedback is collecting. You can help with it by trying to migrate your projects to webpack 5 (for production on your own risk). You can read more &lt;a href="https://github.com/webpack/webpack/issues/9802"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  P.S
&lt;/h2&gt;

&lt;p&gt;I plan to continue talking about the new features of webpack 5 and about the webpack itself. Some of the articles will be larger, some smaller. Very small notes (not only about webpack) can be seen in my &lt;a href="https://twitter.com/wdxlab"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

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

</description>
      <category>webpack</category>
      <category>webpack5</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
