我试图用@font-face在CSS文件中加载字体,但字体从未加载.这是我的目录 struct .

enter image description here

然后在webpack.config.js我有加载器得到字体.

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'eval',
  entry: [
    "./index.js"
  ],
  output: {
    path: __dirname+"/build",
    filename: "main.js"
  },
  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
      loaders: [
          { test: /\.js$/, loaders: ['react-hot', 'babel-loader'], exclude: /node_modules/ },
          { test: /\.jsx?$/, loaders: ['react-hot', 'babel-loader'], exclude: /node_modules/ },
          { test: /\.svg$/, loader: "raw" },
          { test: /\.css$/, loader: "style-loader!css-loader" },
          { test: /\.(eot|svg|ttf|woff|woff2)$/, loader: 'file?name=src/css/[name].[ext]'}

      ]
  }
};

在我的CSS文件中,我有以下内容:

@font-face {
  font-family: 'Darkenstone';
  src: url('./Darkenstone.woff') format('woff');
}

body {
  background-color: green;
  font-size: 24px;
  font-family: 'Darkenstone';
}

最后,我在my index.js中调用我的CSS文件:

import './src/css/master.css';

一切都正常,但de font从不加载.

推荐答案

在try 了很多东西后,下一个加载器成功了.我没有使用文件加载器,而是使用url加载器.你需要安装url加载器.

{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }

Node.js相关问答推荐

Spotify Auth访问令牌给出错误代码400

GitLab SAST中的Nodejcan未找到匹配项

findoneandupdate()方法更新数据库,但其响应在请求中返回为null

如何在Mongoose for MongoDB中编写此查询

遇到 - TypeError:try 使用 Express(Node.js) 从 JSON 文件访问数据时无法读取未定义的属性(读取帖子)

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

未授权使用联合身份未授权用户角色从 Amplify graphQL 访问类型 Y 上的 X

nyc 代码覆盖不适用于 NodeJs Express 服务器

Angular Build 生产返回致命的 javascript 无效大小错误

使用正则表达式查找文档,但输入是数组

Mongoose-更新嵌套数组

使用 firebase 函数 api 运行套接字(相同的端口创建问题)

baseurl64 缓冲区解码

错误:大多数中间件(如 bodyParser)不再与 Express Bundle

在 express 中添加故意延迟

如何在 node 中转义 shell 命令的字符串?

为什么 Node 控制台不显示功能代码?

要求('babel/register')不起作用

找不到在 docker compose 环境中运行的 node js 应用程序的模块

使用 Monit 而不是基本的 Upstart 设置有什么好处?