Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack load fonts

I have this code in a React element:

require('../../Style/fonts/SomethingStrange.ttf')

In my Webpack.config:

{ test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, 
      loader: 'file-loader?name=fonts/[name].[ext]' }

Problem: no font on my webpages...

Doesn't Webpack let me automatically require fonts? Do I have to program anything else to load fonts?

like image 904
Seneca Avatar asked Oct 22 '15 14:10

Seneca


People also ask

How do I import a font into Webpack?

If you have some local font files of your own, place them in a font directory within src and reference them within . style. scss using @font-face as you normally would—webpack will see that you're referencing a font file and run it through the file-loader like it did with Font Awesome and Google Fonts.

What does file-loader do in Webpack?

They allow you to pre-process files as you import or “load” them. Thus, loaders are kind of like “tasks” in other build tools and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs.

What is raw loader?

The RAW Loader is the easiest yet most effective way to scoop up loose material from your tray and load it into your cone or paper. The simplicity of the Raw Loader is what makes it such a Rawesome tool that is a must for all RYO enthusiasts. Included with the RAW Loader is a non-stick scraper and a long bamboo poker.

Where do you put images in Webpack?

First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/. }; It's quite similar to setting up fonts with Webpack.


1 Answers

You have to require your fonts inside css files, not inside js/jsx files. For example:

@font-face {
    font-family: 'SomethingStrange';
    src: url('../../Style/fonts/SomethingStrange.ttf');
}
like image 71
nikita-graf Avatar answered Oct 04 '22 11:10

nikita-graf