gpt4 book ai didi

css - webpack 不加载 @font-face

转载 作者:行者123 更新时间:2023-12-03 17:32:00 27 4
gpt4 key购买 nike

在我的 React 应用程序中,我正在实现服务器端渲染,所以我不想添加 node_modules由于性能问题在我的服务器端。但我想要 css来自 node_modules用作 UI Kit 库。所以我在 中使用了 webpack nodeExternal webpack.server.config 文件如

const path = require('path');
const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.js');
const nodeExternals = require('webpack-node-externals')
let test = {
test: /\.css$/,
loader: 'css/locals?module&localIdentName=[name]__[local]___[hash:base64:5]'
}


const config = {
target:'node',
entry: './src/index.js',

output:{
filename:'bundle.js',
path:path.resolve(__dirname,'build')
},
externals: [nodeExternals({
whitelist: [/\.(?!(?:jsx?|json)$).{1,5}$/i]
})]
}

module.exports = merge(baseConfig,config)

这里 webpack 添加 css 文件,但它是 未加载 @font-face 来自我的 node_modules UI 工具包文件。如果我删除 节点外部 来自 webpack.server.config 文件然后一切正常,但 bundle.js文件大小越来越大。
我已经在 中添加了字体规则webpack.base.config 文件
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CSSExtract = new ExtractTextPlugin('styles.css');


module.exports ={
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
}, {
test: /\.s?css$/,
use: CSSExtract.extract({
use: [
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
})
},{
test: /\.(gif|svg|jpg|png|ttf|eot|woff(2)?)(\?[a-z0-9=&.]+)?$/,
loader: 'url-loader?limit=100000&name=fonts/[name].[ext]'
}
],
},
devtool: 'inline-source-map',
plugins: [
CSSExtract
]
}

我试过 file-loader也是同样的问题
它显示我错误

@font-face {
^
SyntaxError: Invalid or unexpected token



如何使用 @font-face 从 node_modules 添加 css 文件

最佳答案

这是 webpack 设置:

{
test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[contenthash].[ext]",
outputPath: "fonts/",
},
},
],
},
然后你必须将它们导入到 css 或 scss 主文件中。
@font-face {
font-family: "Montserrat";
src: url("../../assets/fonts/Montserrat-Light.ttf");
font-weight: lighter;
font-style: normal;
}

关于css - webpack 不加载 @font-face,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52203067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com