Webpack 5 - Asset Modules

Good day. With this post I want to start a series of articles about new features of the upcoming webpack 5. Why do I want to talk about webpack? At least because I take an active part in its development and constantly delve into its insides. In this post I want to talk about Asset Modules - an experimental feature of webpack 5, which allows you to get rid of several familiar loaders, while retaining their benefit.


Imagine that we need to assemble a page with pictures and styles.


Webpack 4 solution


The webpack 4 configuration for this task may look like this:
webpack.config.js


module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        use: [
          'file-loader',
          'svgo-loader'
        ]
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      }
    ]
  }
};

src/index.js


import './styles.css';

// ...

src/styles.css


.logo {
  background: url("/images/logo.svg") no-repeat;
  background-size: cover;
  width: 75px;
  height: 65px;
}

Conclusion:


/dist/main.js
/dist/eb4c5fa504857.svg

With this approach, all svg files will be processed using svgo and using file-loader placed in the directory with the bundle assembled, and css, using css-loader , will turn into something like this:


.logo {
  background: url("eb4c5fa504857.svg") no-repeat;
  background-size: cover;
  width: 75px;
  height: 65px;
}

At some point, we may need to optimize our page and we may want to inline images directly in css. To do this, replace file-loaderwith url-loader :


      {
        test: /\.svg$/,
        use: [
-         'file-loader',
+         'url-loader',
          'svgo-loader'
        ]
      },

Conclusion:


/dist/main.js

The compiled css will change as follows:


-   background: url("eb4c5fa504857.svg") no-repeat;
+   background: url("data:image/svg+xml;base64,....") no-repeat;

Further, we may want to inline only small svg-sized ones (for example, up to 8kb), and leave the rest as separate files. For this, url-loaderthere is a special limit setting:


      {
        test: /\.svg$/,
        use: [
-         'url-loader',
+         'url-loader?limit=8192',
          'svgo-loader'
        ]
      },

, svg- 8, svg , url-loader file-loader.


, webpack 5 Asset Modules, , url-loader file-loader ( url-loader , , limit).


webpack 5


, , Asset Modules. :


module.exports = {
  // ...
+ experiments: {
+   asset: true
+ }
};

.


, svg- asset , file-loader url-loader โ€” , , - :


      {
        test: /\.svg$/,
-       use: [
-         'url-loader?limit=8000',
-         'svgo-loader'
-       ]
+       type: 'asset',
+       use: 'svgo-loader'
      },

, , type: 'asset' : 8 ( ), , .
use .


asset .


asset/inline


url-loader. , type: 'asset/inline' data-url:


      {
        test: /\.svg$/,
-       type: 'asset',
+       type: 'asset/inline',
        use: 'svgo-loader'
      },

, type: 'asset/inline' data-url.
, svg- mini-svg-data-uri, svg data-url, base64, :


+ const miniSVGDataURI = require('mini-svg-data-uri');
// ...
      {
        test: /\.svg$/,
        type: 'asset/inline',
+       generator: {
+         dataUrl(content) {
+           content = content.toString();
+           return miniSVGDataURI(content);
+         }
+       },
        use: 'svgo-loader'
      },

css:


-   background: url("data:image/svg+xml;base64,....") no-repeat;
+   background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg'....") no-repeat;

data-url.


asset/resource


file-loader. , type: 'asset/resource' :


      {
        test: /\.svg$/,
-       type: 'asset/inline',
+       type: 'asset/resource',
-       generator: {
-         dataUrl(content) {
-           content = content.toString();
-           return miniSVGDataURI(content);
-         }
-       },
        use: 'svgo-loader'
      },


, asset/resource , output.path ( dist), output.assetModuleFilename :


module.exports = {
+ output: {
+   assetModuleFilename: 'assets/[name][ext]'
+ },
  // ...
};

:


/dist/main.js
/dist/assets/logo.svg

[name] [hash] long term caching:


module.exports = {
  output: {
-    assetModuleFilename: 'assets/[name][ext]'
+    assetModuleFilename: 'assets/[hash][ext]'
  },
  // ...
};

:


/dist/main.js
/dist/assets/eb4c5fa504857.svg

, asset-. , svg- dist/icons, asset- dist/assets:


      {
        test: /\.svg$/,
        type: 'asset/resource',
+       generator: {
+         filename: 'icons/[hash][ext]'
+       },
        use: 'svgo-loader'

:


/dist/main.js
/dist/assets/fd441ca8b6d00.png
/dist/icons/eb4c5fa504857.svg

asset/source


raw-loader. , type: 'asset/source' :
file.txt


hello world

webpack.config.js


module.exports = {
       // ...
      {
        test: /\.svg$/,
        type: 'asset/resource',
        generator: {
          filename: 'icons/[hash][ext]'
        },
        use: 'svgo-loader'
      },
+     {
+       test: /\.txt$/,
+       type: 'asset/source'
+     },
      // ...

index.js


import './styles.css';
+ import txt from './file.txt';

+ console.log(txt); // hello world

:


/dist/main.js
/dist/icons/eb4c5fa504857.svg

asset


asset/resource asset/inline, - , . , 8, asset/resource, โ€” asset/inline.


module.exports = {
       // ...
      {
        test: /\.svg$/,
-       type: 'asset/resource',
+       type: 'asset'
-        generator: {
-          filename: 'icons/[hash][ext]'
-        },
        use: 'svgo-loader'
      },
      {
        test: /\.txt$/,
        type: 'asset/source'
      },
      // ...

asset/inline :


      {
        test: /.svg$/,
        type: 'asset',
+       parser: {
+         dataUrlCondition: {
+           maxSize: 20 * 1024 // 20kb
+         }
+       },
        use: 'svgo-loader'
      },

: Asset Modules webpack 5 " ".
.


webpack 5?


. , webpack 5 beta.13, . , webpack 5 ( , production).


P.S


webpack 5 webpack. , , ( webpack) -.


Thank you for the attention.


All Articles