plugin.config.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // optimize chunks
  21. config.optimization
  22. // share the same chunks across different modules
  23. .runtimeChunk(false)
  24. .splitChunks({
  25. chunks: 'async',
  26. name: 'vendors',
  27. maxInitialRequests: Infinity,
  28. minSize: 0,
  29. cacheGroups: {
  30. vendors: {
  31. test: (module: { context: string }) => {
  32. const packageName = getModulePackageName(module) || '';
  33. if (packageName) {
  34. return [
  35. 'bizcharts',
  36. 'gg-editor',
  37. 'g6',
  38. '@antv',
  39. 'l7',
  40. 'gg-editor-core',
  41. 'bizcharts-plugin-slider',
  42. ].includes(packageName);
  43. }
  44. return false;
  45. },
  46. name(module: { context: string }) {
  47. const packageName = getModulePackageName(module);
  48. if (packageName) {
  49. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  50. return 'viz'; // visualization package
  51. }
  52. }
  53. return 'misc';
  54. },
  55. },
  56. },
  57. });
  58. };
  59. export default webpackPlugin;