import path from 'path'; function getModulePackageName(module) { if (!module.context) return null; const nodeModulesPath = path.join(__dirname, '../node_modules/'); if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) { return null; } const moduleRelativePath = module.context.substring(nodeModulesPath.length); const [moduleDirName] = moduleRelativePath.split(path.sep); let packageName = moduleDirName; // handle tree shaking if (packageName && packageName.match('^_')) { // eslint-disable-next-line prefer-destructuring packageName = packageName.match(/^_(@?[^@]+)/)[1]; } return packageName; } export const chainWebpack = config => { // optimize chunks config.optimization // share the same chunks across different modules .runtimeChunk(false) .splitChunks({ chunks: 'all', // name: 'vendors', maxInitialRequests: Infinity, minSize: 0, cacheGroups: { commonDependencies: { test (module) { const reg = /[\\/]node_modules[\\/]/g return reg.test(module.context) }, priority: 1, name: 'commonDependencies.js' }, }, }); };