plugin.config.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: 30000,
  29. minChunks: 3,
  30. automaticNameDelimiter: '.',
  31. cacheGroups: {
  32. vendors: {
  33. test: (module: { context: string }) => {
  34. const packageName = getModulePackageName(module) || '';
  35. if (packageName) {
  36. return [
  37. 'bizcharts',
  38. 'gg-editor',
  39. 'g6',
  40. '@antv',
  41. 'l7',
  42. 'gg-editor-core',
  43. 'bizcharts-plugin-slider',
  44. ].includes(packageName);
  45. }
  46. return false;
  47. },
  48. name(module: { context: string }) {
  49. const packageName = getModulePackageName(module);
  50. if (packageName) {
  51. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  52. return 'viz'; // visualization package
  53. }
  54. }
  55. return 'misc';
  56. },
  57. },
  58. },
  59. });
  60. };
  61. export default webpackPlugin;