plugin.config.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 chainWebpack = config => {
  18. // optimize chunks
  19. config.optimization // share the same chunks across different modules
  20. .runtimeChunk(false)
  21. .splitChunks({
  22. chunks: 'all',
  23. // name: 'vendors',
  24. maxInitialRequests: Infinity,
  25. minSize: 0,
  26. cacheGroups: {
  27. commonDependencies: {
  28. test (module) {
  29. const reg = /[\\/]node_modules[\\/]/g
  30. return reg.test(module.context)
  31. },
  32. priority: 1,
  33. name: 'commonDependencies.js'
  34. },
  35. },
  36. });
  37. };