plugin.config.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Change theme plugin
  2. import MergeLessPlugin from 'antd-pro-merge-less';
  3. // import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
  4. const ThemeColorReplacer = require('webpack-theme-color-replacer');
  5. import path from 'path';
  6. function getModulePackageName(module) {
  7. if (!module.context) return null;
  8. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  9. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  10. return null;
  11. }
  12. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  13. const [moduleDirName] = moduleRelativePath.split(path.sep);
  14. let packageName = moduleDirName;
  15. // handle tree shaking
  16. if (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 ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  24. if (
  25. process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
  26. process.env.NODE_ENV !== 'production'
  27. ) {
  28. // 将所有 less 合并为一个供 themePlugin使用
  29. const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
  30. const stylesDir = path.join(__dirname, '../src/');
  31. config.plugin('merge-less').use(MergeLessPlugin, [
  32. {
  33. stylesDir,
  34. outFile,
  35. },
  36. ]);
  37. config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [{
  38. fileName: 'css/theme-colors.css',
  39. matchColors: getAntdSerials('#1890ff'), // 主色系列
  40. }]);
  41. // config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  42. // {
  43. // antDir: path.join(__dirname, '../node_modules/antd'),
  44. // stylesDir,
  45. // varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  46. // mainLessFile: outFile, // themeVariables: ['@primary-color'],
  47. // indexFileName: 'index.html',
  48. // generateOne: true,
  49. // lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  50. // },
  51. // ]);
  52. }
  53. // optimize chunks
  54. config.optimization
  55. .runtimeChunk(false) // share the same chunks across different modules
  56. .splitChunks({
  57. chunks: 'async',
  58. name: 'vendors',
  59. maxInitialRequests: Infinity,
  60. minSize: 0,
  61. cacheGroups: {
  62. vendors: {
  63. test: module => {
  64. const packageName = getModulePackageName(module);
  65. if (packageName) {
  66. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  67. }
  68. return false;
  69. },
  70. name(module) {
  71. const packageName = getModulePackageName(module);
  72. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  73. return 'viz'; // visualization package
  74. }
  75. return 'misc';
  76. },
  77. },
  78. },
  79. });
  80. };
  81. function getAntdSerials(color) {
  82. const lightens = new Array(9).fill().map((t, i) => {
  83. return ThemeColorReplacer.varyColor.lighten(color, i / 10);
  84. });
  85. // 此处为了简化,采用了darken。实际按color.less需求可以引入tinycolor, colorPalette变换得到颜色值
  86. const darkens = new Array(6).fill().map((t, i) => {
  87. return ThemeColorReplacer.varyColor.darken(color, i / 10);
  88. });
  89. return lightens.concat(darkens);
  90. }