plugin.config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Change theme plugin
  2. // import MergeLessPlugin from 'antd-pro-merge-less';
  3. // import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
  4. import ThemeColorReplacer from 'webpack-theme-color-replacer';
  5. import path from 'path';
  6. import generate from '@ant-design/colors/lib/generate';
  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 ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  25. if (
  26. process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
  27. process.env.NODE_ENV !== 'production'
  28. ) {
  29. // 将所有 less 合并为一个供 themePlugin使用
  30. // const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
  31. // const stylesDir = path.join(__dirname, '../src/');
  32. // config.plugin('merge-less').use(MergeLessPlugin, [
  33. // {
  34. // stylesDir,
  35. // outFile,
  36. // },
  37. // ]);
  38. config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [
  39. {
  40. fileName: 'css/theme-colors.css',
  41. matchColors: getAntdSerials('#1890ff'), // 主色系列
  42. // 改变样式选择器,解决样式覆盖问题
  43. changeSelector(selector: string): string {
  44. switch (selector) {
  45. case '.ant-calendar-today .ant-calendar-date':
  46. return ':not(.ant-calendar-selected-date)' + selector;
  47. case '.ant-btn:focus,.ant-btn:hover':
  48. return '.ant-btn:focus:not(.ant-btn-primary),.ant-btn:hover:not(.ant-btn-primary)';
  49. case '.ant-btn.active,.ant-btn:active':
  50. return '.ant-btn.active:not(.ant-btn-primary),.ant-btn:active:not(.ant-btn-primary)';
  51. default:
  52. return selector;
  53. }
  54. },
  55. },
  56. ]);
  57. // config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  58. // {
  59. // antDir: path.join(__dirname, '../node_modules/antd'),
  60. // stylesDir,
  61. // varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  62. // mainLessFile: outFile, // themeVariables: ['@primary-color'],
  63. // indexFileName: 'index.html',
  64. // generateOne: true,
  65. // lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  66. // },
  67. // ]);
  68. }
  69. // optimize chunks
  70. config.optimization
  71. .runtimeChunk(false) // share the same chunks across different modules
  72. .splitChunks({
  73. chunks: 'async',
  74. name: 'vendors',
  75. maxInitialRequests: Infinity,
  76. minSize: 0,
  77. cacheGroups: {
  78. vendors: {
  79. test: (module: { context: string }) => {
  80. const packageName = getModulePackageName(module);
  81. if (packageName) {
  82. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  83. }
  84. return false;
  85. },
  86. name(module: { context: string }) {
  87. const packageName = getModulePackageName(module);
  88. if (packageName) {
  89. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  90. return 'viz'; // visualization package
  91. }
  92. }
  93. return 'misc';
  94. },
  95. },
  96. },
  97. });
  98. };
  99. const getAntdSerials = (color: string) => {
  100. const lightNum = 9;
  101. const devide10 = 10;
  102. // 淡化(即less的tint)
  103. const lightens = new Array(lightNum).fill().map((t, i) => {
  104. return ThemeColorReplacer.varyColor.lighten(color, i / devide10);
  105. });
  106. const colorPalettes = generate(color);
  107. return lightens.concat(colorPalettes);
  108. }