1
0

webpack_config_dev.js 7.6 KB

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