plugin.config.ts 3.31 KB
Newer Older
陈帅's avatar
陈帅 committed
1
// Change theme plugin
陈帅's avatar
陈帅 committed
2 3
// eslint-disable-next-line eslint-comments/abdeils - enable - pair;
/* eslint-disable import/no-extraneous-dependencies */
4 5
import ThemeColorReplacer from 'webpack-theme-color-replacer';
import generate from '@ant-design/colors/lib/generate';
陈帅's avatar
陈帅 committed
6
import path from 'path';
陈帅's avatar
陈帅 committed
7

陈帅's avatar
陈帅 committed
8
function getModulePackageName(module: { context: string }) {
Zhongjie Wu's avatar
Zhongjie Wu committed
9 10 11 12 13 14 15 16 17
  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);
陈帅's avatar
陈帅 committed
18
  let packageName: string | null = moduleDirName;
Zhongjie Wu's avatar
Zhongjie Wu committed
19
  // handle tree shaking
陈帅's avatar
陈帅 committed
20
  if (packageName && packageName.match('^_')) {
Zhongjie Wu's avatar
Zhongjie Wu committed
21
    // eslint-disable-next-line prefer-destructuring
陈帅's avatar
陈帅 committed
22
    packageName = packageName.match(/^_(@?[^@]+)/)![1];
Zhongjie Wu's avatar
Zhongjie Wu committed
23 24 25 26
  }
  return packageName;
}

陈帅's avatar
陈帅 committed
27
export default (config: any) => {
28
  // preview.pro.ant.design only do not use in your production;
29 30 31 32
  if (
    process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
    process.env.NODE_ENV !== 'production'
  ) {
33 34
    config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [
      {
35
        fileName: 'css/theme-colors-[contenthash:8].css',
36
        matchColors: getAntdSerials('#1890ff'), // 主色系列
37
        // 改变样式选择器,解决样式覆盖问题
huangzheng's avatar
tslint  
huangzheng committed
38
        changeSelector(selector: string): string {
hz's avatar
hz committed
39 40 41 42 43 44 45 46 47 48
          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;
          }
49
        },
50
        // isJsUgly: true,
51 52
      },
    ]);
53
  }
54

Zhongjie Wu's avatar
Zhongjie Wu committed
55 56
  // optimize chunks
  config.optimization
57 58
    // share the same chunks across different modules
    .runtimeChunk(false)
Zhongjie Wu's avatar
Zhongjie Wu committed
59 60 61 62 63 64 65
    .splitChunks({
      chunks: 'async',
      name: 'vendors',
      maxInitialRequests: Infinity,
      minSize: 0,
      cacheGroups: {
        vendors: {
陈帅's avatar
陈帅 committed
66
          test: (module: { context: string }) => {
Zhongjie Wu's avatar
Zhongjie Wu committed
67 68 69 70 71 72
            const packageName = getModulePackageName(module);
            if (packageName) {
              return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
            }
            return false;
          },
陈帅's avatar
陈帅 committed
73
          name(module: { context: string }) {
Zhongjie Wu's avatar
Zhongjie Wu committed
74
            const packageName = getModulePackageName(module);
陈帅's avatar
陈帅 committed
75 76 77 78
            if (packageName) {
              if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
                return 'viz'; // visualization package
              }
Zhongjie Wu's avatar
Zhongjie Wu committed
79 80 81 82 83 84
            }
            return 'misc';
          },
        },
      },
    });
陈帅's avatar
陈帅 committed
85
};
86

huangzheng's avatar
lint  
huangzheng committed
87 88 89
const getAntdSerials = (color: string) => {
  const lightNum = 9;
  const devide10 = 10;
90
  // 淡化(即less的tint)
陈帅's avatar
陈帅 committed
91
  const lightens = new Array(lightNum).fill(undefined).map((_, i: number) => {
huangzheng's avatar
lint  
huangzheng committed
92
    return ThemeColorReplacer.varyColor.lighten(color, i / devide10);
93
  });
hz's avatar
hz committed
94 95
  const colorPalettes = generate(color);
  return lightens.concat(colorPalettes);
陈帅's avatar
陈帅 committed
96
};