question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't load font files using custom webpack

See original GitHub issue

Cant load font files using custom webpack.

Font files are stored at: src/assets/fonts/[.eot, .ttf, .woff, .woff2]

custom webpack (even using the comment out webpack config below doesnt help):

  config.module.rules.push({
    test: /\.(eot|ttf|woff|otf|woff2)$/,
    include: [path.resolve(__dirname, '../src')],
    use: [
      'file-loader',
    ],
    // use: {
    //   loader: 'file-loader?name=/fonts/[name].[ext]',
    //   options: {
    //     name: 'assets/fonts/[name].[ext]',
    //   },
    // },
  });

Storybook version:

 "@storybook/addon-actions": "^5.0.11",
    "@storybook/addon-info": "^5.0.11",
    "@storybook/addon-links": "^5.0.11",
    "@storybook/addon-storysource": "^5.0.11",
    "@storybook/addons": "^5.0.11",
    "@storybook/react": "^5.0.11",

Please advise.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
iampavacommented, May 21, 2020

Just got it to work by using CopyWebpackPlugin instead of the file loader.

Our usecase is this: multiple apps in the same repo (monorepo). Among them, one is a library of common UI components, and it also has storybook. The fonts are located in a different app client/. I wanted to import the fonts from client app inside common-ui to be used by storybook.

Inside storybook’s webpack.config.js I added:

config.plugins.push(
    new CopyWebpackPlugin({
      patterns: [{
        from: resolve(__dirname, '../../client/src/assets/fonts'),
        to: 'static/fonts'
      }]
    }),
  );

And then, inside preview-head.html I added:

<style>
@font-face {
    font-family: "OurFontName";
    src: url("static/fonts/OurFontName.eot");
    src: url("static/fonts/OurFontName.eot?#iefix") format("embedded-opentype"),
      url("static/fonts/OurFontName.woff2") format("woff2"),
      url("static/fonts/OurFontName.woff") format("woff"),
      url("static/fonts/OurFontName.woff") format("truetype");
    font-weight: bold;
    font-style: normal;
  }

  @font-face {
    font-family: "OurFontName";
    src: url("static/fonts/OurFontName.eot");
    src: url("static/fonts/OurFontName.eot?#iefix") format("embedded-opentype"),
      url("static/fonts/OurFontName.woff2") format("woff2"),
      url("static/fonts/OurFontName.woff") format("woff"),
      url("static/fonts/OurFontName.woff") format("truetype");
    font-weight: 400;
    font-style: normal;
  }
</style>

And it woooorks 🎉

7reactions
goranefblcommented, Sep 19, 2019

I am using sass files and also have @font-face imports in them.

Here is the webpack.config.js that worked for me:

const path = require('path');

module.exports = async ({ config, mode }) => {
    config.module.rules.push(
        {
            test: /\.scss$/,
            use: ['style-loader', 'css-loader', 'sass-loader'],
            include: path.resolve(__dirname, '../')
        },
        {
            test: /\.(png|woff|woff2|eot|ttf|svg)$/,
            loaders: ['file-loader'],
            include: path.resolve(__dirname, '../')
        }
    );
    return config;
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack can't load fonts (ttf)
Webpack requires a font loader to load font files present in your project. you are using a file loader to load fonts. Change...
Read more >
Loading Fonts with webpack
The first thing we have to do is install an external webpack loader called file-loader. File-loader will allow us to import file-based assets ......
Read more >
How to use Fonts with Webpack 5 - Setup Tutorial
In this tutorial, you will learn how to set up a local font with Webpack. We will use Open Sans, but you can...
Read more >
file-loader - webpack - JS.ORG
The file-loader resolves import / require() on a file into a url and emits the file into the output directory. Getting Started. To...
Read more >
Load images and fonts with Webpack file loader like a pro
Webpack file -loader is a loader used mainly for supporting images such as SVG and PNG, and fonts in your webpack project. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found