Keine Beschreibung

webpack.config.prod.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const CopyWebpackPlugin = require('copy-webpack-plugin');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  6. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  7. const baseConfig = require('./webpack.config.base');
  8. const { buildEnv } = baseConfig.extra;
  9. const config = {
  10. ...baseConfig,
  11. devtool: false,
  12. mode: 'production',
  13. output: {
  14. ...baseConfig.output,
  15. filename: '[name].js'
  16. },
  17. module: {
  18. rules: [
  19. ...baseConfig.module.rules.filter(
  20. rule => !['/\\.css$/', '/\\.less$/', '/\\.(scss|sass)$/'].includes(rule.test.toString())
  21. ),
  22. {
  23. test: /\.css$/,
  24. use: ['style-loader', 'css-loader', 'postcss-loader']
  25. },
  26. {
  27. test: /\.less$/,
  28. exclude: /node_modules/,
  29. use: [
  30. 'style-loader',
  31. baseConfig.extra.moduleCSSLoader,
  32. 'postcss-loader',
  33. baseConfig.extra.lessLoader
  34. ]
  35. },
  36. {
  37. test: /\.less$/,
  38. include: /node_modules/,
  39. use: ['style - loader', 'css-loader', 'postcss-loader', baseConfig.extra.lessLoader]
  40. }
  41. ]
  42. },
  43. plugins: [
  44. ...baseConfig.plugins,
  45. new webpack.DefinePlugin({
  46. isProd: JSON.stringify(true)
  47. }),
  48. // 使用 Prepack 优化包体大小
  49. // 暂时存在 Bug,等待修复
  50. // 使用前 21 - 425
  51. // 使用后 21 - 433
  52. // new PrepackWebpackPlugin({
  53. // mathRandomSeed: '0'
  54. // }),
  55. // 必须将 CopyWebpackPlugin 与 HtmlWebpackPlugin 添加到末尾
  56. new CopyWebpackPlugin([{ from: buildEnv.public, to: buildEnv.build }]),
  57. new HtmlWebpackPlugin({
  58. template: path.join(__dirname, '../template/template.ejs'),
  59. title: 'Webpack React',
  60. favicon: path.join(baseConfig.extra.buildEnv.public, 'favicon.ico'),
  61. manifest: path.join(buildEnv.public, 'manifest.json'),
  62. meta: [
  63. { name: 'robots', content: 'noindex,nofollow' },
  64. {
  65. name: 'viewport',
  66. content:
  67. 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no'
  68. }
  69. ],
  70. appMountIds: ['root'],
  71. inject: false,
  72. minify: {
  73. html5: true,
  74. useShortDoctype: true,
  75. collapseWhitespace: true,
  76. conservativeCollapse: true,
  77. preserveLineBreaks: true,
  78. removeComments: true,
  79. keepClosingSlash: true,
  80. removeRedundantAttributes: true,
  81. removeEmptyAttributes: true,
  82. removeStyleLinkTypeAttributes: true
  83. },
  84. mobile: true,
  85. scripts: ['./static.js']
  86. })
  87. ],
  88. optimization: {
  89. runtimeChunk: false,
  90. minimizer: [
  91. new UglifyJsPlugin({
  92. exclude: /.*ts-worker.*/
  93. }),
  94. new OptimizeCSSAssetsPlugin({})
  95. ]
  96. }
  97. };
  98. delete config.extra;
  99. module.exports = config;