plugin.config.js 2.51 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

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

26
export default config => {
27 28 29 30 31
  // pro ε’Œ εΌ€ε‘ηŽ―ε’ƒε†ζ·»εŠ θΏ™δΈͺ插仢
  if (process.env.APP_TYPE === 'site' || process.env.NODE_ENV !== 'production') {
    // ε°†ζ‰€ζœ‰ less εˆεΉΆδΈΊδΈ€δΈͺδΎ› themePlugin使用
    const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
    const stylesDir = path.join(__dirname, '../src/');
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32

33 34 35 36 37 38
    config.plugin('merge-less').use(MergeLessPlugin, [
      {
        stylesDir,
        outFile,
      },
    ]);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39

40 41 42 43 44 45 46 47 48 49 50 51
    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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  // optimize chunks
  config.optimization
    .runtimeChunk(false) // share the same chunks across different modules
    .splitChunks({
      chunks: 'async',
      name: 'vendors',
      maxInitialRequests: Infinity,
      minSize: 0,
      cacheGroups: {
        vendors: {
          test: module => {
            const packageName = getModulePackageName(module);
            if (packageName) {
              return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
            }
            return false;
          },
          name(module) {
            const packageName = getModulePackageName(module);

            if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
              return 'viz'; // visualization package
            }
            return 'misc';
          },
        },
      },
    });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80
};