webpack_config_dev.js 4.1 KB

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