webpack_config_dev.js 4.6 KB

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