webpack.prod.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { name } = require('./package');
  8. module.exports = WebpackMerge.merge(baseConfig, {
  9. mode: 'production',
  10. //开发环境下默认启用cache,在内存中对已经构建的部分进行缓存
  11. //避免其他模块修改,但是该模块未修改时候,重新构建,能够更快的进行增量构建
  12. //属于空间换时间的做法
  13. cache: true,
  14. // 代码入口
  15. entry: {
  16. // 注册界面
  17. main: './src/main.js',
  18. },
  19. output: {
  20. path: path.resolve(__dirname, './dist/client-trace-v3'),
  21. publicPath: './',
  22. library: `${name}`,
  23. libraryTarget: 'umd', // 把微应用打包成 umd 库格式
  24. filename: './asset-js-bundle/[name].[contenthash:8].js',
  25. chunkFilename: './asset-js-chunk/[name].[contenthash:8].js',
  26. },
  27. optimization: {
  28. minimize: true, // 压缩 bundle
  29. splitChunks: {
  30. // include all types of chunks
  31. chunks: 'all',
  32. // name: false,
  33. minSize: 30000,
  34. maxSize: 500000,
  35. cacheGroups: {
  36. common: {
  37. test: /[\\/]node_modules[\\/]/,
  38. name: 'common',
  39. chunks: 'initial',
  40. priority: 2,
  41. minChunks: 2,
  42. },
  43. }
  44. },
  45. },
  46. //devtool: 'source-map', // 打包不需要 source-map
  47. plugins: (module.exports.plugins || []).concat([
  48. new HtmlWebpackPlugin({
  49. title: 'Prodog',
  50. template: './public/index.html', // 源模板文件
  51. filename: './index.html', // 输出文件【注意:这里的根路径是module.exports.output.path】
  52. chunks: ['main']
  53. }),
  54. new webpack.LoaderOptionsPlugin({
  55. minimize: true
  56. }),
  57. // CSS 提取
  58. new MiniCssExtractPlugin({
  59. filename: "./asset-style/[name].[contenthash:8].css"
  60. }),
  61. ])
  62. })