plugin.config.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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) {
  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 = moduleDirName; // handle tree shaking
  16. if (packageName && packageName.match('^_')) {
  17. // eslint-disable-next-line prefer-destructuring
  18. packageName = packageName.match(/^_(@?[^@]+)/)[1];
  19. }
  20. return packageName;
  21. }
  22. export default config => {
  23. // preview.pro.ant.design only do not use in your production;
  24. if (
  25. process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
  26. process.env.NODE_ENV !== 'production'
  27. ) {
  28. config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [
  29. {
  30. fileName: 'css/theme-colors-[contenthash:8].css',
  31. matchColors: getAntdSerials('#1890ff'),
  32. // 主色系列
  33. // 改变样式选择器,解决样式覆盖问题
  34. changeSelector(selector) {
  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. }, // isJsUgly: true,
  46. },
  47. ]);
  48. } // optimize chunks
  49. config.optimization // share the same chunks across different modules
  50. .runtimeChunk(false)
  51. .splitChunks({
  52. chunks: 'async',
  53. name: 'vendors',
  54. maxInitialRequests: Infinity,
  55. minSize: 0,
  56. cacheGroups: {
  57. vendors: {
  58. test: module => {
  59. const packageName = getModulePackageName(module) || '';
  60. if (packageName) {
  61. return [
  62. 'bizcharts',
  63. 'gg-editor',
  64. 'g6',
  65. '@antv',
  66. 'gg-editor-core',
  67. 'bizcharts-plugin-slider',
  68. ].includes(packageName);
  69. }
  70. return false;
  71. },
  72. name(module) {
  73. const packageName = getModulePackageName(module);
  74. if (packageName) {
  75. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  76. return 'viz'; // visualization package
  77. }
  78. }
  79. return 'misc';
  80. },
  81. },
  82. },
  83. });
  84. };
  85. const getAntdSerials = color => {
  86. const lightNum = 9;
  87. const devide10 = 10; // 淡化(即less的tint)
  88. const lightens = new Array(lightNum).fill(undefined).map((_, i) => {
  89. return ThemeColorReplacer.varyColor.lighten(color, i / devide10);
  90. });
  91. const colorPalettes = generate(color);
  92. return lightens.concat(colorPalettes);
  93. };