1
0

webpack_config_github.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.html")){
  28. HTMMLPlugin.push(new HtmlWebpackPlugin({
  29. inject:'body',
  30. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  31. filename: path.join("error/4xx", filePath),
  32. chunks: ["signal"],
  33. publicPath: "../../"
  34. }))
  35. return
  36. }
  37. if(filePath.includes("400.html")){
  38. HTMMLPlugin.push(new HtmlWebpackPlugin({
  39. inject:'body',
  40. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  41. filename: path.join("error/4xx", filePath),
  42. chunks: ["common", "err404"],
  43. publicPath: "../../"
  44. }))
  45. return
  46. }
  47. HTMMLPlugin.push(new HtmlWebpackPlugin({
  48. inject:'body',
  49. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  50. filename: path.join("error/4xx", filePath),
  51. chunks: ["common", "err4xx"],
  52. publicPath: "../../"
  53. }))
  54. })
  55. const { localPathResult: AllHTMLLocalFile5xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src/html/error/5xx'))
  56. AllHTMLLocalFile5xx.forEach((filePath) => {
  57. if (!filePath.endsWith(".html")) {
  58. return
  59. }
  60. if(filePath.includes("signal.html")){
  61. HTMMLPlugin.push(new HtmlWebpackPlugin({
  62. inject:'body',
  63. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  64. filename: path.join("error/5xx", filePath),
  65. chunks: ["signal"],
  66. publicPath: "../../"
  67. }))
  68. return
  69. }
  70. HTMMLPlugin.push(new HtmlWebpackPlugin({
  71. inject:'body',
  72. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  73. filename: path.join("error/5xx", filePath),
  74. chunks: ["common", "err5xx"],
  75. publicPath: "../../"
  76. }))
  77. })
  78. module.exports = {
  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].[hash].bundle.js', //打包后输出文件的文件名
  100. chunkFilename: '[name].bundle.js',
  101. clean: true,
  102. publicPath: "./",
  103. charset: true,
  104. },
  105. resolve: {
  106. alias: {
  107. "@": path.join(__dirname, "src")
  108. }
  109. },
  110. optimization: {
  111. minimize: true,
  112. minimizer: [
  113. new TerserPlugin({
  114. terserOptions: {
  115. compress: {
  116. drop_console: true, // 移除console.log (Github/发布版专属)
  117. drop_debugger: true, // 移除debugger (Github/发布版专属)
  118. },
  119. },
  120. }),
  121. ],
  122. },
  123. module: {
  124. rules: [
  125. {
  126. test: /\.css$/, // 匹配.css文件
  127. use: [
  128. MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"
  129. ],
  130. },
  131. {
  132. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  133. type: 'asset/resource',
  134. generator: {
  135. filename: 'images/[name][ext]'
  136. }
  137. },
  138. {
  139. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  140. type: 'asset/resource',
  141. generator: {
  142. filename: 'images/[name][ext]'
  143. }
  144. },
  145. {
  146. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  147. type: 'asset/resource',
  148. generator: {
  149. filename: 'font/[name][ext]'
  150. }
  151. },
  152. {
  153. test: /\.html$/i,
  154. loader: "html-loader",
  155. },
  156. {
  157. test: require.resolve("jquery"),
  158. loader: "expose-loader",
  159. options: {
  160. exposes: ["$", "jQuery"],
  161. },
  162. },
  163. ],
  164. },
  165. plugins: [
  166. new CopyWebpackPlugin({
  167. patterns: [
  168. { from: 'public', to: './' },
  169. { from: './config.json', to: './SH_CONFIG.json' },
  170. { from: './LICENSE', to: './LICENSE_US' },
  171. { from: './LICENSE_CN', to: './LICENSE_CN' },
  172. ],
  173. }),
  174. ...HTMMLPlugin,
  175. new HtmlWebpackPlugin({
  176. inject:'body',
  177. template: path.resolve(__dirname, 'src', "html", "index.html"), //指定模板文件
  178. filename: "index.html",
  179. chunks: ["common", "index"],
  180. minify: html_minify,
  181. publicPath: "./",
  182. }),
  183. new HtmlWebpackPlugin({
  184. inject:'body',
  185. template: path.resolve(__dirname, 'src', "html","LICENSE_CN.html"), //指定模板文件
  186. filename: "LICENSE_CN.html",
  187. chunks: ["common", "license"],
  188. minify: html_minify,
  189. publicPath: "./",
  190. }),
  191. new HtmlWebpackPlugin({
  192. inject:'body',
  193. template: path.resolve(__dirname, 'src', "html","LICENSE_EN.html"), //指定模板文件
  194. filename: "LICENSE_EN.html",
  195. chunks: ["common", "license"],
  196. minify: html_minify,
  197. publicPath: "./",
  198. }),
  199. new HtmlWebpackPlugin({
  200. inject:'body',
  201. template: path.resolve(__dirname, 'src', "html","mitorg.html"), //指定模板文件
  202. filename: "mitorg.html",
  203. chunks: ["common", "mitorg"],
  204. minify: html_minify,
  205. publicPath: "./",
  206. }),
  207. new HtmlWebpackPlugin({
  208. inject:'body',
  209. template: path.resolve(__dirname, 'src', "html","index.new.signal.html"), //指定模板文件
  210. filename: "index.new.signal.html",
  211. chunks: ["common", "new", "signal"], // 此signal要设置common
  212. minify: html_minify,
  213. publicPath: "./",
  214. }),
  215. new HtmlWebpackPlugin({
  216. inject:'body',
  217. template: path.resolve(__dirname, 'src', "html","index.new.html"), //指定模板文件
  218. filename: "index.new.html",
  219. chunks: ["common", "new"],
  220. minify: html_minify,
  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. minify: html_minify,
  229. publicPath: "./",
  230. }),
  231. new MiniCssExtractPlugin({
  232. filename: 'style/[name].[hash].bundle.css',
  233. chunkFilename: 'css/[id].bundle.css',
  234. }),
  235. ],
  236. devServer: {
  237. static: {
  238. directory: path.join(__dirname, dist_name),
  239. },
  240. compress: true,
  241. port: 1001,
  242. open: true,
  243. hot: false,
  244. },
  245. };