webpack_config_github.js 8.2 KB

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