简体   繁体   English

自定义本地 fonts 不适用于 webpack 5

[英]Custom local fonts not working with webpack 5

I have some local fonts I want to use in my project.我有一些本地 fonts 我想在我的项目中使用。 I've read a few tutorials and questions on this, and I'm following the reccomendations I've seen, but my fonts are not showing up properly in the browser.我已经阅读了一些教程和关于此的问题,并且正在按照我所看到的建议进行操作,但是我的 fonts 没有在浏览器中正确显示。 I am using webpack 5. In my webpack config:我正在使用 webpack 5. 在我的 webpack 配置中:

module.exports = {
  // ...
  module: {
    rules: [
      // ...
      {
        test: /\.(woff|woff2|ttf)$/,
        use: {
          loader: "url-loader",
        },
      },
    ]
  }
}

I have a bunch of.tff font files in my src/assets/fonts/ directory.我的src/assets/fonts/目录中有一堆 .tff 字体文件。 I have a.scss file for global styles. In there, I define the font names and I want to use, and where webpack should find them:我有一个全局 styles 的 .scss 文件。在那里,我定义了字体名称和我想要使用的字体名称,webpack 应该在哪里找到它们:

@font-face {
  font-family: "InterRegular";
  src: url("../assets/fonts/Inter-Regular.ttf") format("truetype");
  font-display: swap;
}
@font-face {
  font-family: "InterMedium";
  src: url("../assets/fonts/Inter-Medium.ttf") format("truetype");
  font-display: swap;
}
@font-face {
  font-family: "InterSemiBold";
  src: url("../assets/fonts/Inter-SemiBold.ttf") format("truetype");
  font-display: swap;
}
// etc

I'm fairly sure webpack is finding these, because if I get the path to the file wrong, webpack errors.我相当确定 webpack 找到了这些,因为如果我得到文件的路径错误,webpack 错误。 I then try to apply the font:然后我尝试应用字体:

html,
body {
  font-family: "InterSemiBold", sans-serif;
}

There are no errors, but the font does not get applied to the page.没有错误,但字体未应用于页面。 When I look in my.network tab, I can see that a font file is indeed being loaded:当我查看 my.network 选项卡时,我可以看到确实正在加载一个字体文件:

在此处输入图像描述

But this is clearly not the InterSemiBold font.但这显然不是InterSemiBold 字体。 Regardless of what font I'm using, this strangely-named.tff file always shows this same, seriffed font.不管我使用什么字体,这个名字奇怪的.tff 文件总是显示相同的衬线字体。

Looking at the computed value of an element, I can see that the browser is reading the "InterSemiBold", sans-serif value of the font family, but still defaulting to Arial :查看元素的计算值,我可以看到浏览器正在读取字体系列的"InterSemiBold", sans-serif ,但仍默认为Arial

在此处输入图像描述

I have also tried loading in fonts using the file-loader with webpack, but that makes no difference, and many recommend using url-loader instead.我也尝试使用 webpack 的file-loader在 fonts 中加载,但这没有区别,许多人建议改用url-loader

What am I doing wrong here?我在这里做错了什么? Why is my font not being loaded in and applied?为什么我的字体没有被加载和应用?

Your dev tools screenshot indicates your actual page/app style sheet expects the font-family name to be 'Inter'.您的开发工具屏幕截图表明您的实际页面/应用程序样式表期望字体系列名称为“Inter”。
So you don't need different family names for each font-weight所以你不需要为每个字体粗细使用不同的姓氏
and change your @font-face rules to something like this:并将您的@font-face规则更改为以下内容:

@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400;
  src: url('../assets/fonts/Inter-Regular.ttf') format('truetype')
}
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 500;
  src: url('../assets/fonts/Inter-Medium.ttf') format('truetype')
}
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 600;
  src: url('../assets/fonts/Inter-SemiBold.ttf') format('truetype')
}
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 700;
  src: url('../assets/fonts/Inter-Bold.ttf') format('truetype')
}

Your @font-face rules should include a font-style value.你的@font-face规则应该包含一个font-style值。
For italic styles you would change it to font-style: normal .对于斜体样式,您可以将其更改为font-style: normal

