webpack5配置打包后就无法实现hot功能
参照webpack5官方文档配置的webpack.config.js,如下:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: 'production',
entry: {
index: './src/js/index.js',
home: './src/js/home.js'
},
output: {
filename: '[name].[hash].bundle.js',
path: path.resolve(__dirname,"assets"),
clean: true,
},
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
}
},
'css-loader',
],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
name: '[name].[hash].[ext]'
}
}
],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
]
},
devServer: {
contentBase: path.join(__dirname, '/'),
port: 8888,
hot: true,
open: true,
},
plugins: [
new HtmlWebpackPlugin({
filename: '../index.html',
template: './src/index.html',
inject: 'body',
chunks: ['index'],
showErrors: true
}),
new HtmlWebpackPlugin({
filename: '../home.html',
template: './src/home.html',
inject: 'body',
chunks: ['home'],
showErrors: true
}),
new MiniCssExtractPlugin({
filename: 'css/[name].min.css',
}),
new webpack.HotModuleReplacementPlugin(),
],
};
这么做就将html、css、img、js全部打包了,如果html不打包倒是可以热更,html打包后就无法实现热更功能了,start启动后只会打开打包后的页面,内容变更如果不build自然就无法热更
package.json如下:
{
"name": "webpack-demo",
"version": "1.0.0",
"description": "",
"private": true,
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC",
"scripts": {
"start": "webpack serve --config webpack.config.js",
"build": "webpack"
},
"devDependencies": {
"babel-loader": "^8.2.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^5.2.5",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.1",
"mini-css-extract-plugin": "^1.6.0",
"style-loader": "^2.0.0",
"webpack": "^5.37.1",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.11.2",
"yarn": "^1.22.10"
},
"dependencies": {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据你提供的 demo 发现,
filename
路径有问题