plugin.config.js 1.7 KB

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