我试图在我的webpack中包含字体文件,但有点困难。
font.css
@font-face {
font-family: 'LatoBlack';
src: url('./font/Lato-Black.woff2') format('woff2'), /* Modern Browsers */
url('./font/Lato-Black.woff') format('woff'), /* Modern Browsers */
url('./font/Lato-Black.ttf') format('truetype');
font-style: normal;
font-weight: normal;
text-rendering: optimizeLegibility;
}
Webpack.base.Config
module.exports = {
module: {
loaders: [
{ test: /\.svg$/, loader: 'url?limit=65000&mimetype=image/svg+xml&name=./font/[name].[ext]' },
{ test: /\.woff$/, loader: 'url?limit=65000&mimetype=application/font-woff&name=./font/[name].[ext]' },
{ test: /\.woff2$/, loader: 'url?limit=65000&mimetype=application/font-woff2&name=./font/[name].[ext]' },
{ test: /\.[ot]tf$/, loader: 'url?limit=65000&mimetype=application/octet-stream&name=./font/[name].[ext]' },
{ test: /\.eot$/, loader: 'url?limit=65000&mimetype=application/vnd.ms-fontobject&name=./font/[name].[ext]' }
]
}
};
结构
webpack.prod.conf
module.exports = new Config().extend('./webpack.base.config.js').merge({
entry: [
'./src/index.js'
],
devtool: 'eval',
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
],
output: {
filename: 'main.js',
path: path.join(__dirname, 'app/www'),
publicPath: '/app/www'
},
});
错误
ERROR in ./~/css-loader?{"modules":true,"sourceMap":true,"importLoaders":2,"localIdentName":"[local]_[hash:base64:5]"}!./~/postcss-loader?sourceMap&parser=postcss-scss!./src/components/shared/main.css
Module not found: Error: Can't resolve './font/Lato-ThinItalic.ttf' in '/var/www/kindred/src/components/shared'
@ ./~/css-loader?{"modules":true,"sourceMap":true,"importLoaders":2,"localIdentName":"[local]_[hash:base64:5]"}!./~/postcss-loader?sourceMap&parser=postcss-scss!./src/components/shared/main.css 6:5744-5781
@ ./src/components/shared/main.css
@ ./src/index.js
@ multi ./src/index.js
webpack.base.config
/* eslint-disable filenames/match-regex, import/no-commonjs, import/unambiguous */
const path = require('path');
const {environment} = require('webpack-config');
module.exports = {
module: {
loaders: [
{
test: /dashjs\/dist\/.*/,
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
},
{
test: /\.js$/,
exclude: /node_modules\/.*/,
use: ['babel-loader']
},
{
test: /\.(jpg|png)$/,
loader: 'url?limit=25000'
},
{
test: /\.svg$/,
use: ['babel-loader', 'svg-react-loader']
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader?sourceMap',
query: {
modules: true,
sourceMap: true,
importLoaders: 2,
localIdentName: environment.get('cssIdent')
}
},
'postcss-loader?sourceMap&parser=postcss-scss'
],
},
// Font Definitions
{ test: /\.svg$/, loader: 'url?limit=65000&mimetype=image/svg+xml&name=./font/[name].[ext]' },
{ test: /\.woff$/, loader: 'url?limit=65000&mimetype=application/font-woff&name=./font/[name].[ext]' },
{ test: /\.woff2$/, loader: 'url?limit=65000&mimetype=application/font-woff2&name=./font/[name].[ext]' },
{ test: /\.[ot]tf$/, loader: 'url?limit=65000&mimetype=application/octet-stream&name=./font/[name].[ext]' },
{ test: /\.eot$/, loader: 'url?limit=65000&mimetype=application/vnd.ms-fontobject&name=./font/[name].[ext]' }
]
}
};
那么如何加载? :o
答案 0 :(得分:1)
由于相对webpack无法根据其当前位置找到。在模块规则中添加this并安装file-loader
npm install --save-dev file-loader
{
test: /\.(svg|eot|ttf|woff|woff2)$/,
use:'file-loader'
options: {
name: ‘images/[name].[hash].[ext]’
}
},
编辑: 通常相对网址可以通过三种方式解决
- 使用绝对路径
- 使用选项并将网址设置为false
use:[
{
loader: ‘css-loader’,
options: {url: false}
}
]
- 使用raw loder,因为它会将url作为字符串
use: ['raw-loader', 'css-loader'],
答案 1 :(得分:0)
Webpack 5或更高版本
不需要安装任何加载程序,只需添加webpack.config.js
。
module: {
rules: [
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},