HtmlWebpackPlugin
HtmlWebpackPlugin简化了HTML文件的创建,以便为你的webpack包提供服务。这对于在文件名中包含每次会随着编译而发生变化哈希的 webpack bundle 尤其有用。 你可以让插件为你生成一个HTML文件,使用lodash模板提供你自己的模板,或使用你自己的loader。
场景
通常情况下,我们会在打包之后,在dist 文件夹下新建一个index.html文件,引入我们打包之后的js文件,这就需要我们手动去设置引入js的文件,不是很方便的管理,尤其是在使用 hash 并且输出 多个 bundle 文件的时候。
[HtmlWebpackPlugin](https://github.com/jantimon/html-webpack-plugin)做的事情就是不需要我们去手动写index.html 文件,它会自动帮我们生成一个 index.html并且 将我们打包的bundle js文件引入到这个index.html 中
安装
npm install --save-dev html-webpack-plugin
使用
该插件将为你生成一个 HTML5 文件, 其中包括使用 script 标签的 body 中的所有 webpack 包
var HtmlWebpackPlugin = require('html-webpack-plugin'); //1.导入var path = require('path');var webpackConfig = {entry: 'index.js',output: {path: path.resolve(__dirname, './dist'),filename: 'index_bundle.js'},plugins: [new HtmlWebpackPlugin()] //2.使用};
打包后,这将会产生一个包含以下内容的文件 dist/index.html:(自动生成)
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>webpack App</title></head><body><script src="index_bundle.js"></script></body></html>
配置项
- 指定模板
如果没有显式传入模板文件,插件内部会使用默认的一套模板来注入bundle脚本
plugins: [new HtmlWebpackPlugin({template: './src/index.html' //配置自己的模板})]
- 指定 title
plugins: [new HtmlWebpackPlugin({title: '管理输出',}),],
CleanWebpackPlugin
用于在下一次打包时清除之前打包的文件
场景
dist 文件夹下遗留了我们之前使用的代码,但是是不需要的文件,因为dist目录最终是实际用到的文件。
比较推荐的做法是,在每次构建前清理 /dist文件夹,这样只会生成用到的文件
安装
npm install --save-dev clean-webpack-plugin
使用
webpack.config.js
const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const { CleanWebpackPlugin } = require('clean-webpack-plugin'); //1.引入module.exports = {entry: {index: './src/index.js',print: './src/print.js',},plugins: [new CleanWebpackPlugin(), //2.使用new HtmlWebpackPlugin({title: 'Output Management',}),],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist'),},};
webpack-dev-server
webpack-dev-server 提供了一个 简单的web server ,并且具有live reloading (实时重新加载)功能
注意 : 版本问题 ,下述给出了正确的使用版本
场景
在每次编译代码时,手动运行 npm run build 会很麻烦,webpack提供了几种方式可以帮助我们在代码发生变化后自动编译代码
安装
npm install --save-dev webpack-dev-server
使用
//版本说明"webpack": "^5.10.0","webpack-cli": "^4.2.0","webpack-dev-server": "^3.11.0"
webpack.config.js
const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const { CleanWebpackPlugin } = require('clean-webpack-plugin');module.exports = {mode: 'development',entry: {index: './src/index.js',print: './src/print.js',},devtool: 'inline-source-map',devServer: { //配置dev-servercontentBase: path.join(__dirname, 'dist') //或者 './dist',//告诉服务器内容的来源,仅在需要提供静态文件时才进行配置compress:true, //为每个静态文件开启 gzip compressionport:8080 //指定端口号},plugins: [new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),new HtmlWebpackPlugin({title: 'Development',}),],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist'),},};//注:上述默认配置:将 dist 目录下的文件 serve 到 localhost:8080下
package.json
"scripts": {"start": "webpack serve --open","build": "webpack"},
npm run start 即可看到浏览器自动加载页面,更改源文件,web server 将在编译代码后自动重新加载
注意:按照官方文档配置发现会报错
解决
"start": "webpack serve --open Chrome.exe"
issues https://github.com/webpack/webpack-cli/issues/2001
old version 使用
//版本确认"webpack": "^4.41.5","webpack-cli": "^3.3.10","webpack-dev-server": "^3.11.0"
修改配置文件,告知 dev server 从什么位置查找文件
webpack.config.js
const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const { CleanWebpackPlugin } = require('clean-webpack-plugin');module.exports = {mode: 'development',entry: {app: './src/index.js',print: './src/print.js',},devtool: 'inline-source-map',devServer: {contentBase: './dist', //此项配置},plugins: [new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),new HtmlWebpackPlugin({title: 'Development',}),],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist'),},};
以上配置 告知 webpack-dev-server 将 dist 目录下的文件 serve 到 localhost:8080 下
webpack-dev-server 在编译之后不会写入到任何输出文件,而是将 bundle 文件保留在内存中,然后将它们 serve 到 server 中,就好像它们是挂载在 server 根路径上的真实文件一样。如果你的页面希望在其他不同路径中找到 bundle 文件,则可以通过 dev server 配置中的
publicPath选项进行修改。
https://webpack.docschina.org/guides/development/#using-webpack-dev-server
hot module replacement
模块热替换 HMR ,它允许在运行时更新所有类型的模块,而无需完全刷新。 适用于 开发环境
场景
例如在提交表单的时候,更新某一处代码,这时候如果是重新加载的话,之前填入表单的数据会丢失。
使用
//webpakc.config.jsmodule.exports = {devServer: {contentBase: './dist',hot: true, //开启}};
index.js//是否开启了热更新if (module.hot) {//接受热更新module.hot.accept(// 中间这一段 不用写'./print.js', function() {console.log('Accepting the updated printMe module!');})}
HMR 加载样式
style-loader 自带 module.hot.accept
