You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

10 months ago
  1. //用于修改webpack默认配置
  2. const { override, addWebpackAlias, fixBabelImports, addLessLoader, addBabelPlugin } = require('customize-cra')
  3. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  4. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
  5. .BundleAnalyzerPlugin
  6. const webpack = require('webpack')
  7. const path = require('path')
  8. const darkThemeVars = require('antd/dist/dark-theme');
  9. // 分析打包大小
  10. const addAnalyze = () => (config) => {
  11. let plugins = [new BundleAnalyzerPlugin({ analyzerPort: 7777 })]
  12. config.plugins = [...config.plugins, ...plugins]
  13. return config
  14. }
  15. // 打包体积优化
  16. const addOptimization = () => (config) => {
  17. if (process.env.NODE_ENV === 'production') {
  18. config.optimization = {
  19. splitChunks: {
  20. chunks: 'all',
  21. minSize: 30000,
  22. maxSize: 0,
  23. minChunks: 1,
  24. maxAsyncRequests: 5,
  25. maxInitialRequests: 3,
  26. automaticNameDelimiter: '~',
  27. name: true,
  28. cacheGroups: {
  29. vendors: {
  30. test: /[\\/]node_modules[\\/]/,
  31. priority: 10
  32. },
  33. default: {
  34. minChunks: 2,
  35. priority: -10,
  36. reuseExistingChunk: true
  37. }
  38. }
  39. }
  40. }
  41. // 关闭sourceMap
  42. config.devtool = false
  43. // 添加js打包gzip配置
  44. config.plugins.push(
  45. new CompressionWebpackPlugin({
  46. test: /\.js$|\.css$/,
  47. threshold: 1024
  48. }),
  49. new webpack.optimize.AggressiveMergingPlugin(), //合并块
  50. new webpack.optimize.ModuleConcatenationPlugin()
  51. )
  52. }
  53. return config
  54. }
  55. module.exports = override(
  56. // addAnalyze(),
  57. // 配置路径别名
  58. addWebpackAlias({
  59. '@': path.resolve('src')
  60. }),
  61. addOptimization(),
  62. // 针对antd 实现按需打包:根据import来打包 (使用babel-plugin-import)
  63. fixBabelImports('import', {
  64. libraryName: 'antd',
  65. libraryDirectory: 'es',
  66. style: true//自动打包相关的样式 默认为 style:'css'
  67. }),
  68. // 使用less-loader对源码重的less的变量进行重新制定,设置antd自定义主题
  69. addLessLoader({
  70. javascriptEnabled: true,
  71. modifyVars: {
  72. 'hack': `true;@import "${require.resolve('antd/lib/style/color/colorPalette.less')}";`,
  73. ...darkThemeVars,
  74. '@primary-color': '#6e41ff',
  75. },
  76. localIdentName: '[local]--[hash:base64:5]' // use less-modules
  77. })
  78. )