webpack.prod.config.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * @Author: Johnhong9527
  3. * @Date: 2020-05-12 16:35:36
  4. * @Last Modified by: Johnhong9527
  5. * @Last Modified time: 2020-05-12 16:35:44
  6. */
  7. const merge = require("webpack-merge");
  8. const common = require("./webpack.common.config.js");
  9. const HtmlWebpackPlugin = require("html-webpack-plugin");
  10. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  11. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  12. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  13. const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
  14. module.exports = merge(common, {
  15. mode: "production",
  16. module: {
  17. rules: [
  18. {
  19. test: /\.css$/,
  20. use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"]
  21. },
  22. {
  23. test: /\.less$/,
  24. use: [
  25. MiniCssExtractPlugin.loader,
  26. "css-loader",
  27. "less-loader",
  28. "postcss-loader"
  29. ]
  30. }
  31. // {
  32. // test: /\.(scss|sass)$/,
  33. // use: [
  34. // MiniCssExtractPlugin.loader,
  35. // 'css-loader',
  36. // 'postcss-loader',
  37. // 'sass-loader'
  38. // ]
  39. // },
  40. ]
  41. },
  42. plugins: [
  43. new HtmlWebpackPlugin({
  44. filename: "index.html",
  45. template: "public/index.html",
  46. inject: "body",
  47. minify: {
  48. removeComments: true,
  49. collapseWhitespace: true
  50. }
  51. }),
  52. new CleanWebpackPlugin(),
  53. new MiniCssExtractPlugin({
  54. filename: "css/[name].[hash].css",
  55. chunkFilename: "css/[id].[hash].css"
  56. })
  57. ],
  58. optimization: {
  59. minimizer: [
  60. new UglifyJsPlugin(),
  61. new OptimizeCssAssetsPlugin({
  62. assetNameRegExp: /\.css$/g,
  63. cssProcessor: require("cssnano"),
  64. cssProcessorPluginOptions: {
  65. preset: ["default", { discardComments: { removeAll: true } }]
  66. },
  67. canPrint: true
  68. })
  69. ],
  70. splitChunks: {
  71. chunks: "all",
  72. minSize: 30000,
  73. maxSize: 0,
  74. minChunks: 1,
  75. cacheGroups: {
  76. framework: {
  77. test: "framework",
  78. name: "framework",
  79. enforce: true
  80. },
  81. vendors: {
  82. priority: -10,
  83. test: /node_modules/,
  84. name: "vendor",
  85. enforce: true
  86. }
  87. }
  88. }
  89. }
  90. });