plugin.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Change theme plugin
  2. import MergeLessPlugin from 'antd-pro-merge-less';
  3. import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
  4. import path from 'path';
  5. function getModulePackageName(module: { context: string }) {
  6. if (!module.context) return null;
  7. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  8. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  9. return null;
  10. }
  11. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  12. const [moduleDirName] = moduleRelativePath.split(path.sep);
  13. let packageName: string | null = moduleDirName;
  14. // handle tree shaking
  15. if (packageName && packageName.match('^_')) {
  16. // eslint-disable-next-line prefer-destructuring
  17. packageName = packageName.match(/^_(@?[^@]+)/)![1];
  18. }
  19. return packageName;
  20. }
  21. export default (config: any) => {
  22. // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  23. if (
  24. process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
  25. process.env.NODE_ENV !== 'production'
  26. ) {
  27. // 将所有 less 合并为一个供 themePlugin使用
  28. const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
  29. const stylesDir = path.join(__dirname, '../src/');
  30. config.plugin('merge-less').use(MergeLessPlugin, [
  31. {
  32. stylesDir,
  33. outFile,
  34. },
  35. ]);
  36. config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  37. {
  38. antDir: path.join(__dirname, '../node_modules/antd'),
  39. stylesDir,
  40. varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  41. mainLessFile: outFile, // themeVariables: ['@primary-color'],
  42. indexFileName: 'index.html',
  43. generateOne: true,
  44. lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  45. },
  46. ]);
  47. }
  48. // optimize chunks
  49. config.optimization
  50. .runtimeChunk(false) // share the same chunks across different modules
  51. .splitChunks({
  52. chunks: 'async',
  53. name: 'vendors',
  54. maxInitialRequests: Infinity,
  55. minSize: 0,
  56. cacheGroups: {
  57. vendors: {
  58. test: (module: { context: string }) => {
  59. const packageName = getModulePackageName(module);
  60. if (packageName) {
  61. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  62. }
  63. return false;
  64. },
  65. name(module: { context: string }) {
  66. const packageName = getModulePackageName(module);
  67. if (packageName) {
  68. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  69. return 'viz'; // visualization package
  70. }
  71. }
  72. return 'misc';
  73. },
  74. },
  75. },
  76. });
  77. };