公共路径

image.png

  1. output:{
  2. // 可以指定为前端域名,也可以指定为cdn域名
  3. publickPath:"http://localhost:8080/"
  4. }

编译代码后:(所有的资源前面都加上了基础路径)
image.png
编译之前:
image.png

环境变量

image.png

  1. const path = require("path")
  2. const HtmlWebpackPlugin = require("html-webpack-plugin")
  3. // 将导出的对象换成一个函数
  4. // env 为编译时输入的信息
  5. module.export = (env) => {
  6. return {
  7. // 配置多入口
  8. entry:{
  9. index:"./src/index.js",
  10. another:"./src/another-module.js"
  11. },
  12. output:{
  13. filename:"[name].[contenthash].js",
  14. path:path.resolve(__dirname,"./dist"),
  15. clean:true,
  16. assetModuleFilename:"images/[contenthash][ext]"
  17. },
  18. // 因为现在是一个函数了,并且有一个env对象传入
  19. // 通过env对象动态调整mode(环境变量)的值
  20. mode:env.production ? "production" : "development",
  21. // devtool:"inline-source-map",
  22. // plugins: [
  23. // new HtmlWebpackPlugin({
  24. // template:"./index.html",
  25. // filename:"app.html",
  26. // inject:"body"
  27. // })
  28. // ],
  29. // 定义哪些资源文件需要打包
  30. module:{
  31. rules:[
  32. // ...
  33. ]
  34. },
  35. // 优化插件
  36. optimization:{
  37. // 压缩代码
  38. minimizer:{
  39. new CssMinimizerPlugin(),
  40. new TerserPlugin()
  41. },
  42. // 分离代码
  43. // 配置后,将会自动把公共的代码抽离到单独的文件中
  44. // splitChunks:{
  45. // chunks:"all"
  46. // }
  47. }
  48. }
  49. }

打包:npx webpack —env production —env goal=local
编译的结果中有编译是输入的信息,通过这些信息确定使用那个环境
image.png
这时的文件并没有压缩(但我们可以使用terser进行压缩),这里没有起作用是因为配置了css代码压缩,如下:
image.png
如果这时还想要别的代码页压缩,就需要配置terser了,如下:
npm i terser-webpack-plugin -D
安装好后引入对应的插件
image.png
然后配置裁:
image.png
这时通过命令,在生产环境下打包:npx webpack —env production
打包后的文件将都会被压缩,
但如果通过开发模式打包:npx webpack —env development
打包后的文件是不会被压缩的
这就是配置环境变量的作用

拆分配置文件

image.png
项目根目录下创建config文件夹,用于存放配置文件
image.png

