React/SCSS/GatsbyJS - 使用 @font-face 导入时本地字体无法解析

发布于 2025-01-10 02:42:57 字数 2457 浏览 1 评论 0原文

我目前在 Gatsbyjs 项目的两个子组件中收到错误,这些组件导入了我的 global.scss 文件,我在其中尝试加载本地字体,出现此错误:

ERROR in ./src /components/elements/Nav/nav.scss 模块构建失败(来自 ./node_modules/mini-css-ext ract-plugin/dist/loader.js):ModuleBuildError:模块构建失败(来自./node_modules/css-loader/dist/cjs.js):错误:无法解析“/Projects/src/components/elements/Nav”中的“../../public/static/fonts/GangsterGroteskRegular.woff2” ' 在 finishWithoutResolve

我尝试使用 gatsby-browser.js 文件更全局地导入 SCSS 文件,但这似乎并没有过滤到子组件。

我已经尝试使用 Webpack 解决此问题并安装 gatsby-plugin-web-font-loader ,因为问题似乎源于此,但到目前为止没有任何效果。


我的 global.scss 文件:

@font-face {
    font-family: 'GangsterGrotesk';
    src: local('GangsterGrotesk'),
        url('../../public/static/fonts/GangsterGroteskRegular.woff2')
            format('woff2');
}


$font: 'GangsterGrotesk' sans-serif;
$font-stack: 'GangsterGrotesk' sans-serif, 'Arial' sans-serif,
    'Helvetica' sans-serif;

body {
    margin: 0;
    padding: 0;
    font-family:$font-stack:;
}

nav.tsx

import React from 'react';
import { Link, withPrefix } from 'gatsby';
import { siteData } from '../../../config/index';
import './nav.scss';

const Nav = () => {
    const { menu } = siteData.navLinks;

    return (
        <nav>
            <Link className="logo" to="/">
                <h4>{siteData.siteTitle}</h4>
            </Link>
            <ul className="nav__list">
                {menu.map(({ name, url }, key) => {
                    return (
                        <li className="nav__item">
                            <Link className="nav-link" key={key} to={url}>
                                {name}
                            </Link>
                        </li>
                    );
                })}
            </ul>
        </nav>
    );
};

export default Nav;


nav.scss

@import '../../../styles/global.scss';

nav {
    padding: 0.25rem 0;
    width: 100vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-direction: row;
    background: $col-bg;
    position: fixed;
    overflow: hidden;
    border-bottom: 1px $col-border solid;
    & > * {
        overflow-x: none;
    }
}

.nav__list {
    align-self: center;
}

.nav__item {
    display: inline-block;
    padding: 0 2rem 0 2rem;
}


I am currently receiving errors in two child components of my Gatsbyjs project that import my global.scss file where I am attempting to load a local font giving me this error :

ERROR in ./src/components/elements/Nav/nav.scss Module build failed (from ./node_modules/mini-css-ext ract-plugin/dist/loader.js): ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js): Error: Can't resolve '../../public/static/fonts/GangsterGroteskRegular.woff2' in '/Projects/src/components/elements/Nav' at finishWithoutResolve

I have attempted to import the SCSS files more globally using the gatsby-browser.js file but that doesn't seem to filter down to child components.

I have already tried addressing this with Webpack and installing gatsby-plugin-web-font-loader as it seems the issue stems from there but nothing has worked thus far.


My global.scss file:

@font-face {
    font-family: 'GangsterGrotesk';
    src: local('GangsterGrotesk'),
        url('../../public/static/fonts/GangsterGroteskRegular.woff2')
            format('woff2');
}


$font: 'GangsterGrotesk' sans-serif;
$font-stack: 'GangsterGrotesk' sans-serif, 'Arial' sans-serif,
    'Helvetica' sans-serif;

body {
    margin: 0;
    padding: 0;
    font-family:$font-stack:;
}

nav.tsx

import React from 'react';
import { Link, withPrefix } from 'gatsby';
import { siteData } from '../../../config/index';
import './nav.scss';