The font-url must use the exact file name of a font style (just a note, as some automatic font loaders rename the filenames internally or load updated files directly from Google - resulting in filenames like this "inter-v11-latin-800.ttf"). font-url 必须使用字体样式的确切文件名(请注意,因为一些自动字体加载器会在内部重命名文件名或直接从 Google 加载更新的文件 - 导致文件名类似于“inter-v11-latin-800. ttf”)。

Since a browser can't automatically tell which intermediate weight would be eg 'semi-bold' or 'light', you add specific numeric font-weight values which can be used to map all font-weights to different selectors like this:由于浏览器无法自动判断哪个中间粗体是“半粗体”或“轻”,因此您可以添加特定font-weight值,这些值可用于将所有字体粗细映射到不同的选择器,如下所示:

body{
    font-family:Inter;
    font-size:16px;
}

.medium{
    font-weight:500;
}

.semibold{
    font-weight:600;
}


strong, h1, h2,
.button{
    font-weight:700;
}

You might also double check your main css – it might also contain a separate @font-face declaration.你也可以仔细检查你的主 css——它也可能包含一个单独的 @font-face 声明。

If everything is working fine, you should see the .tff files in dev tools just as defined in @font-face urls (eg "Inter-Regular.ttf")如果一切正常,您应该在开发工具中看到 .tff 文件,就像在 @font-face urls 中定义的一样(例如“Inter-Regular.ttf”)

Still not working?还是行不通?

Try to open the font via absolute URL in your browser.尝试通过浏览器中的绝对 URL打开字体。

Font file connection test example字体文件连接测试示例
Provided your compiled folder structure looks something like this:假设您编译的文件夹结构如下所示:

  • the final URL is "myapp.com"最终 URL 是“myapp.com”
  • the main css is located under URL "myapp.com/css/main.css"主 CSS 位于 URL“myapp.com/css/main.css”下
  • font files are located (at least according to your css/compiling code) in directory URL "myapp.com/assets/fonts/"字体文件位于目录 URL“myapp.com/assets/fonts/”中(至少根据您的 css/编译代码)
  • the actual font files should be available (downloadable) under URL "myapp.com/assets/fonts/Inter-Regular.ttf"实际的字体文件应该在 URL“myapp.com/assets/fonts/Inter-Regular.ttf”下可用(可下载)

If this doesn't work – you need to fix the URLs in your @font-face rule.如果这不起作用 - 您需要修复@font-face规则中的 URL。

This especially important, if assets are copied/assembled during a compiling process to a new directory – so previously paths/URLs might not be "automagically" fixed.如果在编译过程中将资产复制/组装到新目录中,这一点尤其重要——因此以前的路径/URL 可能不会“自动”修复。

Another cause might be inlined css – so the css becomes part of the compiled HTML <head> or <body> – relative paths/URLs might not work anymore => absolute paths could fix this (... albeit, any decent auto inlining script should be smart enough to translate relative to absolute URLs).另一个原因可能是内联 css - 因此 css 成为编译的 HTML <head><body>的一部分 - 相对路径/URL 可能不再起作用 => 绝对路径可以解决这个问题(......尽管,任何体面的自动内联脚本应该足够聪明,可以相对于绝对 URL 进行翻译)。

Compiled css编译好的css

The final css might also include some overriding rules.最终的 css 还可能包含一些覆盖规则。
So check the finally compiled css in devtools and search for any @font-face rules – as a last resort: add a !important keyword to a font src to enforce the desired URL.因此,在 devtools 中检查最终编译的 css 并搜索任何@font-face规则 - 作为最后的手段:将!important关键字添加到字体src以强制执行所需的 URL。

Font files might be corrupt?字体文件可能已损坏?

Since the "inter" is available as free google webfont you could get a "fresh" copy via google webfonts helper由于“inter”可作为免费的 google webfont 使用,您可以通过google webfonts helper获得“新鲜”副本

Did you ever solve this issue?你有没有解决过这个问题? I'm having the same problem with Webpack 5 and a Google Font I'm using locally.我在使用 Webpack 5 和我在本地使用的 Google 字体时遇到了同样的问题。 Dev Tools shows that the element is computed with the desired font, but it is clearly not that font, and when I add a back-up option 'monospace' it reverts to the backup font instead. Dev Tools 显示该元素是使用所需字体计算的,但显然不是该字体,当我添加备用选项“monospace”时,它会恢复为备用字体。 None of the above suggestions have worked for me, so curious to know if you got this to work.以上建议对我都没有用,很想知道你是否让这个工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM