plugin.config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Change theme plugin
  2. // eslint-disable-next-line eslint-comments/abdeils - enable - pair;
  3. /* eslint-disable import/no-extraneous-dependencies */
  4. import ThemeColorReplacer from 'webpack-theme-color-replacer';
  5. import generate from '@ant-design/colors/lib/generate';
  6. import path from 'path';
  7. function getModulePackageName(module: { context: string }) {
  8. if (!module.context) return null;
  9. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  10. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  11. return null;
  12. }
  13. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  14. const [moduleDirName] = moduleRelativePath.split(path.sep);
  15. let packageName: string | null = moduleDirName;
  16. // handle tree shaking
  17. if (packageName && packageName.match('^_')) {
  18. // eslint-disable-next-line prefer-destructuring
  19. packageName = packageName.match(/^_(@?[^@]+)/)![1];
  20. }
  21. return packageName;
  22. }
  23. export default (config: any) => {
  24. // preview.pro.ant.design only do not use in your production;
  25. if (
  26. process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
  27. process.env.NODE_ENV !== 'production'
  28. ) {
  29. config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [
  30. {
  31. fileName: 'css/theme-colors-[contenthash:8].css',
  32. matchColors: getAntdSerials('#1890ff'), // 主色系列
  33. // 改变样式选择器,解决样式覆盖问题
  34. changeSelector(selector: string): string {
  35. switch (selector) {
  36. case '.ant-calendar-today .ant-calendar-date':
  37. return ':not(.ant-calendar-selected-date)' + selector;
  38. case '.ant-btn:focus,.ant-btn:hover':
  39. return '.ant-btn:focus:not(.ant-btn-primary),.ant-btn:hover:not(.ant-btn-primary)';
  40. case '.ant-btn.active,.ant-btn:active':
  41. return '.ant-btn.active:not(.ant-btn-primary),.ant-btn:active:not(.ant-btn-primary)';
  42. default:
  43. return selector;
  44. }
  45. },
  46. // isJsUgly: true,
  47. },
  48. ]);
  49. }
  50. // optimize chunks
  51. config.optimization
  52. // share the same chunks across different modules
  53. .runtimeChunk(false)
  54. .splitChunks({
  55. chunks: 'async',
  56. name: 'vendors',
  57. maxInitialRequests: Infinity,
  58. minSize: 0,
  59. cacheGroups: {
  60. vendors: {
  61. test: (module: { context: string }) => {
  62. const packageName = getModulePackageName(module);
  63. if (packageName) {
  64. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  65. }
  66. return false;
  67. },
  68. name(module: { context: string }) {
  69. const packageName = getModulePackageName(module);
  70. if (packageName) {
  71. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  72. return 'viz'; // visualization package
  73. }
  74. }
  75. return 'misc';
  76. },
  77. },
  78. },
  79. });
  80. };
  81. const getAntdSerials = (color: string) => {
  82. const lightNum = 9;
  83. const devide10 = 10;
  84. // 淡化(即less的tint)
  85. const lightens = new Array(lightNum).fill(undefined).map((_, i: number) => {
  86. return ThemeColorReplacer.varyColor.lighten(color, i / devide10);
  87. });
  88. const colorPalettes = generate(color);
  89. return lightens.concat(colorPalettes);
  90. };