Keine Beschreibung

webpack.config.prod.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: ['style-loader', 'css-loader', 'postcss-loader', baseConfig.extra.lessLoader],
  30. },
  31. {
  32. test: /\.less$/,
  33. include: /node_modules/,
  34. use: ['style-loader', 'css-loader', 'postcss-loader', baseConfig.extra.lessLoader],
  35. },
  36. ],
  37. },
  38. plugins: [
  39. ...baseConfig.plugins,
  40. new webpack.DefinePlugin({
  41. isProd: JSON.stringify(true),
  42. }),
  43. // 使用 Prepack 优化包体大小
  44. // 暂时存在 Bug,等待修复
  45. // 使用前 21 - 425
  46. // 使用后 21 - 433
  47. // new PrepackWebpackPlugin({
  48. // mathRandomSeed: '0'
  49. // }),
  50. // 必须将 CopyWebpackPlugin 与 HtmlWebpackPlugin 添加到末尾
  51. new CopyWebpackPlugin({ patterns: [{ from: buildEnv.public, to: buildEnv.build }] }),
  52. new HtmlWebpackPlugin({
  53. template: path.join(__dirname, '../template/template.ejs'),
  54. title: 'Webpack React',
  55. favicon: path.join(baseConfig.extra.buildEnv.public, 'favicon.ico'),
  56. manifest: path.join(buildEnv.public, 'manifest.json'),
  57. meta: [
  58. { name: 'robots', content: 'noindex,nofollow' },
  59. {
  60. name: 'viewport',
  61. content:
  62. 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no',
  63. },
  64. ],
  65. appMountIds: ['root'],
  66. inject: false,
  67. minify: {
  68. html5: true,
  69. useShortDoctype: true,
  70. collapseWhitespace: true,
  71. conservativeCollapse: true,
  72. preserveLineBreaks: true,
  73. removeComments: true,
  74. keepClosingSlash: true,
  75. removeRedundantAttributes: true,
  76. removeEmptyAttributes: true,
  77. removeStyleLinkTypeAttributes: true,
  78. },
  79. mobile: true,
  80. scripts: ['./static.js'],
  81. }),
  82. ],
  83. optimization: {
  84. runtimeChunk: false,
  85. minimizer: [
  86. new UglifyJsPlugin({
  87. exclude: /.*ts-worker.*/,
  88. }),
  89. new OptimizeCSSAssetsPlugin({}),
  90. ],
  91. },
  92. };
  93. delete config.extra;
  94. module.exports = config;