Webpack 5-基础配置-处理 Html 资源,开发服务器&自动化

2023年05月06日 10:04 · 阅读(87) ·

依赖环境

名称 版本
操作系统 Windows 10 X64
Node.js Nodejs 16+
Webpack 8.5.0
Webpack 5-基础 -

来源

名称 版本
视频地址 尚硅谷Webpack5入门到原理(面试开发一条龙)
视频+资料下载 百度云资料
课件下载 Webpack5-课件.zip
Webpack 文档 https://webpack.docschina.org/concepts/

处理 Html 资源

1. 下载包

  1. npm i html-webpack-plugin -D

2. 配置

  • webpack.config.js
  1. const path = require("path");
  2. const ESLintWebpackPlugin = require("eslint-webpack-plugin");
  3. const HtmlWebpackPlugin = require("html-webpack-plugin");
  4. module.exports = {
  5. entry: "./src/main.js",
  6. output: {
  7. path: path.resolve(__dirname, "dist"),
  8. filename: "static/js/main.js", // 将 js 文件输出到 static/js 目录中
  9. clean: true, // 自动将上次打包目录资源清空
  10. },
  11. module: {
  12. rules: [
  13. {
  14. // 用来匹配 .css 结尾的文件
  15. test: /\.css$/,
  16. // use 数组里面 Loader 执行顺序是从右到左
  17. use: ["style-loader", "css-loader"],
  18. },
  19. {
  20. test: /\.less$/,
  21. use: ["style-loader", "css-loader", "less-loader"],
  22. },
  23. {
  24. test: /\.s[ac]ss$/,
  25. use: ["style-loader", "css-loader", "sass-loader"],
  26. },
  27. {
  28. test: /\.styl$/,
  29. use: ["style-loader", "css-loader", "stylus-loader"],
  30. },
  31. {
  32. test: /\.(png|jpe?g|gif|webp)$/,
  33. type: "asset",
  34. parser: {
  35. dataUrlCondition: {
  36. maxSize: 10 * 1024, // 小于10kb的图片会被base64处理
  37. },
  38. },
  39. generator: {
  40. // 将图片文件输出到 static/imgs 目录中
  41. // 将图片文件命名 [hash:8][ext][query]
  42. // [hash:8]: hash值取8位
  43. // [ext]: 使用之前的文件扩展名
  44. // [query]: 添加之前的query参数
  45. filename: "static/imgs/[hash:8][ext][query]",
  46. },
  47. },
  48. {
  49. test: /\.(ttf|woff2?)$/,
  50. type: "asset/resource",
  51. generator: {
  52. filename: "static/media/[hash:8][ext][query]",
  53. },
  54. },
  55. {
  56. test: /\.js$/,
  57. exclude: /node_modules/, // 排除node_modules代码不编译
  58. loader: "babel-loader",
  59. },
  60. ],
  61. },
  62. plugins: [
  63. new ESLintWebpackPlugin({
  64. // 指定检查文件的根目录
  65. context: path.resolve(__dirname, "src"),
  66. }),
  67. new HtmlWebpackPlugin({
  68. // 以 public/index.html 为模板创建文件
  69. // 新的html文件有两个特点:1. 内容和源文件一致 2. 自动引入打包生成的js等资源
  70. template: path.resolve(__dirname, "public/index.html"),
  71. }),
  72. ],
  73. mode: "development",
  74. };

3. 修改 index.html

去掉引入的 js 文件,因为 HtmlWebpackPlugin 会自动引入

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>webpack5</title>
  8. </head>
  9. <body>
  10. <h1>Hello Webpack5</h1>
  11. <div class="box1"></div>
  12. <div class="box2"></div>
  13. <div class="box3"></div>
  14. <div class="box4"></div>
  15. <div class="box5"></div>
  16. <i class="iconfont icon-arrow-down"></i>
  17. <i class="iconfont icon-ashbin"></i>
  18. <i class="iconfont icon-browse"></i>
  19. </body>
  20. </html>

4. 运行指令

  1. npx webpack

此时 dist 目录就会输出一个 index.html 文件

开发服务器&自动化

每次写完代码都需要手动输入指令才能编译代码,太麻烦了,我们希望一切自动化

1. 下载包

  1. npm i webpack-dev-server -D

2. 配置

  • webpack.config.js
  1. const path = require("path");
  2. const ESLintWebpackPlugin = require("eslint-webpack-plugin");
  3. const HtmlWebpackPlugin = require("html-webpack-plugin");
  4. module.exports = {
  5. entry: "./src/main.js",
  6. output: {
  7. path: path.resolve(__dirname, "dist"),
  8. filename: "static/js/main.js", // 将 js 文件输出到 static/js 目录中
  9. clean: true, // 自动将上次打包目录资源清空
  10. },
  11. module: {
  12. rules: [
  13. {
  14. // 用来匹配 .css 结尾的文件
  15. test: /\.css$/,
  16. // use 数组里面 Loader 执行顺序是从右到左
  17. use: ["style-loader", "css-loader"],
  18. },
  19. {
  20. test: /\.less$/,
  21. use: ["style-loader", "css-loader", "less-loader"],
  22. },
  23. {
  24. test: /\.s[ac]ss$/,
  25. use: ["style-loader", "css-loader", "sass-loader"],
  26. },
  27. {
  28. test: /\.styl$/,
  29. use: ["style-loader", "css-loader", "stylus-loader"],
  30. },
  31. {
  32. test: /\.(png|jpe?g|gif|webp)$/,
  33. type: "asset",
  34. parser: {
  35. dataUrlCondition: {
  36. maxSize: 10 * 1024, // 小于10kb的图片会被base64处理
  37. },
  38. },
  39. generator: {
  40. // 将图片文件输出到 static/imgs 目录中
  41. // 将图片文件命名 [hash:8][ext][query]
  42. // [hash:8]: hash值取8位
  43. // [ext]: 使用之前的文件扩展名
  44. // [query]: 添加之前的query参数
  45. filename: "static/imgs/[hash:8][ext][query]",
  46. },
  47. },
  48. {
  49. test: /\.(ttf|woff2?)$/,
  50. type: "asset/resource",
  51. generator: {
  52. filename: "static/media/[hash:8][ext][query]",
  53. },
  54. },
  55. {
  56. test: /\.js$/,
  57. exclude: /node_modules/, // 排除node_modules代码不编译
  58. loader: "babel-loader",
  59. },
  60. ],
  61. },
  62. plugins: [
  63. new ESLintWebpackPlugin({
  64. // 指定检查文件的根目录
  65. context: path.resolve(__dirname, "src"),
  66. }),
  67. new HtmlWebpackPlugin({
  68. // 以 public/index.html 为模板创建文件
  69. // 新的html文件有两个特点:1. 内容和源文件一致 2. 自动引入打包生成的js等资源
  70. template: path.resolve(__dirname, "public/index.html"),
  71. }),
  72. ],
  73. // 开发服务器
  74. devServer: {
  75. host: "localhost", // 启动服务器域名
  76. port: "3000", // 启动服务器端口号
  77. open: true, // 是否自动打开浏览器
  78. },
  79. mode: "development",
  80. };

3. 运行指令

  1. npx webpack serve

注意运行指令发生了变化

并且当你使用开发服务器时,所有代码都会在内存中编译打包,并不会输出到 dist 目录下。

开发时我们只关心代码能运行,有效果即可,至于代码被编译成什么样子,我们并不需要知道。