config-overrides.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const {
  2. override,
  3. fixBabelImports,
  4. addLessLoader,
  5. addWebpackAlias,
  6. } = require(
  7. 'customize-cra')
  8. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  9. const path = require('path')
  10. //执行npm run build不产生map文件
  11. process.env.GENERATE_SOURCEMAP = 'false'
  12. const isEnvProduction = process.env.NODE_ENV === 'production'
  13. const addCompression = () => config => {
  14. if (isEnvProduction) {
  15. config.plugins.push(
  16. // gzip压缩
  17. new CompressionWebpackPlugin({
  18. test: /\.(css|less|js)$/,
  19. // 只处理比1kb大的资源
  20. threshold: 1024,
  21. // 只处理压缩率低于90%的文件
  22. minRatio: 0.9,
  23. }),
  24. )
  25. }
  26. return config
  27. }
  28. /**
  29. * 解决react router 路由刷新404问题
  30. * @returns {function(*): {historyApiFallback: boolean}}
  31. */
  32. const devServerConfig = () => config => {
  33. return {
  34. ...config,
  35. historyApiFallback: true,
  36. }
  37. }
  38. module.exports = {
  39. webpack: override(
  40. fixBabelImports('import-antd', {
  41. libraryName: 'antd',
  42. libraryDirectory: 'es',
  43. style: true,
  44. }),
  45. fixBabelImports('import-ant-mobile', {
  46. libraryName: 'antd-mobile',
  47. libraryDirectory: 'lib',
  48. style: true,
  49. }),
  50. addLessLoader({
  51. lessOptions: {
  52. javascriptEnabled: true,
  53. modifyVars: { '@primary-color': '#246aa5' },
  54. },
  55. }),
  56. addCompression(),
  57. addWebpackAlias({
  58. '@': path.join(__dirname, 'src'),
  59. }),
  60. (config) => {
  61. //修改、添加loader 配置 :
  62. // 所有的loaders规则是在config.module.rules(数组)的第二项
  63. // 即:config.module.rules[2].oneof (如果不是,具体可以打印 一下是第几项目)
  64. // 修改 less 配置 ,规则 loader 在第7项(具体可以打印配置)
  65. const loaders = config.module.rules
  66. .find(rule => Array.isArray(rule.oneOf)).oneOf
  67. console.log(loaders)
  68. loaders[7].use.push({
  69. loader: 'style-resources-loader',
  70. options: {
  71. patterns: path.resolve(__dirname, 'src/configs/theme.config.less'),//全局引入公共的less文件
  72. },
  73. })
  74. return config
  75. },
  76. ),
  77. // devServer: overrideDevServer(devServerConfig()),
  78. }