1
0

webpack_config_github.js 8.2 KB

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