plugin.config.ts 2.81 KB
Newer Older
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
1 2
// Change theme plugin

3
import MergeLessPlugin from 'antd-pro-merge-less';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
4
import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
5
import path from 'path';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
6

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

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
26
export default (config: any) => {
27 28 29 30 31
  // 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'
  ) {
32 33 34
    // ๅฐ†ๆ‰€ๆœ‰ less ๅˆๅนถไธบไธ€ไธชไพ› themePluginไฝฟ็”จ
    const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
    const stylesDir = path.join(__dirname, '../src/');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
35

36 37 38 39 40 41
    config.plugin('merge-less').use(MergeLessPlugin, [
      {
        stylesDir,
        outFile,
      },
    ]);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
42

43 44 45 46 47 48 49 50 51 52 53 54
    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',
      },
    ]);
  }
Zhongjie Wu's avatar
Zhongjie Wu committed
55 56 57 58 59 60 61 62 63 64
  // optimize chunks
  config.optimization
    .runtimeChunk(false) // share the same chunks across different modules
    .splitChunks({
      chunks: 'async',
      name: 'vendors',
      maxInitialRequests: Infinity,
      minSize: 0,
      cacheGroups: {
        vendors: {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
65
          test: (module: { context: string }) => {
Zhongjie Wu's avatar
Zhongjie Wu committed
66 67 68 69 70 71
            const packageName = getModulePackageName(module);
            if (packageName) {
              return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
            }
            return false;
          },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
72
          name(module: { context: string }) {
Zhongjie Wu's avatar
Zhongjie Wu committed
73
            const packageName = getModulePackageName(module);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
74 75 76 77
            if (packageName) {
              if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
                return 'viz'; // visualization package
              }
Zhongjie Wu's avatar
Zhongjie Wu committed
78 79 80 81 82 83
            }
            return 'misc';
          },
        },
      },
    });
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
84
};