gpt4 book ai didi

javascript - scss 中的 webpack 5 图像/字体无法正确编译

转载 作者:行者123 更新时间:2023-12-05 01:57:47 24 4
gpt4 key购买 nike

我的问题是;我的 .scss 文件中的图像和字体正在重命名为哈希,并且 scss 文件中的 url 路径正在更改。我的 .scss 文件被导入到我的 app.js 文件的最顶部,如:

import '../scss/app.scss';

我的 .scss 文件中的示例:

@font-face {
font-family: 'FTBase';
src: url("../fonts/FTBase-Book.woff2") format("woff2"),
url("../fonts/FTBase-Book.woff") format("woff");
font-weight: 350;
font-style: normal;
font-display: swap
}
.event--1 {
background: var(--salmon) url("../images/leading-women.jpg") no-repeat center;
background-size: contain;
}

运行 dev/build/production 后的输出是:

@font-face {
font-family: "FTBase";
src: url(../51ebe4030489fa0868f9.woff2) format("woff2"), url(../be034b081210fbdf38a2.woff) format("woff");
font-weight: 350;
font-style: normal;
font-display: swap;
}
.event--2 {
background: var(--salmon) url(../485639d38ea610d7bba2.jpg) no-repeat center;
background-size: cover;
}

并将图像/字体输出到/dist。

这是我的 webpack 文件:webpack.config.js:

const templateFiles = fs.readdirSync(environment.paths.source)
.filter((file) => path.extname(file).toLowerCase() === '.html');

const htmlPluginEntries = templateFiles.map((template) => new HTMLWebpackPlugin({
inject: 'body',
hash: false,
minify: false,
filename: template,
template: path.resolve(environment.paths.source, template),
favicon: path.resolve(environment.paths.source, 'images', 'favicon.ico'),
}));

module.exports = {
entry: {
app: path.resolve(environment.paths.source, 'js', 'app.js'),
},
output: {
filename: 'js/[name].js',
path: environment.paths.output,
},
module: {
rules: [{
test: /\.((c|sa|sc)ss)$/i,
use: [{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
}
}, {
loader: 'css-loader',
}, {
loader: 'sass-loader',
}, {
loader: 'postcss-loader',
}],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.(png|gif|jpe?g|svg|jpg)$/i,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images/',
},
}, ],
},
{
test: /\.(eot|ttf|woff|woff2)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
},
}, ],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/[name].min.css',
}),
new ImageminWebpWebpackPlugin({
config: [{
test: /\.(jpe?g|png)/,
options: {
quality: 76,
}
}],
overrideExtension: true,
detailedLogs: false,
silent: false,
strict: true,
}),
new CleanWebpackPlugin({
verbose: true,
cleanOnceBeforeBuildPatterns: ['**/*', '!stats.json'],
}),
new CopyWebpackPlugin({
patterns: [{
from: path.resolve(environment.paths.source, 'images'),
to: path.resolve(environment.paths.output, 'images'),
toType: 'dir',
globOptions: {
ignore: ['*.DS_Store', 'Thumbs.db'],
},
}, ],
}),
].concat(htmlPluginEntries),
target: 'web',
};

webpack.dev.config.js:

const webpackConfiguration = require('../webpack.config');
const environment = require('./environment');

module.exports = merge(webpackConfiguration, {
mode: 'development',

/* Manage source maps generation process */
devtool: 'eval-source-map',

/* Development Server Configuration */
devServer: {
contentBase: environment.paths.output,
watchContentBase: true,
publicPath: '/',
open: true,
historyApiFallback: true,
compress: true,
overlay: true,
hot: false,
watchOptions: {
poll: 300,
},
...environment.server,
},

/* File watcher options */
watchOptions: {
aggregateTimeout: 300,
poll: 300,
ignored: /node_modules/,
},

/* Additional plugins configuration */
plugins: [],
});

webpack.prod.config.js:

const webpackConfiguration = require('../webpack.config');

module.exports = merge(webpackConfiguration, {
mode: 'production',

/* Manage source maps generation process. Refer to https://webpack.js.org/configuration/devtool/#production */
devtool: false,

/* Optimization configuration */
optimization: {
splitChunks: {
chunks: 'all',
name: 'vendor',
},
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
}),
new CssMinimizerPlugin(),
],
},

/* Performance treshold configuration values */
performance: {
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},

/* Additional plugins configuration */
plugins: [],
});

在我的 webpack.config.js 文件中,我尝试在 css-loader 和 sass-loader 上将 sourceMaps 设置为 true,更改 publicPath 以及我在其他论坛上尝试过的一些其他操作,但没有成功。

最佳答案

所以我想通了,我必须从图像和字体中删除 file-loader。我将其替换为:

{
test: /\.(png|jpg|gif|svg)$/,
type: 'asset/resource',
}, {
test: /.(ttf|otf|eot|woff(2)?)(\?[a-z0-9]+)?$/,
type: 'asset/resource',
},

然后我不得不将输出编辑为:

  output: {
filename: 'js/[name].js',
path: environment.paths.output,
assetModuleFilename: 'assets/[name][ext]',
},

现在,当我运行 dev/build/production 时,它会抓取 scss 文件中的所有 Assets ,让它们保持相同的名称,并创建一个名为“assets”的文件夹,该文件夹位于“/dist/assets”,它会自动将 scss 文件中的 url 更改为 '../assets/image.jpg'

关于javascript - scss 中的 webpack 5 图像/字体无法正确编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68883063/

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