目前我有3种字体要添加到我的React项目中:a,灯光,粗体。
我的文件结构:
/src
├── /fonts/
│ ├── /A.ttf
│ ├── /A-light.ttf
│ └── /A-bold.ttf
│
├── /styles/
│ ├── /base/
│ │ └── /__base.scss
│ └── styles.scss
│
├── app.jsx
└── webpack.config.js
_base.scss:
@font-face {
font-family: "A";
font-style: normal;
font-weight: 400;
src: url("../../fonts/A.ttf") format("truetype");
}
@font-face {
font-family: "A";
font-style: normal;
font-weight: 800;
src: url("../../fonts/A-bold.ttf") format("truetype");
}
@font-face {
font-family: "A";
font-style: normal;
font-weight: 300;
src: url("../../fonts/A-light.ttf") format("truetype");
}
body {
font-family: A, Helvetica, Arial, sans-serif;
}
_base.scss由styles.scss导入,styles.scss导入app.jsx。
我的webpack配置如下所示:
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV === 'development') {
require('dotenv').config({path: '.env.development'});
}
module.exports = env => {
const isProduction = env === 'production';
const CSSExtract = new ExtractTextPlugin('styles.css');
return {
entry: ['babel-polyfill', './src/app.jsx'],
output: {
path: path.join(__dirname, 'public', 'dist'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.css', '.scss']
},
module: {
rules: [
{
exclude: /(node_modules|bower_components)/,
test: /\.jsx?$/,
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.s?css$/,
use: CSSExtract.extract({
use: [
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
})
},
{
test: /\.(png|jpg|svg)$/,
use: {
loader: 'url-loader'
}
},
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]'
}
}
]
},
plugins: [
CSSExtract,
new webpack.DefinePlugin({
'process.env.API_AUTH_TOKEN': JSON.stringify(process.env.API_AUTH_TOKEN),
'process.env.API_EMAIL': JSON.stringify(process.env.API_EMAIL),
'process.env.API_PASSWORD': JSON.stringify(process.env.API_PASSWORD)
}),
new StyleLintPlugin({})
],
devtool: isProduction ? 'source-map' : 'inline-source-map',
devServer: {
overlay: {
warnings: true,
errors: true
},
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
publicPath: '/dist/'
}
};
};
然而,Webpack无法编译。
错误:
./src/styles/styles.scss
Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve '../../fonts/A.ttf'
任何帮助将不胜感激!
答案 0 :(得分:3)
从 webpack 5 开始,您可以改用内置的资产模块。从官方 Webpack 指南 here 中,将其添加到 module.rules
以加载 .ttf 文件:
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
答案 1 :(得分:1)
使用npm的'ttf-loader'对我来说非常合适。
https://www.npmjs.com/package/ttf-loader
module: {
rules: [
{
test: /\.ttf$/,
use: [
{
loader: 'ttf-loader',
options: {
name: './font/[hash].[ext]',
},
},
]
}
]
}
答案 2 :(得分:0)
Webpack需要字体加载器来加载项目中存在的字体文件。您正在使用文件加载器来加载字体。 变化
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]'
}
到
{
test: /\.ttf$/,
use: [
{
loader: 'ttf-loader',
options: {
name: './font/[hash].[ext]',
},
},
]
}
在您的项目中安装字体加载器 来自TTF Loader的NPM。
答案 3 :(得分:0)
使用~
前缀字体路径,以告诉Webpack这不是相对导入
https://github.com/webpack-contrib/sass-loader#imports