[英]How to make webpack-dev-server pick up latest changes
我正在使用 Webpack 5 並且無法設置 dev-server 以立即獲取最新更改。 這就是為什么當我的項目有任何變化時,我應該每次都先進行yarn build
,然后進行yarn dev
。 我已經在這里看到了同樣的問題Webpack-dev-server does not pick up latest changes ,但writeToDisk: true
在控制台中顯示錯誤。 我附上了我的 webpack.config.js 文件。
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const miniCss = require('mini-css-extract-plugin');
module.exports = {
entry: { main: './src/index.js' },
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js',
publicPath: ''
},
mode: 'development',
devServer: {
static: {
directory: path.join(__dirname, "dist")
},
compress: true,
port: 8080,
devMiddleware: {
publicPath: "//localhost:8080",
},
hot: "only",
open: true
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: '/node_modules/'
},
{
test: /\.pug$/,
loader: 'pug-loader',
options: {
pretty: true
}
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, {
loader: 'css-loader'
}]
},
{
test: /\.(s*)css$/,
use: [
miniCss.loader,
'css-loader',
'sass-loader',
]
},
{
test: /\.(png|svg|jpg|gif|woff(2)?|eot|ttf|otf)$/,
type: 'asset/resource'
},
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/pages/page/page.pug',
filename: 'index.html'
}),
new MiniCssExtractPlugin(),
new miniCss(),
]
}
您的問題很可能是hot: "only"
。 根據https://webpack.js.org/configuration/dev-server/如果它不是hot: true
你將不會看到關於構建失敗的頁面刷新。
除此之外,我沒有看到您的代碼有任何明顯錯誤。 我懷疑如果你把它改成hot: true
它應該刷新頁面並且你有一些其他的構建失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.