css - 在 webpack 中使用本地网络字体

标签 css fonts sass webpack

我正在尝试在我的 React 元素中使用一些本地网络字体。我将它们包含在我的 main.scss 文件中,并通过 webpack 加载它们。包构建正确,包括 main.scss 样式。我看到 webpack 加载字体文件,并将它们复制到 public/fonts/,但我的包文件找不到字体。

据我了解,您的@font-face src 应该与 bundle 所在的位置有关。我将其设置为与我在 webpack 中加载字体相同的路径,'./fonts/'。我看到的确切错误是:

file:///Users/myusername/Documents/folder1/folder2/folder3/APP/fonts/FoundersGroteskWeb-Regular.woff net::ERR_FILE_NOT_FOUND

我一直在尝试很多不同的路径配置,并在 webpack 中使用 publicPath 选项,但此时我正在兜圈子,因为这似乎是一个非常简单的引用错误。

文件结构:

APP
├──webpack.config.js
├──src
     ├──app
        ├──App.jsx
        ├──styles
           ├──main.scss
           ├──fonts
              ├──allthefonts.woff
     ├──public
        ├──bundle.js
        ├──fonts
           ├──allthefonts.woff

App.jsx:

require('./styles/main.scss');

ma​​in.scss:

 @font-face {
    font-family: FoundersGrotesk;
    src: url('./fonts/FoundersGroteskWeb-Bold.eot') format('eot'),
         url('./fonts/FoundersGroteskWeb-Bold.woff') format('woff'),
         url('./fonts/FoundersGroteskWeb-Bold.woff2') format('woff2');
    font-weight: bold;
}

@font-face {
    font-family: FoundersGrotesk_Cnd;
    src: url('./fonts/FoundersGrotXCondWeb-Bold.eot') format('eot'),
         url('./fonts/FoundersGrotXCondWeb-Bold.woff') format('woff'),
         url('./fonts/FoundersGrotXCondWeb-Bold.woff2') format('woff2');
    font-weight: bold;
}

@font-face {
    font-family: FoundersGrotesk;
    src: url('./fonts/FoundersGroteskWeb-Regular.eot') format('eot'),
         url('./fonts/FoundersGroteskWeb-Regular.woff') format('woff'),
         url('./fonts/FoundersGroteskWeb-Regular.woff2') format('woff2');
    font-weight: normal;
}

webpack.config.js:

 'use strict';

const webpack = require('webpack');
const PROD = JSON.parse(process.env.PROD_ENV || '0');

module.exports = {

  entry: './src/app/App.jsx',

  output: {
    path: './src/public/',
    filename: PROD ? 'bundle.min.js' : 'bundle.js'
  },

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: '/node_modules/',
        query: {
          presets: ['es2015', 'react', 'stage-1']
        }
      },
      {
        test: /\.s?css$/,
        loaders: ['style', 'css', 'sass']
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader?name=./fonts/[name].[ext]'
      }
    ]
  }
};

最佳答案

this thread 中感谢@omerts,得到了一个可行的解决方案.涉及使用 publicPath 的解决方案。我一直试图将它用作 module.exports 中的一个选项,带有字体文件加载器,而不是输出。

更新webpack.config.js:

const webpack = require('webpack');
const PROD = JSON.parse(process.env.PROD_ENV || '0');
const path = require('path');

const PATHS = {
  build: path.join(__dirname, './src/public')
};

module.exports = {

  entry: './src/app/App.jsx',

  output: {
    path: PATHS.build,
    filename: PROD ? 'bundle.min.js' : 'bundle.js',
    publicPath: PATHS.build
  },

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: '/node_modules/',
        query: {
          presets: ['es2015', 'react', 'stage-1']
        }
      },
      {
        test: /\.s?css$/,
        loaders: ['style', 'css', 'sass']
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader?name=/fonts/[name].[ext]'
      },
      {
        test: /\.(jpg|png)$/,
        loader: 'file-loader?name=/fonts/[name].[ext]'
      }
    ]
  },

  plugins: PROD ? [
    new webpack.optimize.UglifyJsPlugin({
      beautify: false,
      comments: false,
      compress: { 
        warnings: false,
        drop_console: true
      },
      mangle: {
        except: ['$'],
        screw_ie8: true,
        keep_fnames: false
      }
    })
  ] : []
};

关于css - 在 webpack 中使用本地网络字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41335456/

相关文章:

html - 包含 jQuery 链接后,Bootstrap 切换按钮不起作用

javascript - setTimeinterval 内的 Jquery setTimeout 不起作用

javascript - 在javascript中计算字母大小

css - SCSS Mixin 不工作

css - 如何优化 Bootstrap 以避免渲染未使用的 css 代码

python - 无法使用 Bootstrap 设置 Django 默认登录表单的样式

css - 如何使用 CSS3 转换字体样式?

ios - 如何检查 UIFont 是否为系统字体?

css - Grunt watch 抛出 NoMethodError

javascript - 打开新的基础元素时出现 "Uncaught TypeError: a.indexOf is not a function"错误