最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - What's the correct way to load .otf font files in webpack? - Stack Overflow

matteradmin3PV0评论

What is the appropriate way to load .otf font files when using webpack? I have made several attempts to include a rule in my webpack.config.js, without any success, based on many examples I've seen along the lines of the following:

{ test: /\.(eot|svg|ttf|otf|woff)$/, use: 'file-loader' }
//or
{ test: /\.(eot|svg|ttf|otf|woff)$/, use: 'url-loader' }
//or
{ test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/, use: 'file-loader' }
//...etc

I've set up in my webpack.config.js the following for other file types, which are working successfully:

module.exports = {
    //...
      module: {
        rules: [
          { test: /\.(js)$/, use: 'babel-loader' },
          { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
          { test: /\.(jpe?g|png|gif|svg|xml)$/i, use: 'file-loader'}
        ]
      },
    //...
}

Despite my various attempts to add another rule/case for .otf files, I always get the following error:

Module parse failed: .../fonts/[name-of-my-font].otf Unexpected character ' ' (1:4) You may need an appropriate loader to handle this file type.

I've added the .otf file in a fonts folder in my root directory, and in my index.css I have:

@font-face {
  font-family: 'name-of-my-font';
  src: url('fonts/name-of-my-font.otf'),
  format('opentype');
}

Has anyone experienced a similar problem and found a solution for this?

Per ment request for more of my webpack.config.js file, here is my entire webpack.config.js:

var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
  },
  module: {
    rules: [
      { test: /\.(js)$/, use: 'babel-loader' },
      { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
      { test: /\.(jpe?g|png|gif|svg|xml)$/i, use: 'file-loader' }
    ]
  },
  devServer: {
    historyApiFallback: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'app/index.html'
    })
  ]
};

What is the appropriate way to load .otf font files when using webpack? I have made several attempts to include a rule in my webpack.config.js, without any success, based on many examples I've seen along the lines of the following:

{ test: /\.(eot|svg|ttf|otf|woff)$/, use: 'file-loader' }
//or
{ test: /\.(eot|svg|ttf|otf|woff)$/, use: 'url-loader' }
//or
{ test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/, use: 'file-loader' }
//...etc

I've set up in my webpack.config.js the following for other file types, which are working successfully:

module.exports = {
    //...
      module: {
        rules: [
          { test: /\.(js)$/, use: 'babel-loader' },
          { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
          { test: /\.(jpe?g|png|gif|svg|xml)$/i, use: 'file-loader'}
        ]
      },
    //...
}

Despite my various attempts to add another rule/case for .otf files, I always get the following error:

Module parse failed: .../fonts/[name-of-my-font].otf Unexpected character ' ' (1:4) You may need an appropriate loader to handle this file type.

I've added the .otf file in a fonts folder in my root directory, and in my index.css I have:

@font-face {
  font-family: 'name-of-my-font';
  src: url('fonts/name-of-my-font.otf'),
  format('opentype');
}

Has anyone experienced a similar problem and found a solution for this?

Per ment request for more of my webpack.config.js file, here is my entire webpack.config.js:

var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
  },
  module: {
    rules: [
      { test: /\.(js)$/, use: 'babel-loader' },
      { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]},
      { test: /\.(jpe?g|png|gif|svg|xml)$/i, use: 'file-loader' }
    ]
  },
  devServer: {
    historyApiFallback: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'app/index.html'
    })
  ]
};
Share Improve this question edited Mar 9, 2022 at 16:40 abgregs asked Jul 11, 2017 at 21:14 abgregsabgregs 1,3651 gold badge10 silver badges20 bronze badges 4
  • 1 Can you share your full module.loaders from your webpack config ? – Bulkan Commented Jul 12, 2017 at 2:14
  • @Bulkan I've amended my question to include my full webpack.config.js at the end. Does this help? – abgregs Commented Jul 12, 2017 at 5:32
  • are you sure you have the url-loader and file-loader installed? – Mudiaga Ejenavi Commented Jul 14, 2017 at 18:40
  • @MudiagaEjenavi Thanks for your question. I've checked and both file-loader and url-loader are installed. Do you have any suggestion for if (and how) these both should be specified in my webpack.config.js? Or might something else be causing the error I'm receiving? – abgregs Commented Jul 17, 2017 at 15:42
Add a ment  | 

2 Answers 2

Reset to default 12

I added

  {    
    test: /\.(woff|woff2|eot|ttf|otf)$/,
    loader: "file-loader"
  }

to my webpack.config.js. I think I needed to explicitly link the types to the file-loader. I was using the default VS2017 Angular project.

In my webpack.config.js I have:

{
  test: /\.(woff|woff2|eot|ttf|otf)$/,
  use: "file-loader"
}

Then in my index.js file I have:

import React from 'react';
import ReactDOM from 'react-dom';

import { injectGlobal } from 'styled-ponents';
import avenir from '../font/AvenirLTStd-Light.otf';
injectGlobal`
    @font-face {
        font-family: 'Avenir';
        src: url(${avenir}) format('opentype');
        font-weight: normal;
        font-style: normal;
    }

    * {
        font-family: 'Avenir', sans-serif;
    }
`;

import App from './ponents/App';

ReactDOM.render(
  <App />
  , document.querySelector('#root')
);

Using the injectGlobal helper from the styled-ponents library will automatically inject the styles into your css file. You can probably move the styled-ponents stuff to a separate .js file then import it.

Post a comment

comment list (0)

  1. No comments so far