webpack_config_github.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 entry = {}
  8. const HTMMLPlugin = []
  9. const mode = "production"
  10. const dist_name = "docs"
  11. const html_minify = {
  12. collapseWhitespace: true,
  13. removeComments: true,
  14. removeRedundantAttributes: true,
  15. useShortDoctype: true,
  16. removeEmptyAttributes: true,
  17. removeStyleLinkTypeAttributes: true,
  18. keepClosingSlash: true,
  19. minifyJS: true,
  20. minifyCSS: true,
  21. minifyURLs: true,
  22. }
  23. const { localPathResult: AllHTMLLocalFile4xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src', 'html', "error", "4xx"))
  24. AllHTMLLocalFile4xx.forEach((filePath) => {
  25. if (!filePath.endsWith(".html")) {
  26. return
  27. }
  28. if(filePath.includes("400.html")){
  29. entry["404"] = path.resolve(__dirname, 'src', '404.js')
  30. HTMMLPlugin.push(new HtmlWebpackPlugin({
  31. inject:'body',
  32. template: path.resolve(__dirname, 'src', 'html', "error", "4xx", "404.html"), //指定模板文件
  33. filename: path.join("error", filePath),
  34. chunks: ["404"],
  35. minify: html_minify,
  36. }))
  37. return
  38. }
  39. const name = "ck" + filePath.replace(".html", "").replace("/", "-").replace('\\', '-')
  40. entry[name] = path.resolve(__dirname, 'src', '4xx.js')
  41. HTMMLPlugin.push(new HtmlWebpackPlugin({
  42. inject:'body',
  43. template: path.resolve(__dirname, 'src', 'html', "error", "4xx", filePath), //指定模板文件
  44. filename: path.join("error", filePath),
  45. chunks: [name],
  46. minify: html_minify,
  47. }))
  48. })
  49. const { localPathResult: AllHTMLLocalFile5xx } = filetool.getAllFilePaths(path.resolve(__dirname, 'src', 'html', "error", "5xx"))
  50. AllHTMLLocalFile5xx.forEach((filePath) => {
  51. if (!filePath.endsWith(".html")) {
  52. return
  53. }
  54. const name = "ck" + filePath.replace(".html", "").replace("/", "-").replace('\\', '-')
  55. entry[name] = path.resolve(__dirname, 'src', '5xx.js')
  56. HTMMLPlugin.push(new HtmlWebpackPlugin({
  57. inject:'body',
  58. template: path.resolve(__dirname, 'src', 'html', "error", "5xx", filePath), //指定模板文件
  59. filename: path.join("error", filePath),
  60. chunks: [name],
  61. minify: html_minify,
  62. }))
  63. })
  64. module.exports = {
  65. mode: mode,
  66. context: __dirname,
  67. performance: {
  68. hints: 'warning', // 或者 'error',取决于你希望如何处理超出限制的情况
  69. maxAssetSize: 5000000, // 设置单个资源的最大尺寸,例如5MB
  70. maxEntrypointSize: 10000000, // 设置入口起点的最大尺寸,例如10MB
  71. },
  72. entry: {
  73. ...entry,
  74. index: path.resolve(__dirname, 'src', 'index.js'),
  75. new: path.resolve(__dirname, 'src', 'new.js'),
  76. license: path.resolve(__dirname, 'src', 'license.js'),
  77. mitorg: path.resolve(__dirname, 'src', 'mitorg.js'),
  78. },
  79. output: {
  80. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  81. filename: 'js/[name].[hash].bundle.js', //打包后输出文件的文件名
  82. chunkFilename: '[name].bundle.js',
  83. clean: true,
  84. publicPath: "./",
  85. charset: true,
  86. },
  87. resolve: {
  88. alias: {
  89. "@": path.join(__dirname, "src")
  90. }
  91. },
  92. optimization: {
  93. minimize: true,
  94. minimizer: [
  95. new TerserPlugin({
  96. terserOptions: {
  97. compress: {
  98. drop_console: true, // 移除console.log (Github/发布版专属)
  99. drop_debugger: true, // 移除debugger (Github/发布版专属)
  100. },
  101. },
  102. }),
  103. ],
  104. },
  105. module: {
  106. rules: [
  107. {
  108. test: /\.css$/,
  109. use: ['style-loader', 'css-loader']
  110. },
  111. {
  112. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  113. type: 'asset/resource',
  114. generator: {
  115. filename: 'images/[name][ext]'
  116. }
  117. },
  118. {
  119. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  120. type: 'asset/resource',
  121. generator: {
  122. filename: 'images/[name][ext]'
  123. }
  124. },
  125. {
  126. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  127. type: 'asset/resource',
  128. generator: {
  129. filename: 'font/[name][ext]'
  130. }
  131. },
  132. {
  133. test: /\.html$/i,
  134. loader: "html-loader",
  135. },
  136. {
  137. test: require.resolve("jquery"),
  138. loader: "expose-loader",
  139. options: {
  140. exposes: ["$", "jQuery"],
  141. },
  142. },
  143. ],
  144. },
  145. plugins: [
  146. new CopyWebpackPlugin({
  147. patterns: [
  148. { from: 'public', to: './' }, // 这里假设你想将 public 文件夹下的所有内容复制到输出目录的根目录下
  149. { from: './config.json', to: './SH_CONFIG.json' },
  150. ],
  151. }),
  152. ...HTMMLPlugin,
  153. new HtmlWebpackPlugin({
  154. inject:'body',
  155. template: path.resolve(__dirname, 'src', "html", "index.html"), //指定模板文件
  156. filename: "index.html",
  157. chunks: ["index"],
  158. minify: html_minify,
  159. templateParameters: {
  160. 'process.env.SH_ICP': JSON.stringify(process.env.SH_ICP),
  161. 'process.env.SH_WANGAN': JSON.stringify(process.env.SH_WANGAN),
  162. }
  163. }),
  164. new HtmlWebpackPlugin({
  165. inject:'body',
  166. template: path.resolve(__dirname, 'src', "html","LICENSE_CN.html"), //指定模板文件
  167. filename: "LICENSE_CN.html",
  168. chunks: ["license"],
  169. minify: html_minify,
  170. }),
  171. new HtmlWebpackPlugin({
  172. inject:'body',
  173. template: path.resolve(__dirname, 'src', "html","LICENSE_EN.html"), //指定模板文件
  174. filename: "LICENSE_EN.html",
  175. chunks: ["license"],
  176. minify: html_minify,
  177. }),
  178. new HtmlWebpackPlugin({
  179. inject:'body',
  180. template: path.resolve(__dirname, 'src', "html","mitorg.html"), //指定模板文件
  181. filename: "mitorg.html",
  182. chunks: ["mitorg"],
  183. minify: html_minify,
  184. }),
  185. new HtmlWebpackPlugin({
  186. inject:'body',
  187. template: path.resolve(__dirname, 'src', "html","index.new.signal.html"), //指定模板文件
  188. filename: "index.new.signal.html",
  189. chunks: ["new"],
  190. minify: html_minify,
  191. }),
  192. new HtmlWebpackPlugin({
  193. inject:'body',
  194. template: path.resolve(__dirname, 'src', "html","index.new.html"), //指定模板文件
  195. filename: "index.new.html",
  196. chunks: ["new"],
  197. minify: html_minify,
  198. }),
  199. new MiniCssExtractPlugin({
  200. filename: '[name].[hash].bundle.css',
  201. chunkFilename: '[id].bundle.css',
  202. }),
  203. ],
  204. devServer: {
  205. static: {
  206. directory: path.join(__dirname, dist_name),
  207. },
  208. compress: true,
  209. port: 1001,
  210. open: true,
  211. hot: true,
  212. },
  213. };