Categories
JavaScript Answers

How to load fonts with Webpack and font-face with JavaScript?

Spread the love

To load fonts with Webpack and font-face with JavaScript, we set the modules property to load the fonts.

For instance, we write

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpackConfig = require("./webpack.config");

module.exports = {
  //...
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        use: ["url-loader?limit=100000"],
      },
    ],
  },
};

to set the module.rules property to load woff, woff2, eot, and ttf fonts.

And we set it to use url-loader to load the font files.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *