webpack_config_dev.js 5.2 KB

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