plugin.config.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Change theme plugin
  2. import MergeLessPlugin from 'antd-pro-merge-less';
  3. // import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
  4. import ThemeColorReplacer from 'webpack-theme-color-replacer';
  5. import path from 'path';
  6. import generate from '@ant-design/colors/lib/generate';
  7. function getModulePackageName(module) {
  8. if (!module.context) return null;
  9. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  10. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  11. return null;
  12. }
  13. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  14. const [moduleDirName] = moduleRelativePath.split(path.sep);
  15. let packageName = moduleDirName;
  16. // handle tree shaking
  17. if (packageName.match('^_')) {
  18. // eslint-disable-next-line prefer-destructuring
  19. packageName = packageName.match(/^_(@?[^@]+)/)[1];
  20. }
  21. return packageName;
  22. }
  23. export default config => {
  24. // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  25. if (
  26. process.env.ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ||
  27. process.env.NODE_ENV !== 'production'
  28. ) {
  29. // 将所有 less 合并为一个供 themePlugin使用
  30. const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
  31. const stylesDir = path.join(__dirname, '../src/');
  32. config.plugin('merge-less').use(MergeLessPlugin, [
  33. {
  34. stylesDir,
  35. outFile,
  36. },
  37. ]);
  38. config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [{
  39. fileName: 'css/theme-colors.css',
  40. matchColors: generate('#1890ff'), // 主色系列
  41. }]);
  42. // config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  43. // {
  44. // antDir: path.join(__dirname, '../node_modules/antd'),
  45. // stylesDir,
  46. // varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  47. // mainLessFile: outFile, // themeVariables: ['@primary-color'],
  48. // indexFileName: 'index.html',
  49. // generateOne: true,
  50. // lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  51. // },
  52. // ]);
  53. }
  54. // optimize chunks
  55. config.optimization
  56. .runtimeChunk(false) // share the same chunks across different modules
  57. .splitChunks({
  58. chunks: 'async',
  59. name: 'vendors',
  60. maxInitialRequests: Infinity,
  61. minSize: 0,
  62. cacheGroups: {
  63. vendors: {
  64. test: module => {
  65. const packageName = getModulePackageName(module);
  66. if (packageName) {
  67. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  68. }
  69. return false;
  70. },
  71. name(module) {
  72. const packageName = getModulePackageName(module);
  73. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  74. return 'viz'; // visualization package
  75. }
  76. return 'misc';
  77. },
  78. },
  79. },
  80. });
  81. };