webpack_config_dev.js 5.6 KB

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