Если поменять порт в файле конфигурации, то после первого запуска сервера страница не уходит в бесконечную перезагрузку, но стоит что-то изменить в коде, и проблема проявляется.
Вот мой
webpack.config.js, что я сделал не так?
const path = require('path')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const ESLintPlugin = require('eslint-webpack-plugin')
const isDev = process.env.NODE_ENV === 'development'
const isProd = !isDev
const filename = (ext) => (isDev ? `[name].${ext}` : `[name].[hash].${ext}`)
const optimization = () => {
const config = {
splitChunks: {
chunks: 'all',
},
}
if (isProd) {
config.minimizer = [
new OptimizeCssAssetsWebpackPlugin(),
new TerserWebpackPlugin(),
]
}
return config
}
const plugins = () => {
const base = [
new HTMLWebpackPlugin({
template: './public/index.html',
minify: {
collapseWhitespace: isProd,
},
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: '*.png',
context: path.resolve(__dirname, 'public'),
to: path.resolve(__dirname, 'dist'),
},
{
from: 'serviceWorker.js',
context: path.resolve(__dirname, 'src'),
to: path.resolve(__dirname, 'dist'),
},
],
}),
new MiniCssExtractPlugin({
filename: filename('css'),
}),
new ESLintPlugin(),
]
if (isProd) {
base.push(new BundleAnalyzerPlugin())
}
return base
}
const cssLoaders = (extra) => {
const loaders = [MiniCssExtractPlugin.loader, 'css-loader']
if (extra) loaders.push(extra)
return loaders
}
const babelOptions = (presets) => {
const opts = {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
}
if (presets) {
presets.map((preset) => {
opts.options.presets.push(preset)
return null
})
}
return opts
}
module.exports = {
entry: {
main: ['@babel/polyfill', './src/index.tsx'],
},
output: {
filename: filename('js'),
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json', '.png'],
alias: {
'@models': path.resolve(__dirname, 'src/models'),
'@': path.resolve(__dirname, 'src'),
},
},
optimization: optimization(),
devServer: {
port: 4033,
hot: isDev,
},
devtool: isDev ? 'source-map' : undefined,
plugins: plugins(),
module: {
rules: [
{
test: /\.css$/,
use: cssLoaders(),
},
{
test: /\.(png|jpeg|svg|gif)$/,
type: 'asset/resource',
},
{
test: /\.html$/,
use: 'html-loader',
},
{
test: /\.m?js$/,
exclude: /node_modules/,
use: babelOptions(),
},
{
test: /\.m?jsx$/,
exclude: /node_modules/,
use: babelOptions(['@babel/preset-react']),
},
{
test: /\.m?ts$/,
exclude: /node_modules/,
use: babelOptions(['@babel/preset-typescript']),
},
{
test: /\.m?tsx$/,
exclude: /node_modules/,
use: babelOptions(['@babel/preset-react', '@babel/preset-typescript']),
},
],
},
}