Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack font can't resolve

I am trying to include font files in my webpack with a bit of difficulty.

font.css

@font-face {
    font-family: 'LatoBlack';
    src: url('./font/Lato-Black.woff2') format('woff2'), /* Modern Browsers */
         url('./font/Lato-Black.woff') format('woff'), /* Modern Browsers */
         url('./font/Lato-Black.ttf') format('truetype');
    font-style: normal;
    font-weight: normal;
    text-rendering: optimizeLegibility;
}

Webpack.base.Config

module.exports = {
  module: {
    loaders: [
      { test: /\.svg$/, loader: 'url?limit=65000&mimetype=image/svg+xml&name=./font/[name].[ext]' },
      { test: /\.woff$/, loader: 'url?limit=65000&mimetype=application/font-woff&name=./font/[name].[ext]' },
      { test: /\.woff2$/, loader: 'url?limit=65000&mimetype=application/font-woff2&name=./font/[name].[ext]' },
      { test: /\.[ot]tf$/, loader: 'url?limit=65000&mimetype=application/octet-stream&name=./font/[name].[ext]' },
      { test: /\.eot$/, loader: 'url?limit=65000&mimetype=application/vnd.ms-fontobject&name=./font/[name].[ext]' }
    ]
  }
};

Structure

enter image description here

webpack.prod.conf

module.exports = new Config().extend('./webpack.base.config.js').merge({
  entry: [
    './src/index.js'
  ],
  devtool: 'eval',
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    })
  ],
  output: {
    filename: 'main.js',
    path: path.join(__dirname, 'app/www'),
    publicPath: '/app/www'
  },
});

Error

ERROR in ./~/css-loader?{"modules":true,"sourceMap":true,"importLoaders":2,"localIdentName":"[local]_[hash:base64:5]"}!./~/postcss-loader?sourceMap&parser=postcss-scss!./src/components/shared/main.css
Module not found: Error: Can't resolve './font/Lato-ThinItalic.ttf' in '/var/www/kindred/src/components/shared'
 @ ./~/css-loader?{"modules":true,"sourceMap":true,"importLoaders":2,"localIdentName":"[local]_[hash:base64:5]"}!./~/postcss-loader?sourceMap&parser=postcss-scss!./src/components/shared/main.css 6:5744-5781
 @ ./src/components/shared/main.css
 @ ./src/index.js
 @ multi ./src/index.js

webpack.base.config

/* eslint-disable filenames/match-regex, import/no-commonjs, import/unambiguous */

const path = require('path');
const {environment} = require('webpack-config');

module.exports = {
  module: {
    loaders: [
      {
        test: /dashjs\/dist\/.*/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]'
        }
      },
      {
        test: /\.js$/,
        exclude: /node_modules\/.*/,
        use: ['babel-loader']
      },
      {
        test: /\.(jpg|png)$/,
        loader: 'url?limit=25000'
      },
      {
        test: /\.svg$/,
        use: ['babel-loader', 'svg-react-loader']
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader?sourceMap',
            query: {
              modules: true,
              sourceMap: true,
              importLoaders: 2,
              localIdentName: environment.get('cssIdent')
            }
          },
          'postcss-loader?sourceMap&parser=postcss-scss'
        ],
      },
      // Font Definitions
      { test: /\.svg$/, loader: 'url?limit=65000&mimetype=image/svg+xml&name=./font/[name].[ext]' },
      { test: /\.woff$/, loader: 'url?limit=65000&mimetype=application/font-woff&name=./font/[name].[ext]' },
      { test: /\.woff2$/, loader: 'url?limit=65000&mimetype=application/font-woff2&name=./font/[name].[ext]' },
      { test: /\.[ot]tf$/, loader: 'url?limit=65000&mimetype=application/octet-stream&name=./font/[name].[ext]' },
      { test: /\.eot$/, loader: 'url?limit=65000&mimetype=application/vnd.ms-fontobject&name=./font/[name].[ext]' }
    ]
  }
};

So how can I make it load? :O

like image 682
Jamie Hutber Avatar asked Jun 07 '17 20:06

Jamie Hutber


People also ask

How to load fonts with 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.

How do you use Fontface?

With the @font-face rule, web designers do not have to use one of the "web-safe" fonts anymore. In the @font-face rule you must first define a name for the font (e.g. myFirstFont), and then point to the font file.

How do I use font faces in sass?

Create a font face rule. Embedded OpenType, WOFF2, WOFF, TrueType, and SVG files are automatically sourced. Create a font face rule that applies to bold and italic text. Create a font face rule that only sources a WOFF.


2 Answers

Due to having relative webpack is not able to find according to its current location. In the module rules add this and install file-loader

npm install --save-dev file-loader

{
        test: /\.(svg|eot|ttf|woff|woff2)$/,
        use:'file-loader',
        options: {
            name: 'images/[name].[hash].[ext]'
       }

},

Edit: Usually relative url can be solved in three ways

-use absolute path

-use options and set url to false

use:[
        {
            loader: 'css-loader',
            options: {url: false}
        }
    ]

-use raw loder as it will loder url as string

use: ['raw-loader', 'css-loader'],
like image 178
bawa g Avatar answered Oct 13 '22 06:10

bawa g


Webpack 5 or later versions

Don't need any loader installation, just add in webpack.config.js.

   module: {
    rules: [
      {
       test: /\.(woff|woff2|eot|ttf|otf)$/i,
       type: 'asset/resource',
      },
    ],
  },
like image 24
Predrag Davidovic Avatar answered Oct 13 '22 08:10

Predrag Davidovic