开发环境配置文件

  1. const path = require("path")
  2. const HtmlWebpackPlugin = require("html-webpack-plugin")
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin")
  4. const toml = require("toml")
  5. const yoml = require("yoml")
  6. const json5 = require("json5")
  7. // 将导出的对象换成一个函数
  8. // env 为编译时输入的信息
  9. module.export =
  10. {
  11. // 配置多入口
  12. entry:{
  13. index:"./src/index.js",
  14. another:"./src/another-module.js"
  15. },
  16. output:{
  17. filename:"[name].js",
  18. path:path.resolve(__dirname,"../dist"),
  19. clean:true,
  20. assetModuleFilename:"images/[contenthash][ext]"
  21. },
  22. mode:"development",
  23. // 调试代码
  24. devtool:"inline-source-map",
  25. plugins: [
  26. new HtmlWebpackPlugin({
  27. template:"./index.html",
  28. filename:"app.html",
  29. inject:"body"
  30. }),
  31. new MiniCssExtractPlugin({
  32. filename:"styles/[contenthash].css"
  33. })
  34. ],
  35. // 用于启动服务
  36. devServer:{
  37. static:"./dist"
  38. },
  39. // 定义哪些资源文件需要打包
  40. module:{
  41. rules:[
  42. {
  43. test:/\.png$/,
  44. type:"asset/resource",
  45. generator:{
  46. filename:"images/[contenthash][ext]"
  47. }
  48. },
  49. {
  50. test:/\.svg$/,
  51. type:"asset/inline",
  52. },
  53. {
  54. test:/\.txt$/,
  55. type:"asset/source",
  56. },
  57. {
  58. test:/\.jpg$/,
  59. type:"asset",
  60. // 解析器
  61. parser:{
  62. // 定义是否生成URL的临界值,当文件大于时创建,反之生成URL
  63. dataUrlCondition:{
  64. maxSize:4*1024*1024
  65. }
  66. }
  67. },
  68. {
  69. test:/\.(css|less)$/,
  70. // 这时需要将css文件抽离,所以将不需要style-loader,替换为MiniCssExtractPlugin-loader
  71. // 这个loader是由MiniCssExtractPlugin插件自带的
  72. //use:[MiniCssExtractPlugin-loader,"css-loader","less-loader"]
  73. use:[MiniCssExtractPlugin.loader,"css-loader","less-loader"]
  74. },
  75. {
  76. test:/\.(woff|woff2|eot|ttf|otf)$/,
  77. type:"asset/resource"
  78. },
  79. {
  80. test:/\.(csv|tsv)$/,
  81. type:"csv-loader"
  82. },
  83. {
  84. test:/\.xml$/,
  85. type:"xml-loader"
  86. },
  87. {
  88. // 需要转换的文件
  89. test:/\.toml$/,
  90. // 转换后的类型
  91. type:"json",
  92. // 使用什么转换
  93. parser:{
  94. parse:toml.parse
  95. }
  96. },
  97. {
  98. // 需要转换的文件
  99. test:/\.yoml$/,
  100. // 转换后的类型
  101. type:"json",
  102. // 使用什么转换
  103. parser:{
  104. parse:yoml.parse
  105. }
  106. },
  107. {
  108. // 需要转换的文件
  109. test:/\.json5$/,
  110. // 转换后的类型
  111. type:"json",
  112. // 使用什么转换
  113. parser:{
  114. parse:json5.parse
  115. }
  116. },
  117. {
  118. test:/\.js$/,
  119. exclude:/node_modules/,
  120. use: {
  121. loader:"babel-loader",
  122. options:{
  123. presets:["@abel/preset-env"],
  124. // 用于定义插件的插件,及@babel/plugin-transform-runtime是属于"babel-loader"的插件
  125. plugins:[
  126. [
  127. "@babel/plugin-transform-runtime"
  128. ]
  129. ]
  130. }
  131. }
  132. }
  133. ]
  134. },
  135. // 优化插件
  136. optimization:{
  137. // 分离代码
  138. // 配置后,将会自动把公共的代码抽离到单独的文件中
  139. splitChunks:{
  140. // 缓存组
  141. // 一般用于定义需要缓存的第三方库
  142. cacheFGroups:{
  143. // 第三方库一般都是在node_modules中的
  144. vendor:{
  145. // 这样配置,是为了确保能获取到node_modules中的文件
  146. test:/[\\/]node_modules[\\/]/,
  147. // 起个名字
  148. name:"vendors",
  149. // all 对所有chunks(模块)做处理
  150. chunks:"all"
  151. }
  152. }
  153. }
  154. }
  155. }

打包,并指定运行那个配置文件,当前是开发环境:
npx webpack -c ./com\nfig/webpack.config.dev.js 或
npx webpack —config ./config/webpack.config.dev.js
打包后会生成一个dist文件夹(在项目根目录下)

生产环境配置文件

