webpack_config_github.js 5.1 KB

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