【发布时间】:2020-06-05 14:58:59
【问题描述】:
我有一个 Webpack 设置,它分为 2 个配置文件:
开发配置按预期工作,但生产配置有一个问题:一切正常,但图像没有从源文件夹复制和优化:src/images/sm,src/images/bg 到 dist/images/sm,@987654329 @。
webpack.prod.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
module.exports = function(){
return {
mode: 'development',
entry: [
'./src/app.js'
],
watch: true,
watchOptions: {
aggregateTimeout: 300, // Process all changes which happened in this time into one rebuild
poll: 1000, // Check for changes every second,
ignored: /node_modules/,
// ignored: [
// '**/*.scss', '/node_modules/'
// ]
},
devtool: 'source-maps',
devServer: {
contentBase: path.join(__dirname, 'src'),
watchContentBase: true,
host: '0.0.0.0',
hot: true,
open: true,
inline: true,
port: 9000
},
plugins: [
new HtmlWebpackPlugin({
title: 'Webpack starter project',
template: path.resolve('./src/index.pug')
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.HotModuleReplacementPlugin()
],
module: {
rules: [
{
test: /\.pug$/,
use: ['raw-loader', 'pug-html-loader'],
},
{
test: /\.scss$/,
use: [
'style-loader',
"css-loader",
"sass-loader"
]
},
{
test: /\.(jpg|jpeg|gif|png|svg|webp)$/,
use: [
{
loader: "file-loader",
options: {
outputPath: './images',
name: "[name].[ext]",
},
},
]
},
{
test: /\.html$/,
use: {
loader: 'html-loader',
}
},
]
}
};
}
webpack.config.prod.js
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
module.exports = function(){
return {
mode: 'production',
entry: [
'./src/app.js'
],
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin()
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'Webpack starter project',
filename: 'index.html',
template: path.resolve('./src/index.pug')
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.HotModuleReplacementPlugin()
],
module: {
rules: [
{
test: /\.pug$/,
use: ['raw-loader', 'pug-html-loader'],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
},
{
test: /\.(jpg|jpeg|gif|png|svg|webp)$/,
use: [
{
loader: "file-loader",
options: {
outputPath: './images',
name: "[name].[ext]",
},
},
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true,
mozjpeg: {
progressive: false,
quality: 45
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: true,
},
pngquant: {
quality: '65-90',
speed: 4
},
gifsicle: {
interlaced: true,
optimizationLevel: 3
},
// the webp option will enable WEBP
webp: {
quality: 20
}
}
},
],
},
{
test: /\.html$/,
use: {
loader: 'html-loader',
}
},
]
}
};
}
我将我的 webpack 设置上传到了 Google Drive,如果有人想查看它,here 就是。 我用谷歌搜索了一整天,我的代码没有发现任何问题。 我尝试了其他 webpack 配置,它们的代码与我的相同,并且它们的图像被复制到 dist 文件夹中。 谁能帮我解决这个问题?
【问题讨论】:
标签: javascript node.js webpack webpack-4 webpack-file-loader