webpack_config_prod.cjs 7.9 KB

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