{
test: /\.(jpe?g|png|webp|gif|svg)$/i,
use: [
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false,
},
pngquant: {
quality: [0.65, 0.90],
speed: 4
},
gifsicle: {
interlaced: false,
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
],
type: 'asset/resource',
},
<svg display="none" xmlns="http://www.w3.org/2000/svg">
<symbol id="search" viewBox="0 0 512.005 512.005">
<path d="M505.749 475.587l-145.6-145.6c28.203-34.837 45.184-79.104 45.184-127.317C405.333 90.926 314.41.003 202.666.003S0 90.925 0 202.669s90.923 202.667 202.667 202.667c48.213 0 92.48-16.981 127.317-45.184l145.6 145.6c4.16 4.16 9.621 6.251 15.083 6.251s10.923-2.091 15.083-6.251c8.341-8.341 8.341-21.824-.001-30.165zM202.667 362.669c-88.235 0-160-71.765-160-160s71.765-160 160-160 160 71.765 160 160-71.766 160-160 160z"/>
</symbol>
</svg>
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const mode = process.env.NODE_ENV || 'development';
const devMode = mode === 'development';
const target = devMode ? 'web' : 'browserslist';
const devtool = devMode ? 'source-map' : undefined;
module.exports = {
mode,
target,
devtool,
devServer: {
port: 3000,
open: true,
hot: true,
},
entry: ["@babel/polyfill", path.resolve(__dirname, 'src', 'index.js')],
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
filename: '[name].[contenthash].js',
assetModuleFilename: 'assets/[name][ext]'
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src', 'index.html')
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
})
],
module: {
rules: [
{
test: /\.html$/i,
loader: 'html-loader',
},
{
test: /\.(c|sa|sc)ss$/i,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require('postcss-preset-env')],
}
}
},
'sass-loader',
],
},
{
test: /\.woff2?$/i,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]'
}
},
{
test: /\.(jpe?g|png|webp|gif|svg)$/i,
use: [
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false,
},
pngquant: {
quality: [0.65, 0.90],
speed: 4
},
gifsicle: {
interlaced: false,
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
],
type: 'asset/resource',
},
{
test: /\.m?js$/i,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
]
}
}
а лучше - генери через https://www.npmjs.com/package/webpack5-svg-sprite- установлен:
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
plugins: [
new SpriteLoaderPlugin()
],
.......
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
options: {
extract : true ,
publicPath : '/'
}
}
а лучше - генери через https://www.npmjs.com/package/webpack5-svg-spriteполучается два обработчика svg. Надо бы разделить, например с помощью
- установлен:
resourceQuery: '?sprite'
(при этом у image-loader указать пустую строку) и импортировать как import iconName from 'path/to/file.svg?sprite' Надо бы разделить, например с помощью resourceQuery: '?sprite'- если верно понимаю:
{
test: /\.(jpe?g|png|webp|gif|svg)$/i,
resourceQuery: '',
use: [
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false,
},
pngquant: {
quality: [0.65, 0.90],
speed: 4
},
gifsicle: {
interlaced: false,
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
],
type: 'asset/resource',
},
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
resourceQuery: '?sprite',
options: {
extract : true ,
publicPath : '/assets/'
}
}