在config文件夹下创建webpack.config.prod.js开发环境配置文件

  1. const path = require("path")
  2. const HtmlWebpackPlugin = require("html-webpack-plugin")
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin")
  4. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
  5. const TerserPlugin = require("terser-webpack-plugin")
  6. const toml = require("toml")
  7. const yoml = require("yoml")
  8. const json5 = require("json5")
  9. // 将导出的对象换成一个函数
  10. // env 为编译时输入的信息
  11. module.export =
  12. {
  13. // 配置多入口
  14. entry:{
  15. index:"./src/index.js",
  16. another:"./src/another-module.js"
  17. },
  18. output:{
  19. // 用于缓存
  20. filename:"[name].[contenthash].js",
  21. path:path.resolve(__dirname,"../dist"),
  22. clean:true,
  23. assetModuleFilename:"images/[contenthash][ext]",
  24. pulicPath:"http://localhost:8080"
  25. },
  26. mode:"production",
  27. // 调试代码
  28. // devtool:"inline-source-map",
  29. plugins: [
  30. new HtmlWebpackPlugin({
  31. template:"./index.html",
  32. filename:"app.html",
  33. inject:"body"
  34. }),
  35. new MiniCssExtractPlugin({
  36. filename:"styles/[contenthash].css"
  37. })
  38. ],
  39. // 用于启动服务
  40. // devServer:{
  41. // static:"./dist"
  42. // },
  43. // 定义哪些资源文件需要打包
  44. module:{
  45. rules:[
  46. {
  47. test:/\.png$/,
  48. type:"asset/resource",
  49. generator:{
  50. filename:"images/[contenthash][ext]"
  51. }
  52. },
  53. {
  54. test:/\.svg$/,
  55. type:"asset/inline",
  56. },
  57. {
  58. test:/\.txt$/,
  59. type:"asset/source",
  60. },
  61. {
  62. test:/\.jpg$/,
  63. type:"asset",
  64. // 解析器
  65. parser:{
  66. // 定义是否生成URL的临界值,当文件大于时创建,反之生成URL
  67. dataUrlCondition:{
  68. maxSize:4*1024*1024
  69. }
  70. }
  71. },
  72. {
  73. test:/\.(css|less)$/,
  74. // 这时需要将css文件抽离,所以将不需要style-loader,替换为MiniCssExtractPlugin-loader
  75. // 这个loader是由MiniCssExtractPlugin插件自带的
  76. //use:[MiniCssExtractPlugin-loader,"css-loader","less-loader"]
  77. use:[MiniCssExtractPlugin.loader,"css-loader","less-loader"]
  78. },
  79. {
  80. test:/\.(woff|woff2|eot|ttf|otf)$/,
  81. type:"asset/resource"
  82. },
  83. {
  84. test:/\.(csv|tsv)$/,
  85. type:"csv-loader"
  86. },
  87. {
  88. test:/\.xml$/,
  89. type:"xml-loader"
  90. },
  91. {
  92. // 需要转换的文件
  93. test:/\.toml$/,
  94. // 转换后的类型
  95. type:"json",
  96. // 使用什么转换
  97. parser:{
  98. parse:toml.parse
  99. }
  100. },
  101. {
  102. // 需要转换的文件
  103. test:/\.yoml$/,
  104. // 转换后的类型
  105. type:"json",
  106. // 使用什么转换
  107. parser:{
  108. parse:yoml.parse
  109. }
  110. },
  111. {
  112. // 需要转换的文件
  113. test:/\.json5$/,
  114. // 转换后的类型
  115. type:"json",
  116. // 使用什么转换
  117. parser:{
  118. parse:json5.parse
  119. }
  120. },
  121. {
  122. test:/\.js$/,
  123. exclude:/node_modules/,
  124. use: {
  125. loader:"babel-loader",
  126. options:{
  127. presets:["@abel/preset-env"],
  128. // 用于定义插件的插件,及@babel/plugin-transform-runtime是属于"babel-loader"的插件
  129. plugins:[
  130. [
  131. "@babel/plugin-transform-runtime"
  132. ]
  133. ]
  134. }
  135. }
  136. }
  137. ]
  138. },
  139. // 优化插件
  140. optimization:{
  141. minimizer:[
  142. new CssMinimizerPlugin(),
  143. new TerserPlugin()
  144. ]
  145. // 分离代码
  146. // 配置后,将会自动把公共的代码抽离到单独的文件中
  147. splitChunks:{
  148. // 缓存组
  149. // 一般用于定义需要缓存的第三方库
  150. cacheFGroups:{
  151. // 第三方库一般都是在node_modules中的
  152. vendor:{
  153. // 这样配置,是为了确保能获取到node_modules中的文件
  154. test:/[\\/]node_modules[\\/]/,
  155. // 起个名字
  156. name:"vendors",
  157. // all 对所有chunks(模块)做处理
  158. chunks:"all"
  159. }
  160. }
  161. }
  162. },
  163. // 与性能有关的配置
  164. performance: {
  165. hints:false
  166. }
  167. }

