webpack_config_prod.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import path from 'path'
  2. import HtmlWebpackPlugin from 'html-webpack-plugin'
  3. import MiniCssExtractPlugin from 'mini-css-extract-plugin'
  4. import CopyWebpackPlugin from 'copy-webpack-plugin'
  5. import { fileURLToPath } from 'url'
  6. const __filename = fileURLToPath(import.meta.url)
  7. const __dirname = path.dirname(__filename)
  8. const mode = 'production'
  9. const dist_name = 'dist-prod'
  10. const html_minify = {
  11. collapseWhitespace: true,
  12. removeComments: true,
  13. removeRedundantAttributes: true,
  14. useShortDoctype: true,
  15. removeEmptyAttributes: true,
  16. removeStyleLinkTypeAttributes: true,
  17. keepClosingSlash: true,
  18. minifyJS: true,
  19. minifyCSS: true,
  20. minifyURLs: true
  21. }
  22. const config = {
  23. mode: mode,
  24. context: __dirname,
  25. performance: {
  26. hints: 'warning', // 或者 'error',取决于您希望如何处理超出限制的情况
  27. maxAssetSize: 5000000, // 设置单个资源的最大尺寸,例如5MB
  28. maxEntrypointSize: 10000000 // 设置入口起点的最大尺寸,例如10MB
  29. },
  30. entry: {
  31. common: path.resolve(__dirname, 'src/common.js'),
  32. index: path.resolve(__dirname, 'src/index.js'),
  33. leave: path.resolve(__dirname, 'src/leave.js'),
  34. contributors: path.resolve(__dirname, 'src/contributors.js'),
  35. sponsors: path.resolve(__dirname, 'src/sponsors.js'),
  36. sponsorsnow: path.resolve(__dirname, 'src/sponsors-now.js'),
  37. zsxq: path.resolve(__dirname, 'src/zsxq.js'),
  38. notfound: path.resolve(__dirname, 'src/404.js')
  39. },
  40. output: {
  41. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  42. filename: 'js/[name].[fullhash].bundle.js', //打包后输出文件的文件名
  43. chunkFilename: '[name].bundle.js',
  44. clean: true,
  45. charset: true,
  46. publicPath: '/'
  47. },
  48. resolve: {
  49. alias: {
  50. '@': path.join(__dirname, 'src')
  51. }
  52. },
  53. module: {
  54. rules: [
  55. {
  56. test: /\.(js|mjs|cjs)$/,
  57. exclude: /(node_modules|bower_components)/,
  58. use: {
  59. loader: 'babel-loader'
  60. }
  61. },
  62. {
  63. test: /\.(css|scss|sass)$/,
  64. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
  65. },
  66. {
  67. test: /\.(png|jpg|jpeg|svg|gif)$/i,
  68. use: [
  69. {
  70. loader: 'url-loader',
  71. options: {
  72. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  73. fallback: 'file-loader',
  74. outputPath: 'images', // 类似于 file-loader 的配置
  75. name: '[name].[hash].[ext]'
  76. }
  77. }
  78. ]
  79. },
  80. {
  81. test: /\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  82. use: [
  83. {
  84. loader: 'url-loader',
  85. options: {
  86. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  87. fallback: 'file-loader',
  88. outputPath: 'videos', // 类似于 file-loader 的配置
  89. name: '[name].[hash].[ext]'
  90. }
  91. }
  92. ]
  93. },
  94. {
  95. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  96. use: [
  97. {
  98. loader: 'url-loader',
  99. options: {
  100. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  101. fallback: 'file-loader',
  102. outputPath: 'fonts', // 类似于 file-loader 的配置
  103. name: '[name].[hash].[ext]'
  104. }
  105. }
  106. ]
  107. },
  108. {
  109. test: /\.html$/i,
  110. loader: 'html-loader'
  111. }
  112. ]
  113. },
  114. plugins: [
  115. new CopyWebpackPlugin({
  116. patterns: [
  117. { from: 'public', to: './' },
  118. { from: './config.json', to: './CONFIG.json' }
  119. ]
  120. }),
  121. new HtmlWebpackPlugin({
  122. inject: 'body',
  123. template: path.resolve(__dirname, 'src/html/index.html'), //指定模板文件
  124. filename: 'index.html',
  125. chunks: ['common', 'index'],
  126. minify: html_minify,
  127. publicPath: './'
  128. }),
  129. new HtmlWebpackPlugin({
  130. inject: 'body',
  131. template: path.resolve(__dirname, 'src/html/contributors.html'), //指定模板文件
  132. filename: 'contributors.html',
  133. chunks: ['common', 'contributors'],
  134. minify: html_minify,
  135. publicPath: './'
  136. }),
  137. new HtmlWebpackPlugin({
  138. inject: 'body',
  139. template: path.resolve(__dirname, 'src/html/sponsors.html'), //指定模板文件
  140. filename: 'sponsors.html',
  141. chunks: ['common', 'sponsors'],
  142. minify: html_minify,
  143. publicPath: './'
  144. }),
  145. new HtmlWebpackPlugin({
  146. inject: 'body',
  147. template: path.resolve(__dirname, 'src/html/sponsors-now.html'), //指定模板文件
  148. filename: 'sponsors-now.html',
  149. chunks: ['common', 'sponsorsnow'],
  150. minify: html_minify,
  151. publicPath: './'
  152. }),
  153. new HtmlWebpackPlugin({
  154. inject: 'body',
  155. template: path.resolve(__dirname, 'src/html/zsxq.html'), //指定模板文件
  156. filename: 'zsxq.html',
  157. chunks: ['common', 'zsxq'],
  158. minify: html_minify,
  159. publicPath: './'
  160. }),
  161. new HtmlWebpackPlugin({
  162. inject: 'body',
  163. template: path.resolve(__dirname, 'src/html/contributors.html'), //指定模板文件
  164. filename: 'contributors.html',
  165. chunks: ['common', 'contributors'],
  166. minify: html_minify,
  167. publicPath: './'
  168. }),
  169. new MiniCssExtractPlugin({
  170. filename: 'style/[name].[fullhash].bundle.css',
  171. chunkFilename: 'css/[id].bundle.css'
  172. })
  173. ],
  174. devServer: {
  175. static: {
  176. directory: path.join(__dirname, dist_name)
  177. },
  178. compress: true,
  179. port: 1001,
  180. open: true,
  181. hot: true
  182. }
  183. }
  184. export default config