1
0

webpack_config_github.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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: /\.(js|mjs|cjs)$/,
  140. exclude: /(node_modules|bower_components)/,
  141. use: {
  142. loader: 'babel-loader'
  143. }
  144. },
  145. {
  146. test: /\.(css|scss|sass)$/,
  147. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
  148. },
  149. {
  150. test: /\.(png|jpg|jpeg|svg|gif)$/i,
  151. use: [
  152. {
  153. loader: 'url-loader',
  154. options: {
  155. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  156. fallback: 'file-loader',
  157. outputPath: 'images', // 类似于 file-loader 的配置
  158. name: '[name].[fullhash].[ext]'
  159. }
  160. }
  161. ]
  162. },
  163. {
  164. test: /\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  165. use: [
  166. {
  167. loader: 'url-loader',
  168. options: {
  169. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  170. fallback: 'file-loader',
  171. outputPath: 'videos', // 类似于 file-loader 的配置
  172. name: '[name].[fullhash].[ext]'
  173. }
  174. }
  175. ]
  176. },
  177. {
  178. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  179. use: [
  180. {
  181. loader: 'url-loader',
  182. options: {
  183. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  184. fallback: 'file-loader',
  185. outputPath: 'fonts', // 类似于 file-loader 的配置
  186. name: '[name].[fullhash].[ext]'
  187. }
  188. }
  189. ]
  190. },
  191. {
  192. test: /\.html$/i,
  193. loader: 'html-loader'
  194. }
  195. ]
  196. },
  197. plugins: [
  198. new CopyWebpackPlugin({
  199. patterns: [
  200. { from: 'public', to: './' },
  201. { from: './config.json', to: './SH_CONFIG.json' },
  202. { from: './LICENSE', to: './' },
  203. { from: './LICENSE_CN', to: './' }
  204. ]
  205. }),
  206. ...HTMMLPlugin,
  207. new HtmlWebpackPlugin({
  208. inject: 'body',
  209. template: path.resolve(__dirname, 'src', 'html', 'index.html'), //指定模板文件
  210. filename: 'index.html',
  211. chunks: ['common', 'index'],
  212. minify: html_minify,
  213. publicPath: './'
  214. }),
  215. new HtmlWebpackPlugin({
  216. inject: 'body',
  217. template: path.resolve(__dirname, 'src', 'html', 'LICENSE_US.html'), //指定模板文件
  218. filename: 'LICENSE_US.html',
  219. chunks: ['common', 'license'],
  220. minify: html_minify,
  221. publicPath: './'
  222. }),
  223. new HtmlWebpackPlugin({
  224. inject: 'body',
  225. template: path.resolve(__dirname, 'src', 'html', 'LICENSE_CN.html'), //指定模板文件
  226. filename: 'LICENSE_CN.html',
  227. chunks: ['common', 'license'],
  228. minify: html_minify,
  229. publicPath: './'
  230. }),
  231. new HtmlWebpackPlugin({
  232. inject: 'body',
  233. template: path.resolve(__dirname, 'src', 'html', 'mitorg.html'), //指定模板文件
  234. filename: 'mitorg.html',
  235. chunks: ['common', 'mitorg'],
  236. minify: html_minify,
  237. publicPath: './'
  238. }),
  239. new HtmlWebpackPlugin({
  240. inject: 'body',
  241. template: path.resolve(__dirname, 'src', 'html', 'index.new.signal.html'), //指定模板文件
  242. filename: 'index.new.signal.html',
  243. chunks: ['common', 'new', 'signal'], // 此signal要设置common
  244. minify: html_minify,
  245. publicPath: './'
  246. }),
  247. new HtmlWebpackPlugin({
  248. inject: 'body',
  249. template: path.resolve(__dirname, 'src', 'html', 'index.new.html'), //指定模板文件
  250. filename: 'index.new.html',
  251. chunks: ['common', 'new'],
  252. minify: html_minify,
  253. publicPath: './'
  254. }),
  255. new HtmlWebpackPlugin({
  256. inject: 'body',
  257. template: path.resolve(__dirname, 'src/html/error/4xx/404.signal.html'), //指定模板文件
  258. filename: '404.html',
  259. chunks: ['common', 'signal'], // 此signal要设置common
  260. minify: html_minify,
  261. publicPath: './'
  262. }),
  263. new MiniCssExtractPlugin({
  264. filename: 'style/[name].[hash].bundle.css',
  265. chunkFilename: 'css/[id].bundle.css'
  266. })
  267. ],
  268. devServer: {
  269. static: {
  270. directory: path.join(__dirname, dist_name)
  271. },
  272. compress: true,
  273. port: 1001,
  274. open: true,
  275. hot: false
  276. }
  277. }
  278. export default config