打包,并指定运行那个配置文件,当前是开发环境:
npx webpack -c ./comfig/webpack.config.prod.js 或
npx webpack —config ./config/webpack.config.prod.js
打包后会生成一个dist文件夹(在项目根目录下)

通过npx 启动一个服务:npx webpack serve -c ./comfig/webpack.config.dev.js
情动服务后可在浏览器查看效果

npm脚本

image.png
在package.json文件中新建脚本
image.png
通过 npm run xxx 例如: npm run start ,或 npm run build 运行上面的两个脚本
在脚本中使用webpack可以补添加npx 前缀,如下:
image.png

提取公共配置

image.png
在config项目的文件夹下,创建一个公共配置文件(用于配置生产环境和开发环境中相同的配置)webpack.config.common.js
image.png
下面没有注释的就是通用配置

  1. const path = require("path")
  2. const HtmlWebpackPlugin = require("html-webpack-plugin")
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin")
  4. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
  5. const TerserPlugin = require("terser-webpack-plugin")
  6. const toml = require("toml")
  7. const yoml = require("yoml")
  8. const json5 = require("json5")
  9. module.export =
  10. {
  11. // 配置多入口
  12. entry:{
  13. index:"./src/index.js",
  14. another:"./src/another-module.js"
  15. },
  16. output:{
  17. // 用于缓存
  18. // filename:"[name].[contenthash].js",
  19. path:path.resolve(__dirname,"../dist"),
  20. clean:true,
  21. assetModuleFilename:"images/[contenthash][ext]",
  22. // pulicPath:"http://localhost:8080"
  23. },
  24. // mode:"production",
  25. // 调试代码
  26. // devtool:"inline-source-map",
  27. plugins: [
  28. new HtmlWebpackPlugin({
  29. template:"./index.html",
  30. filename:"app.html",
  31. inject:"body"
  32. }),
  33. new MiniCssExtractPlugin({
  34. filename:"styles/[contenthash].css"
  35. })
  36. ],
  37. // 用于启动服务
  38. // devServer:{
  39. // static:"./dist"
  40. // },
  41. // 定义哪些资源文件需要打包
  42. module:{
  43. rules:[
  44. {
  45. test:/\.png$/,
  46. type:"asset/resource",
  47. generator:{
  48. filename:"images/[contenthash][ext]"
  49. }
  50. },
  51. {
  52. test:/\.svg$/,
  53. type:"asset/inline",
  54. },
  55. {
  56. test:/\.txt$/,
  57. type:"asset/source",
  58. },
  59. {
  60. test:/\.jpg$/,
  61. type:"asset",
  62. // 解析器
  63. parser:{
  64. // 定义是否生成URL的临界值,当文件大于时创建,反之生成URL
  65. dataUrlCondition:{
  66. maxSize:4*1024*1024
  67. }
  68. }
  69. },
  70. {
  71. test:/\.(css|less)$/,
  72. // 这时需要将css文件抽离,所以将不需要style-loader,替换为MiniCssExtractPlugin-loader
  73. // 这个loader是由MiniCssExtractPlugin插件自带的
  74. //use:[MiniCssExtractPlugin-loader,"css-loader","less-loader"]
  75. use:[MiniCssExtractPlugin.loader,"css-loader","less-loader"]
  76. },
  77. {
  78. test:/\.(woff|woff2|eot|ttf|otf)$/,
  79. type:"asset/resource"
  80. },
  81. {
  82. test:/\.(csv|tsv)$/,
  83. type:"csv-loader"
  84. },
  85. {
  86. test:/\.xml$/,
  87. type:"xml-loader"
  88. },
  89. {
  90. // 需要转换的文件
  91. test:/\.toml$/,
  92. // 转换后的类型
  93. type:"json",
  94. // 使用什么转换
  95. parser:{
  96. parse:toml.parse
  97. }
  98. },
  99. {
  100. // 需要转换的文件
  101. test:/\.yoml$/,
  102. // 转换后的类型
  103. type:"json",
  104. // 使用什么转换
  105. parser:{
  106. parse:yoml.parse
  107. }
  108. },
  109. {
  110. // 需要转换的文件
  111. test:/\.json5$/,
  112. // 转换后的类型
  113. type:"json",
  114. // 使用什么转换
  115. parser:{
  116. parse:json5.parse
  117. }
  118. },
  119. {
  120. test:/\.js$/,
  121. exclude:/node_modules/,
  122. use: {
  123. loader:"babel-loader",
  124. options:{
  125. presets:["@abel/preset-env"],
  126. // 用于定义插件的插件,及@babel/plugin-transform-runtime是属于"babel-loader"的插件
  127. plugins:[
  128. [
  129. "@babel/plugin-transform-runtime"
  130. ]
  131. ]
  132. }
  133. }
  134. }
  135. ]
  136. },
  137. // 优化插件
  138. optimization:{
  139. // minimizer:[
  140. // new CssMinimizerPlugin(),
  141. // new TerserPlugin()
  142. // ]
  143. // 分离代码
  144. // 配置后,将会自动把公共的代码抽离到单独的文件中
  145. splitChunks:{
  146. // 缓存组
  147. // 一般用于定义需要缓存的第三方库
  148. cacheFGroups:{
  149. // 第三方库一般都是在node_modules中的
  150. vendor:{
  151. // 这样配置,是为了确保能获取到node_modules中的文件
  152. test:/[\\/]node_modules[\\/]/,
  153. // 起个名字
  154. name:"vendors",
  155. // all 对所有chunks(模块)做处理
  156. chunks:"all"
  157. }
  158. }
  159. }
  160. },
  161. // 与性能有关的配置
  162. performance: {
  163. hints:false
  164. }
  165. }

