npm install @pmmmwh/react-refresh-webpack-plugin react-refresh -D
// webpack.config.js
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ['@babel/env', '@babel/preset-react'],
plugins: [require.resolve('react-refresh/babel')], // react-refresh 添加
},
},
],
},
plugins: [
new ReactRefreshPlugin(), // react-refresh 添加
new HtmlWebpackPlugin({
template: './index.html',
}),
],
};
热更新后清除console控制台
// index.js
if (module.hot) {
module.hot.accept()
module.hot.addStatusHandler(status => {
if (status === 'prepare') console.clear()
})
}
网友评论