webpack_config_dev.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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].[fullhash].bundle.js', //打包后输出文件的文件名
  87. chunkFilename: '[name].bundle.js',
  88. clean: true,
  89. charset: true,
  90. publicPath: "/"
  91. },
  92. resolve: {
  93. alias: {
  94. "@": path.join(__dirname, "src")
  95. }
  96. },
  97. module: {
  98. rules: [
  99. {
  100. test: /\.(css|scss|sass)$/,
  101. use: [
  102. MiniCssExtractPlugin.loader,
  103. 'css-loader',
  104. 'postcss-loader',
  105. 'sass-loader',
  106. ],
  107. },
  108. {
  109. test:/\.(png|jpg|jpeg|svg|gif)$/i,
  110. use: [
  111. {
  112. loader: 'url-loader',
  113. options: {
  114. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  115. fallback: 'file-loader',
  116. outputPath: 'images', // 类似于 file-loader 的配置
  117. name: '[name].[fullhash].[ext]',
  118. },
  119. },
  120. ],
  121. },
  122. {
  123. test:/\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  124. use: [
  125. {
  126. loader: 'url-loader',
  127. options: {
  128. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  129. fallback: 'file-loader',
  130. outputPath: 'videos', // 类似于 file-loader 的配置
  131. name: '[name].[fullhash].[ext]',
  132. },
  133. },
  134. ],
  135. },
  136. {
  137. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  138. use: [
  139. {
  140. loader: 'url-loader',
  141. options: {
  142. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  143. fallback: 'file-loader',
  144. outputPath: 'fonts', // 类似于 file-loader 的配置
  145. name: '[name].[fullhash].[ext]',
  146. },
  147. },
  148. ],
  149. },
  150. {
  151. test: /\.html$/i,
  152. loader: "html-loader",
  153. },
  154. {
  155. test: require.resolve("jquery"),
  156. loader: "expose-loader",
  157. options: {
  158. exposes: ["$", "jQuery"],
  159. },
  160. },
  161. ],
  162. },
  163. plugins: [
  164. new CopyWebpackPlugin({
  165. patterns: [
  166. { from: 'public', to: './' },
  167. { from: './config.json', to: './SH_CONFIG.json' },
  168. { from: './LICENSE', to: './' },
  169. { from: './LICENSE_CN', to: './' },
  170. ],
  171. }),
  172. ...HTMMLPlugin,
  173. new HtmlWebpackPlugin({
  174. inject:'body',
  175. template: path.resolve(__dirname, 'src', "html", "index.html"), //指定模板文件
  176. filename: "index.html",
  177. chunks: ["common", "index"],
  178. publicPath: "./",
  179. }),
  180. new HtmlWebpackPlugin({
  181. inject:'body',
  182. template: path.resolve(__dirname, 'src/html/LICENSE_US.html'), //指定模板文件
  183. filename: "LICENSE_US.html",
  184. chunks: ["common", "license"],
  185. publicPath: "./",
  186. }),
  187. new HtmlWebpackPlugin({
  188. inject:'body',
  189. template: path.resolve(__dirname, 'src/html/LICENSE_CN.html'), //指定模板文件
  190. filename: "LICENSE_CN.html",
  191. chunks: ["common", "license"],
  192. publicPath: "./",
  193. }),
  194. new HtmlWebpackPlugin({
  195. inject:'body',
  196. template: path.resolve(__dirname, 'src/html/mitorg.html'), //指定模板文件
  197. filename: "mitorg.html",
  198. chunks: ["common", "mitorg"],
  199. publicPath: "./",
  200. }),
  201. new HtmlWebpackPlugin({
  202. inject:'body',
  203. template: path.resolve(__dirname, 'src/html/index.new.signal.html'), //指定模板文件
  204. filename: "index.new.signal.html",
  205. chunks: ["common", "new", "signal"], // 此signal要设置common
  206. publicPath: "./",
  207. }),
  208. new HtmlWebpackPlugin({
  209. inject:'body',
  210. template: path.resolve(__dirname, 'src/html/index.new.html'), //指定模板文件
  211. filename: "index.new.html",
  212. chunks: ["common", "new"],
  213. publicPath: "./",
  214. }),
  215. new HtmlWebpackPlugin({
  216. inject:'body',
  217. template: path.resolve(__dirname, 'src/html/error/4xx/404.signal.html'), //指定模板文件
  218. filename: "404.html",
  219. chunks: ["common", "signal"], // 该signal要设置common
  220. publicPath: "./",
  221. }),
  222. new MiniCssExtractPlugin({
  223. filename: 'style/[name].[fullhash].bundle.css',
  224. chunkFilename: 'css/[id].bundle.css',
  225. }),
  226. ],
  227. devServer: {
  228. static: {
  229. directory: path.join(__dirname, dist_name),
  230. },
  231. compress: true,
  232. port: 1001,
  233. open: true,
  234. hot: true,
  235. },
  236. };