webpack_config_dev.js 7.6 KB

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