我具有以下Webpack配置。
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
compress: true,
publicPath: 'dist',
writeToDisk: true,
open: true,
liveReload: true,
hotOnly: true,
},
entry: './src/js/app.js',
output: {
filename: 'app.js',
path: path.resolve(__dirname, 'dist/js'),
publicPath: 'dist',
},
module: {
rules: [
{
test: /\.(scss)$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [require('autoprefixer')];
},
},
},
{
loader: 'sass-loader',
},
],
},
{
test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: '../fonts/',
publicPath: '../fonts/',
},
},
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '../css/app.css',
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
],
};
当我编辑HTML文件时,它不会像nodemon
那样重新加载浏览器
这些是我的脚本
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"start:dev": "webpack-dev-server --hot --mode development"
},
答案 0 :(得分:1)
将devServer.watchContentBase
设置为true
,如下所示:
module.exports = {
//...
devServer: {
watchContentBase: true
}
};
devServer.watchContentBase
布尔值
告诉dev-server观看devServer.contentBase选项提供的文件。默认情况下是禁用的。启用后,文件更改将触发整个页面重新加载。
(来自https://webpack.js.org/configuration/dev-server/#devserverwatchcontentbase)