plugin.config.js 3.9 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) {
  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;
  16. // handle tree shaking
  17. if (packageName.match('^_')) {
  18. // eslint-disable-next-line prefer-destructuring
  19. packageName = packageName.match(/^_(@?[^@]+)/)[1];
  20. }
  21. return packageName;
  22. }
  23. export default config => {
  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) {
  44. var fix = {
  45. '.ant-calendar-today .ant-calendar-date':
  46. ':not(.ant-calendar-selected-date)' + selector,
  47. '.ant-btn:focus,.ant-btn:hover': selector
  48. .split(',')
  49. .map(s => s + ':not(.ant-btn-primary)')
  50. .join(','),
  51. '.ant-btn.active,.ant-btn:active': selector
  52. .split(',')
  53. .map(s => s + ':not(.ant-btn-primary)')
  54. .join(','),
  55. };
  56. return fix[selector] || selector;
  57. },
  58. },
  59. ]);
  60. // config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  61. // {
  62. // antDir: path.join(__dirname, '../node_modules/antd'),
  63. // stylesDir,
  64. // varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  65. // mainLessFile: outFile, // themeVariables: ['@primary-color'],
  66. // indexFileName: 'index.html',
  67. // generateOne: true,
  68. // lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  69. // },
  70. // ]);
  71. }
  72. // optimize chunks
  73. config.optimization
  74. .runtimeChunk(false) // share the same chunks across different modules
  75. .splitChunks({
  76. chunks: 'async',
  77. name: 'vendors',
  78. maxInitialRequests: Infinity,
  79. minSize: 0,
  80. cacheGroups: {
  81. vendors: {
  82. test: module => {
  83. const packageName = getModulePackageName(module);
  84. if (packageName) {
  85. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  86. }
  87. return false;
  88. },
  89. name(module) {
  90. const packageName = getModulePackageName(module);
  91. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  92. return 'viz'; // visualization package
  93. }
  94. return 'misc';
  95. },
  96. },
  97. },
  98. });
  99. };
  100. function getAntdSerials(color) {
  101. const colorPalettes = generate(color);
  102. // 淡化(即less的tint)
  103. const lightens = new Array(9).fill().map((t, i) => {
  104. return ThemeColorReplacer.varyColor.lighten(color, i / 10);
  105. });
  106. return colorPalettes.concat(lightens);
  107. }