如何在Parcel中使用@font-face加载字体?

4

我正在使用Parcel进行打包,我想将自定义字体包含到我的项目中

在我的SCSS文件中

@font-face {
    font-family: "Storytella";
    src: url("./fonts/Storytella.otf") format("otf");
}

然后我会在某个地方使用它,就像这样
字体族:'Storytella',衬线字体;

package.json

{
  "name": "pr",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "normalize.css": "^8.0.1",
    "parcel-bundler": "^1.12.3"
  },
  "devDependencies": {
    "node-sass": "^4.12.0",
    "parcel-plugin-static-files-copy": "^2.2.0",
    "pug": "^2.0.4"
  },
  "scripts": {
    "dev": "parcel index.pug",
    "build": "parcel build index.pug --out-dir dist"
  },
  "staticFiles": {
    "staticPath": "dist",
    "watcherGlob": "**"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

在运行 npm run dev 时,字体文件会被放置在 dist 文件夹中,但字体本身不会加载到页面中。在 Chrome 的网络面板中没有错误或其他任何加载字体的信息。
1个回答

0
在 index.html 中,您需要像这样链接 fonts.css。
<link rel="stylesheet" type="text/css" href="/styles/fonts.css" />

在fonts.css文件中

@font-face {
  font-family: "Storytella";
  src: url("./fonts/Storytella.otf") format("otf");
}

body {
  font-family: 'Storytella', serif;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接