javascript Webpack 加载字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33283863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:19:20  来源:igfitidea点击:

Webpack load fonts

javascriptreactjswebpack

提问by Seneca

I have this code in a React element:

我在 React 元素中有此代码:

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

In my Webpack.config:

在我的 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?

Webpack 不是让我自动需要字体吗?我是否必须编写其他程序才能加载字体?

回答by nikita-graf

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

您必须要求在 css 文件中使用字体,而不是在 js/jsx 文件中。例如:

@font-face {
    font-family: 'SomethingStrange';
    src: url('../../Style/fonts/SomethingStrange.ttf');
}