webpack_config_github.js 8.4 KB

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