webpack_config_github.js 6.0 KB

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