webpack_config_dev.js 6.6 KB

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