You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.9 KiB
66 lines
1.9 KiB
import path from 'path';
|
|
|
|
import * as IWebpackChainConfig from 'webpack-chain';
|
|
|
|
function getModulePackageName(module: { context: string }) {
|
|
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: string | null = moduleDirName;
|
|
// handle tree shaking
|
|
if (packageName && packageName.match('^_')) {
|
|
// eslint-disable-next-line prefer-destructuring
|
|
packageName = packageName.match(/^_(@?[^@]+)/)![1];
|
|
}
|
|
return packageName;
|
|
}
|
|
|
|
const webpackPlugin = (config: IWebpackChainConfig) => {
|
|
// optimize chunks
|
|
config.optimization
|
|
// share the same chunks across different modules
|
|
.runtimeChunk(false)
|
|
.splitChunks({
|
|
chunks: 'async',
|
|
name: 'vendors',
|
|
maxInitialRequests: Infinity,
|
|
minSize: 0,
|
|
cacheGroups: {
|
|
vendors: {
|
|
test: (module: { context: string }) => {
|
|
const packageName = getModulePackageName(module) || '';
|
|
if (packageName) {
|
|
return [
|
|
'bizcharts',
|
|
'gg-editor',
|
|
'g6',
|
|
'@antv',
|
|
'l7',
|
|
'gg-editor-core',
|
|
'bizcharts-plugin-slider',
|
|
].includes(packageName);
|
|
}
|
|
return false;
|
|
},
|
|
name(module: { context: string }) {
|
|
const packageName = getModulePackageName(module);
|
|
if (packageName) {
|
|
if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
|
|
return 'viz'; // visualization package
|
|
}
|
|
}
|
|
return 'misc';
|
|
},
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
export default webpackPlugin;
|