开发环境

  1. module.export = {
  2. output:{
  3. // 用于缓存
  4. filename:"script/[name].js",
  5. },
  6. mode:"development",
  7. // 调试代码
  8. devtool:"inline-source-map",
  9. // 用于启动服务
  10. devServer:{
  11. static:"./dist"
  12. }
  13. }

生产环境

  1. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
  2. const TerserPlugin = require("terser-webpack-plugin")
  3. module.export = {
  4. output:{
  5. // 用于缓存
  6. filename:"script/[name].[contenthash].js",
  7. pulicPath:"http://localhost:8080"
  8. },
  9. mode:"production",
  10. optimization:{
  11. minimizer:[
  12. new CssMinimizerPlugin(),
  13. new TerserPlugin()
  14. ]
  15. },
  16. // 与性能有关的配置
  17. performance: {
  18. hints:false
  19. }
  20. }

合并上面的三个配置文件

通过使用webpack-merge合并配置文件
安装:npm i webpack-merge -D
在项目下的config文件下创建文件 webpack.config.js:
通过webpack.config.js将上面三个配置文件合并到一起
image.png

  1. const {merge} = require("webpack-merge")
  2. // 导入配置文件
  3. const commonConfig = require("./webpack.config.common")
  4. const productionConfig = require("./webpack.config.prod")
  5. const developmentConfig = require("./webpack.config.dev")
  6. // 导出
  7. module.exports = (env) => {
  8. switch(true) {
  9. // 通过判断env.development是否为true,是否执行合并
  10. case env.development :
  11. // 合并配置开发配置文件
  12. return merge(commonConfig,developmentConfig)
  13. case env.production:
  14. return merge(commonCofig,productionConfig)
  15. // defult在这里可以不用添加
  16. defult:
  17. return new Error("No matching configuration was found")
  18. }
  19. }

提取后的脚本

提取完公共配置文件后的运行脚本:(需要在运行时传递当前的环境变量)
image.png