React/SCSS/GatsbyJS - 使用 @font-face 导入时本地字体无法解析
我目前在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不建议指向
/public
文件夹来加载本地字体。该文件夹是在构建过程 (gatsby build
) 期间生成的,例如在gatsbydevelopment
中请求该文件夹时,并不总是可以访问该文件夹。我建议使用源文件夹(在/src
内)或使用静态文件夹,如 使用本地字体文档。例如,使用
/src/fonts/GangsterGroteskRegular.woff2
文件夹,方法应该是:注意:相应调整
src
规则的本地路径 >然后,您将能够分配
$font
: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 ingatsby 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:Note: tweak the local path of
src
rule accordinglyThen, you will be able to assign
$font
:将您要使用的字体(例如 AssistantRegular.ttf)复制到“fonts”目录中。
在项目的index.js中,导入您要使用的字体。
在项目index.css中添加字体
Copy the fonts you want to use (e.g. AssistantRegular.ttf) into the ‘fonts‘ directory.
In the project’s index.js, import the fonts you want to use.
In the projects index.css add the font-face