webpack_config_github.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. sponsors: path.resolve(__dirname, 'src/sponsors.js'),
  36. contributors: path.resolve(__dirname, 'src/contributors.js'),
  37. notfound: path.resolve(__dirname, 'src/404.js')
  38. },
  39. output: {
  40. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  41. filename: 'js/[name].[fullhash].bundle.js', //打包后输出文件的文件名
  42. chunkFilename: '[name].bundle.js',
  43. clean: true,
  44. charset: true,
  45. publicPath: '/'
  46. },
  47. resolve: {
  48. alias: {
  49. '@': path.join(__dirname, 'src')
  50. }
  51. },
  52. optimization: {
  53. minimize: true,
  54. minimizer: [
  55. new TerserPlugin({
  56. terserOptions: {
  57. compress: {
  58. drop_console: true, // 移除console.log (Github/发布版专属)
  59. drop_debugger: true // 移除debugger (Github/发布版专属)
  60. }
  61. }
  62. })
  63. ]
  64. },
  65. module: {
  66. rules: [
  67. {
  68. test: /\.(js|mjs|cjs)$/,
  69. exclude: /(node_modules|bower_components)/,
  70. use: {
  71. loader: 'babel-loader'
  72. }
  73. },
  74. {
  75. test: /\.(css|scss|sass)$/,
  76. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
  77. },
  78. {
  79. test: /\.(png|jpg|jpeg|svg|gif)$/i,
  80. use: [
  81. {
  82. loader: 'url-loader',
  83. options: {
  84. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  85. fallback: 'file-loader',
  86. outputPath: 'images', // 类似于 file-loader 的配置
  87. name: '[name].[hash].[ext]'
  88. }
  89. }
  90. ]
  91. },
  92. {
  93. test: /\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  94. use: [
  95. {
  96. loader: 'url-loader',
  97. options: {
  98. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  99. fallback: 'file-loader',
  100. outputPath: 'videos', // 类似于 file-loader 的配置
  101. name: '[name].[hash].[ext]'
  102. }
  103. }
  104. ]
  105. },
  106. {
  107. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  108. use: [
  109. {
  110. loader: 'url-loader',
  111. options: {
  112. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  113. fallback: 'file-loader',
  114. outputPath: 'fonts', // 类似于 file-loader 的配置
  115. name: '[name].[hash].[ext]'
  116. }
  117. }
  118. ]
  119. },
  120. {
  121. test: /\.html$/i,
  122. loader: 'html-loader'
  123. }
  124. ]
  125. },
  126. plugins: [
  127. new CopyWebpackPlugin({
  128. patterns: [
  129. { from: 'public', to: './' },
  130. { from: './config.json', to: './CONFIG.json' }
  131. ]
  132. }),
  133. new HtmlWebpackPlugin({
  134. inject: 'body',
  135. template: path.resolve(__dirname, 'src/html/index.html'), //指定模板文件
  136. filename: 'index.html',
  137. chunks: ['common', 'index'],
  138. minify: html_minify,
  139. publicPath: './'
  140. }),
  141. new HtmlWebpackPlugin({
  142. inject: 'body',
  143. template: path.resolve(__dirname, 'src/html/leave.html'), //指定模板文件
  144. filename: 'leave.html',
  145. chunks: ['common', 'leave'],
  146. minify: html_minify,
  147. publicPath: './'
  148. }),
  149. new HtmlWebpackPlugin({
  150. inject: 'body',
  151. template: path.resolve(__dirname, 'src/html/sponsors.html'), //指定模板文件
  152. filename: 'sponsors.html',
  153. chunks: ['common', 'sponsors'],
  154. minify: html_minify,
  155. publicPath: './'
  156. }),
  157. new HtmlWebpackPlugin({
  158. inject: 'body',
  159. template: path.resolve(__dirname, 'src/html/contributors.html'), //指定模板文件
  160. filename: 'contributors.html',
  161. chunks: ['common', 'contributors'],
  162. minify: html_minify,
  163. publicPath: './'
  164. }),
  165. new HtmlWebpackPlugin({
  166. inject: 'body',
  167. template: path.resolve(__dirname, 'src/html/404.html'), //指定模板文件
  168. filename: '404.html',
  169. chunks: ['common', 'notfound'],
  170. minify: html_minify,
  171. publicPath: './'
  172. }),
  173. new MiniCssExtractPlugin({
  174. filename: 'style/[name].[fullhash].bundle.css',
  175. chunkFilename: 'css/[id].bundle.css'
  176. })
  177. ],
  178. devServer: {
  179. static: {
  180. directory: path.join(__dirname, dist_name)
  181. },
  182. compress: true,
  183. port: 1001,
  184. open: true,
  185. hot: false
  186. }
  187. }
  188. export default config