craco.config.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const path = require('path');
  2. module.exports = {
  3. webpack: {
  4. configure: (webpackConfig) => {
  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. // Split existing initial modules into deterministic cacheable files. They are
  18. // still loaded before the entry point, so runtime behavior remains unchanged.
  19. const initialChunkMaxSize = 1000000;
  20. const currentCacheGroups = webpackConfig.optimization.splitChunks?.cacheGroups || {};
  21. webpackConfig.optimization.splitChunks = {
  22. ...webpackConfig.optimization.splitChunks,
  23. cacheGroups: {
  24. ...currentCacheGroups,
  25. initialVendors: {
  26. test: /[\\/]node_modules[\\/]/,
  27. chunks: "initial",
  28. enforce: true,
  29. maxSize: initialChunkMaxSize,
  30. minSize: 0,
  31. priority: 30,
  32. reuseExistingChunk: true,
  33. },
  34. initialApp: {
  35. test: module => module.resource?.startsWith(path.resolve(__dirname, "src")),
  36. chunks: "initial",
  37. enforce: true,
  38. maxSize: initialChunkMaxSize,
  39. minSize: 0,
  40. priority: 20,
  41. reuseExistingChunk: true,
  42. },
  43. },
  44. };
  45. // 返回修改后的配置
  46. return webpackConfig;
  47. },
  48. alias: {
  49. '@components': path.resolve(__dirname, 'src/components'),
  50. '@ui': path.resolve(__dirname, 'src/ui'),
  51. '@pages': path.resolve(__dirname, 'src/pages'),
  52. '@assets': path.resolve(__dirname, 'src/assets'),
  53. '@styles': path.resolve(__dirname, 'src/styles'),
  54. '@db': path.resolve(__dirname, 'src/db'),
  55. '@hooks': path.resolve(__dirname, 'src/hooks'),
  56. '@layout': path.resolve(__dirname, 'src/layout'),
  57. '@fonts': path.resolve(__dirname, 'src/fonts'),
  58. '@utils': path.resolve(__dirname, 'src/utils'),
  59. '@widgets': path.resolve(__dirname, 'src/widgets'),
  60. '@contexts': path.resolve(__dirname, 'src/contexts'),
  61. '@constants': path.resolve(__dirname, 'src/constants'),
  62. '@features': path.resolve(__dirname, 'src/features'),
  63. '@api':path.resolve(__dirname,'src/api'),
  64. },
  65. }
  66. };