plugin.config.ts 4.0 KB
Newer Older
陈帅 已提交
1 2
// Change theme plugin

愚道 已提交
3
import MergeLessPlugin from 'antd-pro-merge-less';
4
// import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
5
import ThemeColorReplacer from 'webpack-theme-color-replacer';
愚道 已提交
6
import path from 'path';
7
import generate from '@ant-design/colors/lib/generate';
陈帅 已提交
8

陈帅 已提交
9
function getModulePackageName(module: { context: string }) {
Z
Zhongjie Wu 已提交
10 11 12 13 14 15 16 17 18
  if (!module.context) return null;

  const nodeModulesPath = path.join(__dirname, '../node_modules/');
  if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
    return null;
  }

  const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  const [moduleDirName] = moduleRelativePath.split(path.sep);
陈帅 已提交
19
  let packageName: string | null = moduleDirName;
Z
Zhongjie Wu 已提交
20
  // handle tree shaking
陈帅 已提交
21
  if (packageName && packageName.match('^_')) {
Z
Zhongjie Wu 已提交
22
    // eslint-disable-next-line prefer-destructuring
陈帅 已提交
23
    packageName = packageName.match(/^_(@?[^@]+)/)![1];
Z
Zhongjie Wu 已提交
24 25 26 27
  }
  return packageName;
}

陈帅 已提交
28
export default (config: any) => {
29 30 31 32 33
  // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  if (
    process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
    process.env.NODE_ENV !== 'production'
  ) {
34 35 36
    // 将所有 less 合并为一个供 themePlugin使用
    const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
    const stylesDir = path.join(__dirname, '../src/');
陈帅 已提交
37

38 39 40 41 42 43
    config.plugin('merge-less').use(MergeLessPlugin, [
      {
        stylesDir,
        outFile,
      },
    ]);
44 45 46
    config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [
      {
        fileName: 'css/theme-colors.css',
47
        matchColors: getAntdSerials('#1890ff'), // 主色系列
48 49
        // 改变样式选择器,解决样式覆盖问题
        changeSelector(selector) {
hz932's avatar
hz932 已提交
50 51 52 53 54 55 56 57 58 59
          switch (selector) {
            case '.ant-calendar-today .ant-calendar-date':
              return ':not(.ant-calendar-selected-date)' + selector;
            case '.ant-btn:focus,.ant-btn:hover':
              return '.ant-btn:focus:not(.ant-btn-primary),.ant-btn:hover:not(.ant-btn-primary)';
            case '.ant-btn.active,.ant-btn:active':
              return '.ant-btn.active:not(.ant-btn-primary),.ant-btn:active:not(.ant-btn-primary)';
            default:
              return selector;
          }
60 61 62
        },
      },
    ]);
63 64 65 66 67 68 69 70 71 72 73
    // config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
    //   {
    //     antDir: path.join(__dirname, '../node_modules/antd'),
    //     stylesDir,
    //     varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
    //     mainLessFile: outFile, //     themeVariables: ['@primary-color'],
    //     indexFileName: 'index.html',
    //     generateOne: true,
    //     lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
    //   },
    // ]);
74
  }
Z
Zhongjie Wu 已提交
75 76 77 78 79 80 81 82 83 84
  // optimize chunks
  config.optimization
    .runtimeChunk(false) // share the same chunks across different modules
    .splitChunks({
      chunks: 'async',
      name: 'vendors',
      maxInitialRequests: Infinity,
      minSize: 0,
      cacheGroups: {
        vendors: {
陈帅 已提交
85
          test: (module: { context: string }) => {
Z
Zhongjie Wu 已提交
86 87 88 89 90 91
            const packageName = getModulePackageName(module);
            if (packageName) {
              return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
            }
            return false;
          },
陈帅 已提交
92
          name(module: { context: string }) {
Z
Zhongjie Wu 已提交
93
            const packageName = getModulePackageName(module);
陈帅 已提交
94 95 96 97
            if (packageName) {
              if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
                return 'viz'; // visualization package
              }
Z
Zhongjie Wu 已提交
98 99 100 101 102 103
            }
            return 'misc';
          },
        },
      },
    });
陈帅 已提交
104
};
105 106 107 108 109 110

function getAntdSerials(color) {
  // 淡化(即less的tint)
  const lightens = new Array(9).fill().map((t, i) => {
    return ThemeColorReplacer.varyColor.lighten(color, i / 10);
  });
hz932's avatar
hz932 已提交
111 112
  const colorPalettes = generate(color);
  return lightens.concat(colorPalettes);
113
}