webpack.prod.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const path = require('path');
  2. const webpack = require('webpack');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const WebpackMerge = require('webpack-merge');
  6. const baseConfig = require('./webpack.base.js');
  7. const CopyPlugin = require('copy-webpack-plugin');
  8. const TerserPlugin = require('terser-webpack-plugin');
  9. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  10. module.exports = WebpackMerge.merge(baseConfig, {
  11. mode: 'production',
  12. //开发环境下默认启用cache,在内存中对已经构建的部分进行缓存
  13. //避免其他模块修改,但是该模块未修改时候,重新构建,能够更快的进行增量构建
  14. //属于空间换时间的做法
  15. cache: true,
  16. // 代码入口
  17. entry: {
  18. // 注册界面
  19. main: './src/main.js',
  20. },
  21. output: {
  22. path: path.resolve(__dirname, './dist'),
  23. publicPath: './',
  24. filename: './app-client-yidong-js-bundle/[name].[contenthash:8].js',
  25. chunkFilename: './app-client-yidong-js-chunk/[name].[contenthash:8].js',
  26. },
  27. optimization: {
  28. minimize: true, // 压缩 bundle
  29. minimizer: [new TerserPlugin({
  30. parallel: true, //使用多进程并发运行以提高构建速度 Boolean|Number 默认值: true
  31. terserOptions: {
  32. format: {
  33. comments: false,//删除注释
  34. },
  35. },
  36. extractComments: false, //不将注释提取到单独的文件中
  37. })],
  38. splitChunks: {
  39. // include all types of chunks
  40. chunks: 'all',
  41. name: false,
  42. minSize: 30000,
  43. maxSize: 500000,
  44. cacheGroups: {
  45. common: {
  46. test: /[\\/]node_modules[\\/]/,
  47. name: 'common',
  48. chunks: 'initial',
  49. priority: 2,
  50. minChunks: 2,
  51. },
  52. },
  53. },
  54. },
  55. //devtool: 'source-map', // 打包不需要 source-map
  56. plugins: (module.exports.plugins || []).concat([
  57. // 清除dist文件夹
  58. new CleanWebpackPlugin(),
  59. new HtmlWebpackPlugin({
  60. title: 'Prodog App',
  61. template: './public/index-release.html', // 源模板文件
  62. filename: './app.html', // 输出文件【注意:这里的根路径是module.exports.output.path】
  63. chunks: ['main'],
  64. }),
  65. // CSS 提取
  66. new MiniCssExtractPlugin({
  67. filename: './app-client-yidong-style/[name].[contenthash:8].css',
  68. }),
  69. ]),
  70. });