plugin.config.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import path from 'path';
  2. function getModulePackageName(module: { context: string }) {
  3. if (!module.context) return null;
  4. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  5. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  6. return null;
  7. }
  8. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  9. const [moduleDirName] = moduleRelativePath.split(path.sep);
  10. let packageName: string | null = moduleDirName;
  11. // handle tree shaking
  12. if (packageName && packageName.match('^_')) {
  13. // eslint-disable-next-line prefer-destructuring
  14. packageName = packageName.match(/^_(@?[^@]+)/)![1];
  15. }
  16. return packageName;
  17. }
  18. export default (config: any) => {
  19. // optimize chunks
  20. config.optimization
  21. // share the same chunks across different modules
  22. .runtimeChunk(false)
  23. .splitChunks({
  24. chunks: 'async',
  25. name: 'vendors',
  26. maxInitialRequests: Infinity,
  27. minSize: 0,
  28. cacheGroups: {
  29. vendors: {
  30. test: (module: { context: string }) => {
  31. const packageName = getModulePackageName(module) || '';
  32. if (packageName) {
  33. return [
  34. 'bizcharts',
  35. 'gg-editor',
  36. 'g6',
  37. '@antv',
  38. 'gg-editor-core',
  39. 'bizcharts-plugin-slider',
  40. ].includes(packageName);
  41. }
  42. return false;
  43. },
  44. name(module: { context: string }) {
  45. const packageName = getModulePackageName(module);
  46. if (packageName) {
  47. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  48. return 'viz'; // visualization package
  49. }
  50. }
  51. return 'misc';
  52. },
  53. },
  54. },
  55. });
  56. };