How to Use Webpack's url-loader

Apr 5, 2021

Webpack's url-loader lets you import arbitrary files, like images. If you import a .png file, url-loader will ensure that import resolves to a Base64 string representing the contents of the file.

Example

Here is an example to display a .png file using the url-loader option. The project directory looks like the following:

root
|__deploy
|  |
|  |__index.html
|  |__main.bundle.js
|
|__node_modules
|
|__src
|  |
|  |__app.js
|  |__component.js
|  |__image.js
|
|__package-lock.json
|__package.json
|__webpack.config.js
|__yinyang.png

The dependencies needed are:

This is the webpack configuration file:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      title: "Webpack Output",
    }),
  ],
  entry: {
    main: path.resolve(__dirname, "./src/app.js"),
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "deploy"),
  },
  optimization: {
    minimize: false,
  },
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 8192,
              mimetype: "image/png",
              encoding: true,
            },
          },
        ],
      },
    ],
  },
};

Below are the javascript file contents:

component.js

export default (text = "Hello, Webpack!") => {
  const element = document.createElement("h1");

  element.innerHTML = text;

  return element;
};

image.js

import image from "../image/yinyang.png";

export default () => {
  const element = document.createElement("img");
  element.src = image;

  return element;
};

app.js

import component from "./component";
import image from "./image";

document.body.appendChild(component());
document.body.appendChild(image());

Once you have everything installed and setup properly, run: webpack --mode development to compile and bundle. This will create the deploy folder with the index.html and main.bundle.js file.

This is what the app.js will look like after Webpack bundles it. Notice the const yinyang = "..." code that contains the base64 representation of yinyang.png.

main.bundle.js

/******/ (() => {
  // webpackBootstrap
  /******/ "use strict";
  var __webpack_exports__ = {}; // CONCATENATED MODULE: ./src/component.js

  /* harmony default export */ const component = (text = "Hello, Webpack!") => {
    const element = document.createElement("h1");

    element.innerHTML = text;

    return element;
  }; // CONCATENATED MODULE: ./yinyang.png
  /* harmony default export */ const yinyang =
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA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8+HEBNHv2bEkT3UGjEQaTcqZhMrc8JE/gemyg35O40ELdR0r9n5QfONSf1Y0YcRhO0S5JEboT1wlZXYv1EpXVtglp1h840I+Qd+Dx8p0+MakSuIIX0DoLiDaBImgWKJQhYLetLljK1PJE4mLic5fkzkTSRz7E945169bJwFRCYzx6dcFkmtdNSs85dB/1ebpMZPCqXafPrBs2bKjjTuxTHF9VYviQIYuMmxM/0vETEY4LmQJXP5c+fOUZKkt8APT5rMH7fZjQD90a67mwDx48SMtx6rnouLl0hMwIrcwy1mFdtbYqyx22CqzEaNPlgtXKmaRJfzXYuXNnPa9IGFrv3btncUVpFy5cSEBqWYIkE7RrZQXNtB68FsydlUTQuieJIN1PNhpyIAJSKsNRuXz37t2L8O47A3/kKH758qWBQgYf+HgmkXpwO63/ARm+jFwFXwB7p0pkQIl9e9I0v9kowO7S/t2mvLy8GJ+XhuJqpcBX4H/7u81/1jKjxQy/8v0AAAAASUVORK5CYII="; // CONCATENATED MODULE: ./src/image.js
  /* harmony default export */ const src_image = () => {
    const element = document.createElement("img");
    element.src = yinyang;

    return element;
  }; // CONCATENATED MODULE: ./src/app.js
  document.body.appendChild(component());
  document.body.appendChild(src_image());
  /******/
})();

And this is what it should look like on your file browser:

Output


Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Webpack Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!