webpack_config_dev.cjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  4. const CopyWebpackPlugin = require('copy-webpack-plugin')
  5. const filetool = require('./src/utils/file.js')
  6. const mode = 'development'
  7. const dist_name = 'dist-dev'
  8. const HTMMLPlugin = []
  9. const { localPathResult: AllHTMLLocalFile4xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src/html/error/4xx'))
  10. AllHTMLLocalFile4xx.forEach((filePath) => {
  11. if (!filePath.endsWith('.html')) {
  12. return
  13. }
  14. if (filePath.includes('signal.html')) {
  15. HTMMLPlugin.push(
  16. new HtmlWebpackPlugin({
  17. inject: 'body',
  18. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  19. filename: path.join('error/4xx', filePath),
  20. chunks: ['signal'],
  21. publicPath: '../../'
  22. })
  23. )
  24. return
  25. }
  26. if (filePath.includes('400.html')) {
  27. HTMMLPlugin.push(
  28. new HtmlWebpackPlugin({
  29. inject: 'body',
  30. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  31. filename: path.join('error/4xx', filePath),
  32. chunks: ['common', 'err404'],
  33. publicPath: '../../'
  34. })
  35. )
  36. return
  37. }
  38. HTMMLPlugin.push(
  39. new HtmlWebpackPlugin({
  40. inject: 'body',
  41. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  42. filename: path.join('error/4xx', filePath),
  43. chunks: ['common', 'err4xx'],
  44. publicPath: '../../'
  45. })
  46. )
  47. })
  48. const { localPathResult: AllHTMLLocalFile5xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src/html/error/5xx'))
  49. AllHTMLLocalFile5xx.forEach((filePath) => {
  50. if (!filePath.endsWith('.html')) {
  51. return
  52. }
  53. if (filePath.includes('signal.html')) {
  54. HTMMLPlugin.push(
  55. new HtmlWebpackPlugin({
  56. inject: 'body',
  57. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  58. filename: path.join('error/5xx', filePath),
  59. chunks: ['signal'],
  60. publicPath: '../../'
  61. })
  62. )
  63. return
  64. }
  65. HTMMLPlugin.push(
  66. new HtmlWebpackPlugin({
  67. inject: 'body',
  68. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  69. filename: path.join('error/5xx', filePath),
  70. chunks: ['common', 'err5xx'],
  71. publicPath: '../../'
  72. })
  73. )
  74. })
  75. module.exports = {
  76. mode: mode,
  77. context: __dirname,
  78. performance: {
  79. hints: 'warning', // 或者 'error',取决于你希望如何处理超出限制的情况
  80. maxAssetSize: 5000000, // 设置单个资源的最大尺寸,例如5MB
  81. maxEntrypointSize: 10000000 // 设置入口起点的最大尺寸,例如10MB
  82. },
  83. entry: {
  84. common: path.resolve(__dirname, 'src/common.js'),
  85. index: path.resolve(__dirname, 'src/index.js'),
  86. signal: path.resolve(__dirname, 'src/signal.js'),
  87. new: path.resolve(__dirname, 'src/new.js'),
  88. license: path.resolve(__dirname, 'src/license.js'),
  89. mitorg: path.resolve(__dirname, 'src/mitorg.js'),
  90. err4xx: path.resolve(__dirname, 'src/4xx.js'),
  91. err404: path.resolve(__dirname, 'src/404.js'),
  92. err5xx: path.resolve(__dirname, 'src/5xx.js')
  93. },
  94. output: {
  95. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  96. filename: 'js/[name].[fullhash].bundle.js', //打包后输出文件的文件名
  97. chunkFilename: '[name].bundle.js',
  98. clean: true,
  99. charset: true,
  100. publicPath: '/'
  101. },
  102. resolve: {
  103. alias: {
  104. '@': path.join(__dirname, 'src')
  105. }
  106. },
  107. module: {
  108. rules: [
  109. {
  110. test: /\.(css|scss|sass)$/,
  111. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
  112. },
  113. {
  114. test: /\.(png|jpg|jpeg|svg|gif)$/i,
  115. use: [
  116. {
  117. loader: 'url-loader',
  118. options: {
  119. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  120. fallback: 'file-loader',
  121. outputPath: 'images', // 类似于 file-loader 的配置
  122. name: '[name].[fullhash].[ext]'
  123. }
  124. }
  125. ]
  126. },
  127. {
  128. test: /\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  129. use: [
  130. {
  131. loader: 'url-loader',
  132. options: {
  133. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  134. fallback: 'file-loader',
  135. outputPath: 'videos', // 类似于 file-loader 的配置
  136. name: '[name].[fullhash].[ext]'
  137. }
  138. }
  139. ]
  140. },
  141. {
  142. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  143. use: [
  144. {
  145. loader: 'url-loader',
  146. options: {
  147. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  148. fallback: 'file-loader',
  149. outputPath: 'fonts', // 类似于 file-loader 的配置
  150. name: '[name].[fullhash].[ext]'
  151. }
  152. }
  153. ]
  154. },
  155. {
  156. test: /\.html$/i,
  157. loader: 'html-loader'
  158. },
  159. {
  160. test: require.resolve('jquery'),
  161. loader: 'expose-loader',
  162. options: {
  163. exposes: ['$', 'jQuery']
  164. }
  165. }
  166. ]
  167. },
  168. plugins: [
  169. new CopyWebpackPlugin({
  170. patterns: [
  171. { from: 'public', to: './' },
  172. { from: './config.json', to: './SH_CONFIG.json' },
  173. { from: './LICENSE', to: './' },
  174. { from: './LICENSE_CN', to: './' }
  175. ]
  176. }),
  177. ...HTMMLPlugin,
  178. new HtmlWebpackPlugin({
  179. inject: 'body',
  180. template: path.resolve(__dirname, 'src', 'html', 'index.html'), //指定模板文件
  181. filename: 'index.html',
  182. chunks: ['common', 'index'],
  183. publicPath: './'
  184. }),
  185. new HtmlWebpackPlugin({
  186. inject: 'body',
  187. template: path.resolve(__dirname, 'src/html/LICENSE_US.html'), //指定模板文件
  188. filename: 'LICENSE_US.html',
  189. chunks: ['common', 'license'],
  190. publicPath: './'
  191. }),
  192. new HtmlWebpackPlugin({
  193. inject: 'body',
  194. template: path.resolve(__dirname, 'src/html/LICENSE_CN.html'), //指定模板文件
  195. filename: 'LICENSE_CN.html',
  196. chunks: ['common', 'license'],
  197. publicPath: './'
  198. }),
  199. new HtmlWebpackPlugin({
  200. inject: 'body',
  201. template: path.resolve(__dirname, 'src/html/mitorg.html'), //指定模板文件
  202. filename: 'mitorg.html',
  203. chunks: ['common', 'mitorg'],
  204. publicPath: './'
  205. }),
  206. new HtmlWebpackPlugin({
  207. inject: 'body',
  208. template: path.resolve(__dirname, 'src/html/index.new.signal.html'), //指定模板文件
  209. filename: 'index.new.signal.html',
  210. chunks: ['common', 'new', 'signal'], // 此signal要设置common
  211. publicPath: './'
  212. }),
  213. new HtmlWebpackPlugin({
  214. inject: 'body',
  215. template: path.resolve(__dirname, 'src/html/index.new.html'), //指定模板文件
  216. filename: 'index.new.html',
  217. chunks: ['common', 'new'],
  218. publicPath: './'
  219. }),
  220. new HtmlWebpackPlugin({
  221. inject: 'body',
  222. template: path.resolve(__dirname, 'src/html/error/4xx/404.signal.html'), //指定模板文件
  223. filename: '404.html',
  224. chunks: ['common', 'signal'], // 该signal要设置common
  225. publicPath: './'
  226. }),
  227. new MiniCssExtractPlugin({
  228. filename: 'style/[name].[fullhash].bundle.css',
  229. chunkFilename: 'css/[id].bundle.css'
  230. })
  231. ],
  232. devServer: {
  233. static: {
  234. directory: path.join(__dirname, dist_name)
  235. },
  236. compress: true,
  237. port: 1001,
  238. open: true,
  239. hot: true
  240. }
  241. }