craco.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const path = require('path');
  2. module.exports = {
  3. webpack: {
  4. configure: (webpackConfig, { env }) => {
  5. // 设置 publicPath
  6. // webpackConfig.output.publicPath = 'http://192.168.1.4:8081/';
  7. // These SVGs are consumed as URLs. Bypass SVGR so Babel does not turn large
  8. // flag/sprite sources into unnecessary React components first.
  9. const oneOf = webpackConfig.module.rules.find(rule => Array.isArray(rule.oneOf))?.oneOf;
  10. const svgRuleIndex = oneOf?.findIndex(rule => rule.test?.test?.("icon.svg"));
  11. if (svgRuleIndex >= 0) {
  12. oneOf.splice(svgRuleIndex, 0, {
  13. test: /[\\/]assets[\\/](?:i18n[\\/][^\\/]+|symbol(?:-defs|-mobile))\.svg$/i,
  14. type: "asset/resource",
  15. });
  16. }
  17. // CRA development builds use a fixed "static/js/bundle.js" filename, so
  18. // splitting initial chunks there makes multiple chunks target the same file.
  19. if (env === "production") {
  20. const initialChunkMaxSize = 1000000;
  21. const currentCacheGroups = webpackConfig.optimization.splitChunks?.cacheGroups || {};
  22. webpackConfig.optimization.splitChunks = {
  23. ...webpackConfig.optimization.splitChunks,
  24. cacheGroups: {
  25. ...currentCacheGroups,
  26. initialVendors: {
  27. test: /[\\/]node_modules[\\/]/,
  28. chunks: "initial",
  29. enforce: true,
  30. maxSize: initialChunkMaxSize,
  31. minSize: 0,
  32. priority: 30,
  33. reuseExistingChunk: true,
  34. },
  35. initialApp: {
  36. test: module => module.resource?.startsWith(path.resolve(__dirname, "src")),
  37. chunks: "initial",
  38. enforce: true,
  39. maxSize: initialChunkMaxSize,
  40. minSize: 0,
  41. priority: 20,
  42. reuseExistingChunk: true,
  43. },
  44. },
  45. };
  46. }
  47. // 返回修改后的配置
  48. return webpackConfig;
  49. },
  50. alias: {
  51. '@components': path.resolve(__dirname, 'src/components'),
  52. '@ui': path.resolve(__dirname, 'src/ui'),
  53. '@pages': path.resolve(__dirname, 'src/pages'),
  54. '@assets': path.resolve(__dirname, 'src/assets'),
  55. '@styles': path.resolve(__dirname, 'src/styles'),
  56. '@db': path.resolve(__dirname, 'src/db'),
  57. '@hooks': path.resolve(__dirname, 'src/hooks'),
  58. '@layout': path.resolve(__dirname, 'src/layout'),
  59. '@fonts': path.resolve(__dirname, 'src/fonts'),
  60. '@utils': path.resolve(__dirname, 'src/utils'),
  61. '@widgets': path.resolve(__dirname, 'src/widgets'),
  62. '@contexts': path.resolve(__dirname, 'src/contexts'),
  63. '@constants': path.resolve(__dirname, 'src/constants'),
  64. '@features': path.resolve(__dirname, 'src/features'),
  65. '@api':path.resolve(__dirname,'src/api'),
  66. },
  67. }
  68. };