webpack_config_prod.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 CopyWebpackPlugin = require('copy-webpack-plugin');
  6. const mode = "production"
  7. const dist_name = "dist-prod"
  8. const html_minify = {
  9. collapseWhitespace: true,
  10. removeComments: true,
  11. removeRedundantAttributes: true,
  12. useShortDoctype: true,
  13. removeEmptyAttributes: true,
  14. removeStyleLinkTypeAttributes: true,
  15. keepClosingSlash: true,
  16. minifyJS: true,
  17. minifyCSS: true,
  18. minifyURLs: true,
  19. }
  20. const HTMMLPlugin = []
  21. const { localPathResult: AllHTMLLocalFile4xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src/html/error/4xx'))
  22. AllHTMLLocalFile4xx.forEach((filePath) => {
  23. if (!filePath.endsWith(".html")) {
  24. return
  25. }
  26. if(filePath.includes("signal.html")){
  27. HTMMLPlugin.push(new HtmlWebpackPlugin({
  28. inject:'body',
  29. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  30. filename: path.join("error/4xx", filePath),
  31. chunks: ["signal"],
  32. publicPath: "../../"
  33. }))
  34. return
  35. }
  36. if(filePath.includes("400.html")){
  37. HTMMLPlugin.push(new HtmlWebpackPlugin({
  38. inject:'body',
  39. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  40. filename: path.join("error/4xx", filePath),
  41. chunks: ["common", "err404"],
  42. publicPath: "../../"
  43. }))
  44. return
  45. }
  46. HTMMLPlugin.push(new HtmlWebpackPlugin({
  47. inject:'body',
  48. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  49. filename: path.join("error/4xx", filePath),
  50. chunks: ["common", "err4xx"],
  51. publicPath: "../../"
  52. }))
  53. })
  54. const { localPathResult: AllHTMLLocalFile5xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src/html/error/5xx'))
  55. AllHTMLLocalFile5xx.forEach((filePath) => {
  56. if (!filePath.endsWith(".html")) {
  57. return
  58. }
  59. if(filePath.includes("signal.html")){
  60. HTMMLPlugin.push(new HtmlWebpackPlugin({
  61. inject:'body',
  62. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  63. filename: path.join("error/5xx", filePath),
  64. chunks: ["signal"],
  65. publicPath: "../../"
  66. }))
  67. return
  68. }
  69. HTMMLPlugin.push(new HtmlWebpackPlugin({
  70. inject:'body',
  71. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  72. filename: path.join("error/5xx", filePath),
  73. chunks: ["common", "err5xx"],
  74. publicPath: "../../"
  75. }))
  76. })
  77. module.exports = {
  78. mode: mode,
  79. context: __dirname,
  80. performance: {
  81. hints: 'warning', // 或者 'error',取决于你希望如何处理超出限制的情况
  82. maxAssetSize: 5000000, // 设置单个资源的最大尺寸,例如5MB
  83. maxEntrypointSize: 10000000, // 设置入口起点的最大尺寸,例如10MB
  84. },
  85. entry: {
  86. common: path.resolve(__dirname, 'src/common.js'),
  87. index: path.resolve(__dirname, 'src/index.js'),
  88. signal: path.resolve(__dirname, 'src/signal.js'),
  89. new: path.resolve(__dirname, 'src/new.js'),
  90. license: path.resolve(__dirname, 'src/license.js'),
  91. mitorg: path.resolve(__dirname, 'src/mitorg.js'),
  92. err4xx: path.resolve(__dirname, 'src/4xx.js'),
  93. err404: path.resolve(__dirname, 'src/404.js'),
  94. err5xx: path.resolve(__dirname, 'src/5xx.js'),
  95. },
  96. output: {
  97. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  98. filename: 'js/[name].[fullhash].bundle.js', //打包后输出文件的文件名
  99. chunkFilename: '[name].bundle.js',
  100. clean: true,
  101. charset: true,
  102. publicPath: "/"
  103. },
  104. resolve: {
  105. alias: {
  106. "@": path.join(__dirname, "src")
  107. },
  108. },
  109. module: {
  110. rules: [
  111. {
  112. test: /\.(css|scss|sass)$/,
  113. use: [
  114. MiniCssExtractPlugin.loader,
  115. 'css-loader',
  116. 'postcss-loader',
  117. 'sass-loader',
  118. ],
  119. },
  120. {
  121. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  122. use: [
  123. {
  124. loader: 'url-loader',
  125. options: {
  126. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  127. fallback: 'file-loader',
  128. outputPath: 'images', // 类似于 file-loader 的配置
  129. name: '[name].[fullhash].[ext]',
  130. },
  131. },
  132. ],
  133. },
  134. {
  135. test:/\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  136. use: [
  137. {
  138. loader: 'url-loader',
  139. options: {
  140. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  141. fallback: 'file-loader',
  142. outputPath: 'videos', // 类似于 file-loader 的配置
  143. name: '[name].[fullhash].[ext]',
  144. },
  145. },
  146. ],
  147. },
  148. {
  149. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  150. use: [
  151. {
  152. loader: 'url-loader',
  153. options: {
  154. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  155. fallback: 'file-loader',
  156. outputPath: 'fonts', // 类似于 file-loader 的配置
  157. name: '[name].[fullhash].[ext]',
  158. },
  159. },
  160. ],
  161. },
  162. {
  163. test: /\.html$/i,
  164. loader: "html-loader",
  165. },
  166. {
  167. test: require.resolve("jquery"),
  168. loader: "expose-loader",
  169. options: {
  170. exposes: ["$", "jQuery"],
  171. },
  172. },
  173. ],
  174. },
  175. plugins: [
  176. new CopyWebpackPlugin({
  177. patterns: [
  178. { from: 'public', to: './' },
  179. { from: './config.json', to: './SH_CONFIG.json' },
  180. { from: './LICENSE', to: './' },
  181. { from: './LICENSE_CN', to: './' },
  182. ],
  183. }),
  184. ...HTMMLPlugin,
  185. new HtmlWebpackPlugin({
  186. inject:'body',
  187. template: path.resolve(__dirname, 'src', "html", "index.html"), //指定模板文件
  188. filename: "index.html",
  189. chunks: ["common", "index"],
  190. minify: html_minify,
  191. publicPath: "./",
  192. }),
  193. new HtmlWebpackPlugin({
  194. inject:'body',
  195. template: path.resolve(__dirname, 'src', "html","LICENSE_US.html"), //指定模板文件
  196. filename: "LICENSE_US.html",
  197. chunks: ["common", "license"],
  198. minify: html_minify,
  199. publicPath: "./",
  200. }),
  201. new HtmlWebpackPlugin({
  202. inject:'body',
  203. template: path.resolve(__dirname, 'src/html/LICENSE_CN.html'), //指定模板文件
  204. filename: "LICENSE_CN.html",
  205. chunks: ["common", "license"],
  206. minify: html_minify,
  207. publicPath: "./",
  208. }),
  209. new HtmlWebpackPlugin({
  210. inject:'body',
  211. template: path.resolve(__dirname, 'src', "html","mitorg.html"), //指定模板文件
  212. filename: "mitorg.html",
  213. chunks: ["common", "mitorg"],
  214. minify: html_minify,
  215. publicPath: "./",
  216. }),
  217. new HtmlWebpackPlugin({
  218. inject:'body',
  219. template: path.resolve(__dirname, 'src', "html","index.new.signal.html"), //指定模板文件
  220. filename: "index.new.signal.html",
  221. chunks: ["common", "new", "signal"], // 此signal要设置common
  222. minify: html_minify,
  223. publicPath: "./",
  224. }),
  225. new HtmlWebpackPlugin({
  226. inject:'body',
  227. template: path.resolve(__dirname, 'src', "html","index.new.html"), //指定模板文件
  228. filename: "index.new.html",
  229. chunks: ["common", "new"],
  230. minify: html_minify,
  231. publicPath: "./",
  232. }),
  233. new HtmlWebpackPlugin({
  234. inject:'body',
  235. template: path.resolve(__dirname, 'src/html/error/4xx/404.signal.html'), //指定模板文件
  236. filename: "404.html",
  237. chunks: ["common", "signal"], // 此signal要设置common
  238. minify: html_minify,
  239. publicPath: "./",
  240. }),
  241. new MiniCssExtractPlugin({
  242. filename: 'style/[name].[hash].bundle.css',
  243. chunkFilename: 'css/[id].bundle.css',
  244. }),
  245. ],
  246. devServer: {
  247. static: {
  248. directory: path.join(__dirname, dist_name),
  249. },
  250. compress: true,
  251. port: 1001,
  252. open: true,
  253. hot: true,
  254. },
  255. };