webpack_config_github.cjs 8.3 KB

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