webpack_config_prod.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import path from 'path'
  2. import HtmlWebpackPlugin from 'html-webpack-plugin'
  3. import MiniCssExtractPlugin from 'mini-css-extract-plugin'
  4. import CopyWebpackPlugin from 'copy-webpack-plugin'
  5. import filetool from 'huan-file-tool'
  6. import { fileURLToPath } from 'url'
  7. const __filename = fileURLToPath(import.meta.url)
  8. const __dirname = path.dirname(__filename)
  9. const mode = 'production'
  10. const dist_name = 'dist-prod'
  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 HTMMLPlugin = []
  24. const { localPathResult: AllHTMLLocalFile4xx } = filetool.filewaalk.getAllFilePaths(
  25. path.resolve(__dirname, 'src/html/error/4xx')
  26. )
  27. AllHTMLLocalFile4xx.forEach((filePath) => {
  28. if (!filePath.endsWith('.html')) {
  29. return
  30. }
  31. if (filePath.includes('signal.html')) {
  32. HTMMLPlugin.push(
  33. new HtmlWebpackPlugin({
  34. inject: 'body',
  35. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  36. filename: path.join('error/4xx', filePath),
  37. chunks: ['signal'],
  38. publicPath: '../../'
  39. })
  40. )
  41. return
  42. }
  43. if (filePath.includes('400.html')) {
  44. HTMMLPlugin.push(
  45. new HtmlWebpackPlugin({
  46. inject: 'body',
  47. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  48. filename: path.join('error/4xx', filePath),
  49. chunks: ['common', 'err404'],
  50. publicPath: '../../'
  51. })
  52. )
  53. return
  54. }
  55. HTMMLPlugin.push(
  56. new HtmlWebpackPlugin({
  57. inject: 'body',
  58. template: path.resolve(__dirname, 'src/html/error/4xx', filePath), //指定模板文件
  59. filename: path.join('error/4xx', filePath),
  60. chunks: ['common', 'err4xx'],
  61. publicPath: '../../'
  62. })
  63. )
  64. })
  65. const { localPathResult: AllHTMLLocalFile5xx } = filetool.filewaalk.getAllFilePaths(
  66. path.resolve(__dirname, 'src/html/error/5xx')
  67. )
  68. AllHTMLLocalFile5xx.forEach((filePath) => {
  69. if (!filePath.endsWith('.html')) {
  70. return
  71. }
  72. if (filePath.includes('signal.html')) {
  73. HTMMLPlugin.push(
  74. new HtmlWebpackPlugin({
  75. inject: 'body',
  76. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  77. filename: path.join('error/5xx', filePath),
  78. chunks: ['signal'],
  79. publicPath: '../../'
  80. })
  81. )
  82. return
  83. }
  84. HTMMLPlugin.push(
  85. new HtmlWebpackPlugin({
  86. inject: 'body',
  87. template: path.resolve(__dirname, 'src/html/error/5xx', filePath), //指定模板文件
  88. filename: path.join('error/5xx', filePath),
  89. chunks: ['common', 'err5xx'],
  90. publicPath: '../../'
  91. })
  92. )
  93. })
  94. const config = {
  95. mode: mode,
  96. context: __dirname,
  97. performance: {
  98. hints: 'warning', // 或者 'error',取决于你希望如何处理超出限制的情况
  99. maxAssetSize: 5000000, // 设置单个资源的最大尺寸,例如5MB
  100. maxEntrypointSize: 10000000 // 设置入口起点的最大尺寸,例如10MB
  101. },
  102. entry: {
  103. common: path.resolve(__dirname, 'src/common.js'),
  104. index: path.resolve(__dirname, 'src/index.js'),
  105. signal: path.resolve(__dirname, 'src/signal.js'),
  106. new: path.resolve(__dirname, 'src/new.js'),
  107. license: path.resolve(__dirname, 'src/license.js'),
  108. mitorg: path.resolve(__dirname, 'src/mitorg.js'),
  109. err4xx: path.resolve(__dirname, 'src/4xx.js'),
  110. err404: path.resolve(__dirname, 'src/404.js'),
  111. err5xx: path.resolve(__dirname, 'src/5xx.js')
  112. },
  113. output: {
  114. path: path.resolve(__dirname, dist_name), //打包后的文件存放的地方
  115. filename: 'js/[name].[fullhash].bundle.js', //打包后输出文件的文件名
  116. chunkFilename: '[name].bundle.js',
  117. clean: true,
  118. charset: true,
  119. publicPath: '/'
  120. },
  121. resolve: {
  122. alias: {
  123. '@': path.join(__dirname, 'src')
  124. }
  125. },
  126. module: {
  127. rules: [
  128. {
  129. test: /\.(js|mjs|cjs)$/,
  130. exclude: /(node_modules|bower_components)/,
  131. use: {
  132. loader: 'babel-loader'
  133. }
  134. },
  135. {
  136. test: /\.(css|scss|sass)$/,
  137. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
  138. },
  139. {
  140. test: /\.(png|jpg|jpeg|svg|gif)$/i,
  141. use: [
  142. {
  143. loader: 'url-loader',
  144. options: {
  145. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  146. fallback: 'file-loader',
  147. outputPath: 'images', // 类似于 file-loader 的配置
  148. name: '[name].[fullhash].[ext]'
  149. }
  150. }
  151. ]
  152. },
  153. {
  154. test: /\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  155. use: [
  156. {
  157. loader: 'url-loader',
  158. options: {
  159. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  160. fallback: 'file-loader',
  161. outputPath: 'videos', // 类似于 file-loader 的配置
  162. name: '[name].[fullhash].[ext]'
  163. }
  164. }
  165. ]
  166. },
  167. {
  168. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  169. use: [
  170. {
  171. loader: 'url-loader',
  172. options: {
  173. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  174. fallback: 'file-loader',
  175. outputPath: 'fonts', // 类似于 file-loader 的配置
  176. name: '[name].[fullhash].[ext]'
  177. }
  178. }
  179. ]
  180. },
  181. {
  182. test: /\.html$/i,
  183. loader: 'html-loader'
  184. }
  185. ]
  186. },
  187. plugins: [
  188. new CopyWebpackPlugin({
  189. patterns: [
  190. { from: 'public', to: './' },
  191. { from: './config.json', to: './SH_CONFIG.json' },
  192. { from: './LICENSE', to: './' },
  193. { from: './LICENSE_CN', to: './' }
  194. ]
  195. }),
  196. ...HTMMLPlugin,
  197. new HtmlWebpackPlugin({
  198. inject: 'body',
  199. template: path.resolve(__dirname, 'src', 'html', 'index.html'), //指定模板文件
  200. filename: 'index.html',
  201. chunks: ['common', 'index'],
  202. minify: html_minify,
  203. publicPath: './'
  204. }),
  205. new HtmlWebpackPlugin({
  206. inject: 'body',
  207. template: path.resolve(__dirname, 'src', 'html', 'LICENSE_US.html'), //指定模板文件
  208. filename: 'LICENSE_US.html',
  209. chunks: ['common', 'license'],
  210. minify: html_minify,
  211. publicPath: './'
  212. }),
  213. new HtmlWebpackPlugin({
  214. inject: 'body',
  215. template: path.resolve(__dirname, 'src/html/LICENSE_CN.html'), //指定模板文件
  216. filename: 'LICENSE_CN.html',
  217. chunks: ['common', 'license'],
  218. minify: html_minify,
  219. publicPath: './'
  220. }),
  221. new HtmlWebpackPlugin({
  222. inject: 'body',
  223. template: path.resolve(__dirname, 'src', 'html', 'mitorg.html'), //指定模板文件
  224. filename: 'mitorg.html',
  225. chunks: ['common', 'mitorg'],
  226. minify: html_minify,
  227. publicPath: './'
  228. }),
  229. new HtmlWebpackPlugin({
  230. inject: 'body',
  231. template: path.resolve(__dirname, 'src', 'html', 'index.new.signal.html'), //指定模板文件
  232. filename: 'index.new.signal.html',
  233. chunks: ['common', 'new', 'signal'], // 此signal要设置common
  234. minify: html_minify,
  235. publicPath: './'
  236. }),
  237. new HtmlWebpackPlugin({
  238. inject: 'body',
  239. template: path.resolve(__dirname, 'src', 'html', 'index.new.html'), //指定模板文件
  240. filename: 'index.new.html',
  241. chunks: ['common', 'new'],
  242. minify: html_minify,
  243. publicPath: './'
  244. }),
  245. new HtmlWebpackPlugin({
  246. inject: 'body',
  247. template: path.resolve(__dirname, 'src/html/error/4xx/404.signal.html'), //指定模板文件
  248. filename: '404.html',
  249. chunks: ['common', 'signal'], // 此signal要设置common
  250. minify: html_minify,
  251. publicPath: './'
  252. }),
  253. new MiniCssExtractPlugin({
  254. filename: 'style/[name].[hash].bundle.css',
  255. chunkFilename: 'css/[id].bundle.css'
  256. })
  257. ],
  258. devServer: {
  259. static: {
  260. directory: path.join(__dirname, dist_name)
  261. },
  262. compress: true,
  263. port: 1001,
  264. open: true,
  265. hot: true
  266. }
  267. }
  268. export default config