webpack_config_prod.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  4. const filetool = require("./src/utils/file.js")
  5. const entry = {}
  6. const HTMMLPlugin = []
  7. const dist_name = "dist-prod"
  8. const { localPathResult: AllHTMLLocalFile4xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src', 'html', "error", "4xx"))
  9. AllHTMLLocalFile4xx.forEach((filePath) => {
  10. if (!filePath.endsWith(".html")) {
  11. return
  12. }
  13. if(filePath.includes("400.html")){
  14. entry["404"] = path.resolve(__dirname, 'src', '404.js')
  15. HTMMLPlugin.push(new HtmlWebpackPlugin({
  16. inject:'body',
  17. template: path.resolve(__dirname, 'src', 'html', "error", "4xx", "404.html"), //指定模板文件
  18. filename: path.join("error", filePath),
  19. chunks: ["404"]
  20. }))
  21. return
  22. }
  23. const name = "ck" + filePath.replace(".html", "").replace("/", "-").replace('\\', '-')
  24. entry[name] = path.resolve(__dirname, 'src', '4xx.js')
  25. HTMMLPlugin.push(new HtmlWebpackPlugin({
  26. inject:'body',
  27. template: path.resolve(__dirname, 'src', 'html', "error", "4xx", filePath), //指定模板文件
  28. filename: path.join("error", filePath),
  29. chunks: [name]
  30. }))
  31. })
  32. const { localPathResult: AllHTMLLocalFile5xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src', 'html', "error", "5xx"))
  33. AllHTMLLocalFile5xx.forEach((filePath) => {
  34. if (!filePath.endsWith(".html")) {
  35. return
  36. }
  37. const name = "ck" + filePath.replace(".html", "").replace("/", "-").replace('\\', '-')
  38. entry[name] = path.resolve(__dirname, 'src', '5xx.js')
  39. HTMMLPlugin.push(new HtmlWebpackPlugin({
  40. inject:'body',
  41. template: path.resolve(__dirname, 'src', 'html', "error", "5xx", filePath), //指定模板文件
  42. filename: path.join("error", filePath),
  43. chunks: [name]
  44. }))
  45. })
  46. module.exports = {
  47. mode: 'production',
  48. performance: {
  49. hints: 'warning', // 或者 'error',取决于你希望如何处理超出限制的情况
  50. maxAssetSize: 5000000, // 设置单个资源的最大尺寸,例如5MB
  51. maxEntrypointSize: 10000000, // 设置入口起点的最大尺寸,例如10MB
  52. },
  53. entry: {
  54. ...entry,
  55. index: path.resolve(__dirname, 'src', 'index.js'),
  56. new: path.resolve(__dirname, 'src', 'new.js'),
  57. license: path.resolve(__dirname, 'src', 'license.js'),
  58. mitorg: path.resolve(__dirname, 'src', 'mitorg.js'),
  59. },
  60. output: {
  61. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  62. filename: 'js/[name].[hash].bundle.js', //打包后输出文件的文件名
  63. chunkFilename: '[name].bundle.js',
  64. clean: true,
  65. publicPath: "./",
  66. charset: true,
  67. },
  68. module: {
  69. rules: [
  70. {
  71. test: /\.css$/,
  72. use: ['style-loader', 'css-loader']
  73. },
  74. {
  75. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  76. type: 'asset/resource',
  77. generator: {
  78. filename: 'images/[name][ext]'
  79. }
  80. },
  81. {
  82. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  83. type: 'asset/resource',
  84. generator: {
  85. filename: 'font/[name][ext]'
  86. }
  87. },
  88. {
  89. test: /\.html$/i,
  90. loader: "html-loader",
  91. },
  92. {
  93. test: require.resolve("jquery"),
  94. loader: "expose-loader",
  95. options: {
  96. exposes: ["$", "jQuery"],
  97. },
  98. },
  99. ],
  100. },
  101. plugins: [
  102. ...HTMMLPlugin,
  103. new HtmlWebpackPlugin({
  104. inject:'body',
  105. template: path.resolve(__dirname, 'src', "html", "index.html"), //指定模板文件
  106. filename: "index.html",
  107. chunks: ["index"]
  108. }),
  109. new HtmlWebpackPlugin({
  110. inject:'body',
  111. template: path.resolve(__dirname, 'src', "html","LICENSE_CN.html"), //指定模板文件
  112. filename: "LICENSE_CN.html",
  113. chunks: ["license"]
  114. }),
  115. new HtmlWebpackPlugin({
  116. inject:'body',
  117. template: path.resolve(__dirname, 'src', "html","LICENSE_EN.html"), //指定模板文件
  118. filename: "LICENSE_EN.html",
  119. chunks: ["license"]
  120. }),
  121. new HtmlWebpackPlugin({
  122. inject:'body',
  123. template: path.resolve(__dirname, 'src', "html","mitorg.html"), //指定模板文件
  124. filename: "mitorg.html",
  125. chunks: ["mitorg"]
  126. }),
  127. new HtmlWebpackPlugin({
  128. inject:'body',
  129. template: path.resolve(__dirname, 'src', "html","index.new.signal.html"), //指定模板文件
  130. filename: "index.new.signal.html",
  131. chunks: ["new"]
  132. }),
  133. new HtmlWebpackPlugin({
  134. inject:'body',
  135. template: path.resolve(__dirname, 'src', "html","index.new.html"), //指定模板文件
  136. filename: "index.new.html",
  137. chunks: ["new"]
  138. }),
  139. new MiniCssExtractPlugin({
  140. filename: '[name].[hash].bundle.css',
  141. chunkFilename: '[id].bundle.css',
  142. }),
  143. ],
  144. devServer: {
  145. static: {
  146. directory: path.join(__dirname, dist_name),
  147. },
  148. compress: true,
  149. port: 1002,
  150. open: true,
  151. hot: false,
  152. },
  153. };