plugin.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import path from 'path';
  2. import * as IWebpackChainConfig from 'webpack-chain';
  3. function getModulePackageName(module: { context: string }) {
  4. if (!module.context) return null;
  5. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  6. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  7. return null;
  8. }
  9. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  10. const [moduleDirName] = moduleRelativePath.split(path.sep);
  11. let packageName: string | null = moduleDirName;
  12. // handle tree shaking
  13. if (packageName && packageName.match('^_')) {
  14. // eslint-disable-next-line prefer-destructuring
  15. packageName = packageName.match(/^_(@?[^@]+)/)![1];
  16. }
  17. return packageName;
  18. }
  19. const webpackPlugin = (config: IWebpackChainConfig) => {
  20. config.merge({
  21. optimization: {
  22. minimize: true,
  23. splitChunks: {
  24. chunks: 'async',
  25. minSize: 30000,
  26. minChunks: 2,
  27. automaticNameDelimiter: '.',
  28. cacheGroups: {
  29. vendor: {
  30. name: 'vendors',
  31. test: /^.*node_modules[\\/](?!ag-grid-|lodash|wangeditor|react-virtualized|rc-select|rc-drawer|rc-time-picker|rc-tree|rc-table|rc-calendar|antd).*$/,
  32. chunks: "all",
  33. priority: 10,
  34. },
  35. virtualized: {
  36. name: "virtualized",
  37. test: /[\\/]node_modules[\\/]react-virtualized/,
  38. chunks: "all",
  39. priority: 10
  40. },
  41. rcselect: {
  42. name: "rc-select",
  43. test: /[\\/]node_modules[\\/]rc-select/,
  44. chunks: "all",
  45. priority: 10
  46. },
  47. rcdrawer: {
  48. name: "rcdrawer",
  49. test: /[\\/]node_modules[\\/]rc-drawer/,
  50. chunks: "all",
  51. priority: 10
  52. },
  53. rctimepicker: {
  54. name: "rctimepicker",
  55. test: /[\\/]node_modules[\\/]rc-time-picker/,
  56. chunks: "all",
  57. priority: 10
  58. },
  59. ag: {
  60. name: "ag",
  61. test: /[\\/]node_modules[\\/]ag-grid-/,
  62. chunks: "all",
  63. priority: 10
  64. },
  65. antd: {
  66. name: "antd",
  67. test: /[\\/]node_modules[\\/]antd[\\/]/,
  68. chunks: "all",
  69. priority: 9
  70. },
  71. rctree: {
  72. name: "rctree",
  73. test: /[\\/]node_modules[\\/]rc-tree/,
  74. chunks: "all",
  75. priority: -1
  76. },
  77. rccalendar: {
  78. name: "rccalendar",
  79. test: /[\\/]node_modules[\\/]rc-calendar[\\/]/,
  80. chunks: "all",
  81. priority: -1
  82. },
  83. rctable: {
  84. name: "rctable",
  85. test: /[\\/]node_modules[\\/]rc-table[\\/]es[\\/]/,
  86. chunks: "all",
  87. priority: -1
  88. },
  89. wang: {
  90. name: "wang",
  91. test: /[\\/]node_modules[\\/]wangeditor[\\/]/,
  92. chunks: "all",
  93. priority: -1
  94. },
  95. lodash: {
  96. name: "lodash",
  97. test: /[\\/]node_modules[\\/]lodash[\\/]/,
  98. chunks: "all",
  99. priority: -2
  100. },
  101. bizcharts: {
  102. name: "bizcharts",
  103. test: /[\\/]node_modules[\\/]bizcharts[\\/]/,
  104. chunks: "all",
  105. priority: 10
  106. },
  107. xlsx: {
  108. name: "xlsx",
  109. test: /[\\/]node_modules[\\/]xlsx[\\/]/,
  110. chunks: "async",
  111. priority: 10
  112. }
  113. }
  114. }
  115. }
  116. });
  117. //过滤掉momnet的那些不使用的国际化文件
  118. config.plugin("replace").use(require("webpack").ContextReplacementPlugin).tap(() => {
  119. return [/moment[/\\]locale$/, /zh-cn/];
  120. });
  121. };
  122. export default webpackPlugin;