plugin.config.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import path from 'path';
  2. import * as IWebpackChainConfig from 'webpack-chain';
  3. import MonacoWebpackPlugin from 'monaco-editor-webpack-plugin';
  4. function getModulePackageName(module: { context: string }) {
  5. if (!module.context) return null;
  6. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  7. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  8. return null;
  9. }
  10. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  11. const [moduleDirName] = moduleRelativePath.split(path.sep);
  12. let packageName: string | null = moduleDirName;
  13. // handle tree shaking
  14. if (packageName && packageName.match('^_')) {
  15. // eslint-disable-next-line prefer-destructuring
  16. packageName = packageName.match(/^_(@?[^@]+)/)![1];
  17. }
  18. return packageName;
  19. }
  20. const webpackPlugin = (config: IWebpackChainConfig) => {
  21. // optimize chunks
  22. config.optimization
  23. // share the same chunks across different modules
  24. .runtimeChunk(false)
  25. .splitChunks({
  26. chunks: 'async',
  27. name: 'vendors',
  28. maxInitialRequests: Infinity,
  29. minSize: 0,
  30. cacheGroups: {
  31. vendors: {
  32. test: (module: { context: string }) => {
  33. const packageName = getModulePackageName(module) || '';
  34. if (packageName) {
  35. return [
  36. 'bizcharts',
  37. 'gg-editor',
  38. 'g6',
  39. '@antv',
  40. 'l7',
  41. 'gg-editor-core',
  42. 'bizcharts-plugin-slider',
  43. ].includes(packageName);
  44. }
  45. return false;
  46. },
  47. name(module: { context: string }) {
  48. const packageName = getModulePackageName(module);
  49. if (packageName) {
  50. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  51. return 'viz'; // visualization package
  52. }
  53. }
  54. return 'misc';
  55. },
  56. },
  57. },
  58. });
  59. config.plugin('monaco-editor').use(
  60. new MonacoWebpackPlugin({
  61. languages: ['json','javascript', 'typescript'],
  62. }),
  63. );
  64. };
  65. export default webpackPlugin;