plugin.config.js 3.38 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
// Change theme plugin

愚道's avatar
愚道 committed
3
import MergeLessPlugin from 'antd-pro-merge-less';
4 5
// import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
const ThemeColorReplacer = require('webpack-theme-color-replacer');
愚道's avatar
愚道 committed
6
import path from 'path';
陈帅's avatar
陈帅 committed
7

Zhongjie Wu's avatar
Zhongjie Wu committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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;
}

愚道's avatar
愚道 committed
27
export default config => {
28 29 30 31 32
  // 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'
  ) {
33 34 35
    // 将所有 less 合并为一个供 themePlugin使用
    const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
    const stylesDir = path.join(__dirname, '../src/');
陈帅's avatar
陈帅 committed
36

37 38 39 40 41 42
    config.plugin('merge-less').use(MergeLessPlugin, [
      {
        stylesDir,
        outFile,
      },
    ]);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [{
      fileName: 'css/theme-colors.css',
      matchColors: getAntdSerials('#1890ff'), // 主色系列
    }]);
    // 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',
    //   },
    // ]);
58
  }
Zhongjie Wu's avatar
Zhongjie Wu committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  // 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
87
};
88 89 90 91 92 93 94 95 96 97 98

function getAntdSerials(color) {
  const lightens = new Array(9).fill().map((t, i) => {
    return ThemeColorReplacer.varyColor.lighten(color, i / 10);
  });
  // 此处为了简化,采用了darken。实际按color.less需求可以引入tinycolor, colorPalette变换得到颜色值
  const darkens = new Array(6).fill().map((t, i) => {
    return ThemeColorReplacer.varyColor.darken(color, i / 10);
  });
  return lightens.concat(darkens);
}