webpack_config_prod.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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: /\.(js|mjs|cjs)$/,
  126. exclude: /(node_modules|bower_components)/,
  127. use: {
  128. loader: 'babel-loader'
  129. }
  130. },
  131. {
  132. test: /\.(css|scss|sass)$/,
  133. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
  134. },
  135. {
  136. test: /\.(png|jpg|jpeg|svg|gif)$/i,
  137. use: [
  138. {
  139. loader: 'url-loader',
  140. options: {
  141. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  142. fallback: 'file-loader',
  143. outputPath: 'images', // 类似于 file-loader 的配置
  144. name: '[name].[fullhash].[ext]'
  145. }
  146. }
  147. ]
  148. },
  149. {
  150. test: /\.(mp4|m4v|avi|mov|qt|wmv|mkv|flv|webm|mpeg|mpg|3gp|3g2)$/i,
  151. use: [
  152. {
  153. loader: 'url-loader',
  154. options: {
  155. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  156. fallback: 'file-loader',
  157. outputPath: 'videos', // 类似于 file-loader 的配置
  158. name: '[name].[fullhash].[ext]'
  159. }
  160. }
  161. ]
  162. },
  163. {
  164. test: /\.(woff|woff2|eot|ttf|otf)$/i,
  165. use: [
  166. {
  167. loader: 'url-loader',
  168. options: {
  169. limit: 8192, // 8KB 以下的文件将被转换为 Data URL
  170. fallback: 'file-loader',
  171. outputPath: 'fonts', // 类似于 file-loader 的配置
  172. name: '[name].[fullhash].[ext]'
  173. }
  174. }
  175. ]
  176. },
  177. {
  178. test: /\.html$/i,
  179. loader: 'html-loader'
  180. }
  181. ]
  182. },
  183. plugins: [
  184. new CopyWebpackPlugin({
  185. patterns: [
  186. { from: 'public', to: './' },
  187. { from: './config.json', to: './SH_CONFIG.json' },
  188. { from: './LICENSE', to: './' },
  189. { from: './LICENSE_CN', to: './' }
  190. ]
  191. }),
  192. ...HTMMLPlugin,
  193. new HtmlWebpackPlugin({
  194. inject: 'body',
  195. template: path.resolve(__dirname, 'src', 'html', 'index.html'), //指定模板文件
  196. filename: 'index.html',
  197. chunks: ['common', 'index'],
  198. minify: html_minify,
  199. publicPath: './'
  200. }),
  201. new HtmlWebpackPlugin({
  202. inject: 'body',
  203. template: path.resolve(__dirname, 'src', 'html', 'LICENSE_US.html'), //指定模板文件
  204. filename: 'LICENSE_US.html',
  205. chunks: ['common', 'license'],
  206. minify: html_minify,
  207. publicPath: './'
  208. }),
  209. new HtmlWebpackPlugin({
  210. inject: 'body',
  211. template: path.resolve(__dirname, 'src/html/LICENSE_CN.html'), //指定模板文件
  212. filename: 'LICENSE_CN.html',
  213. chunks: ['common', 'license'],
  214. minify: html_minify,
  215. publicPath: './'
  216. }),
  217. new HtmlWebpackPlugin({
  218. inject: 'body',
  219. template: path.resolve(__dirname, 'src', 'html', 'mitorg.html'), //指定模板文件
  220. filename: 'mitorg.html',
  221. chunks: ['common', 'mitorg'],
  222. minify: html_minify,
  223. publicPath: './'
  224. }),
  225. new HtmlWebpackPlugin({
  226. inject: 'body',
  227. template: path.resolve(__dirname, 'src', 'html', 'index.new.signal.html'), //指定模板文件
  228. filename: 'index.new.signal.html',
  229. chunks: ['common', 'new', 'signal'], // 此signal要设置common
  230. minify: html_minify,
  231. publicPath: './'
  232. }),
  233. new HtmlWebpackPlugin({
  234. inject: 'body',
  235. template: path.resolve(__dirname, 'src', 'html', 'index.new.html'), //指定模板文件
  236. filename: 'index.new.html',
  237. chunks: ['common', 'new'],
  238. minify: html_minify,
  239. publicPath: './'
  240. }),
  241. new HtmlWebpackPlugin({
  242. inject: 'body',
  243. template: path.resolve(__dirname, 'src/html/error/4xx/404.signal.html'), //指定模板文件
  244. filename: '404.html',
  245. chunks: ['common', 'signal'], // 此signal要设置common
  246. minify: html_minify,
  247. publicPath: './'
  248. }),
  249. new MiniCssExtractPlugin({
  250. filename: 'style/[name].[hash].bundle.css',
  251. chunkFilename: 'css/[id].bundle.css'
  252. })
  253. ],
  254. devServer: {
  255. static: {
  256. directory: path.join(__dirname, dist_name)
  257. },
  258. compress: true,
  259. port: 1001,
  260. open: true,
  261. hot: true
  262. }
  263. }
  264. export default config