webpack_config_prod.js 7.9 KB

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