Нема описа

webpack.config.prod.js 2.9KB

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