const Nav = () => {
    const { menu } = siteData.navLinks;

    return (
        <nav>
            <Link className="logo" to="/">
                <h4>{siteData.siteTitle}</h4>
            </Link>
            <ul className="nav__list">
                {menu.map(({ name, url }, key) => {
                    return (
                        <li className="nav__item">
                            <Link className="nav-link" key={key} to={url}>
                                {name}
                            </Link>
                        </li>
                    );
                })}
            </ul>
        </nav>
    );
};

export default Nav;


nav.scss

@import '../../../styles/global.scss';

nav {
    padding: 0.25rem 0;
    width: 100vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-direction: row;
    background: $col-bg;
    position: fixed;
    overflow: hidden;
    border-bottom: 1px $col-border solid;
    & > * {
        overflow-x: none;
    }
}

.nav__list {
    align-self: center;
}

.nav__item {
    display: inline-block;
    padding: 0 2rem 0 2rem;
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2025-01-17 02:42:58

我不建议指向 /public 文件夹来加载本地字体。该文件夹是在构建过程 (gatsby build) 期间生成的,例如在 gatsbydevelopment 中请求该文件夹时,并不总是可以访问该文件夹。我建议使用源文件夹(在 /src 内)或使用静态文件夹,如 使用本地字体文档

例如,使用 /src/fonts/GangsterGroteskRegular.woff2 文件夹,方法应该是:

@font-face {
  font-family: "GangsterGrotesk";
  src: url("../fonts/GangsterGroteskRegular.woff2");
}

注意:相应调整 src 规则的本地路径 >

然后,您将能够分配 $font

$font: 'GangsterGrotesk' sans-serif;

I wouldn't recommend pointing the/public folder to load local fonts. That folder is generated during the build process (gatsby build) and won't be always accessible when requesting it in gatsby develop for example. I'd recommend using a sourced folder (inside /src) or using the static folder, as described in the using local fonts docs.

For example, using a /src/fonts/GangsterGroteskRegular.woff2 folder, the approach should be:

@font-face {
  font-family: "GangsterGrotesk";
  src: url("../fonts/GangsterGroteskRegular.woff2");
}

Note: tweak the local path of src rule accordingly

Then, you will be able to assign $font:

$font: 'GangsterGrotesk' sans-serif;
呆头 2025-01-17 02:42:58
  1. 下载字体
  2. 在源目录下创建Fonts文件夹

<前><代码>.src/字体

  1. 将您要使用的字体(例如 AssistantRegular.ttf)复制到“fonts”目录中。

  2. 在项目的index.js中,导入您要使用的字体。

  3. 在项目index.css中添加字体

@font-face { font-family: "AssistantRegular";源代码:
本地(“助理常规”),
url("./fonts/assistant.regular.ttf") 格式("truetype");字体粗细:正常; }

.woff -> .woff ->格式(“woff”),
.ttf->格式(“真实类型”)
.eot ->格式('嵌入式开放类型')
.svg#vtks_sonhoregular _>格式('svg')

  1. 现在字体可供项目使用,并且可以在常规 CSS 等中使用

.body{
字体系列:AssistantRegular;
...
}

  1. Download the font
  2. Create a Fonts folder in the source directory
.src/fonts
  1. Copy the fonts you want to use (e.g. AssistantRegular.ttf) into the ‘fonts‘ directory.

  2. In the project’s index.js, import the fonts you want to use.

  3. In the projects index.css add the font-face

@font-face { font-family: "AssistantRegular"; src:
local("AssistantRegular"),
url("./fonts/assistant.regular.ttf") format("truetype"); font-weight: normal; }

.woff -> format("woff"),
.ttf -> format("truetype")
.eot -> format('embedded-opentype')
.svg#vtks_sonhoregular _> format('svg')

  1. Now the font/s are available to the project and can be used in regular CSS etc

.body {
font-family: AssistantRegular;
...
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文