plugin.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Change theme plugin
  2. import MergeLessPlugin from 'antd-pro-merge-less';
  3. import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
  4. import path from 'path';
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin');
  6. const prodGzipList = ['js', 'css'];
  7. export default config => {
  8. config.module
  9. .rule()
  10. .test(/\.(pdf|svg|docx|doc)$/)
  11. .use('file-loader?name=[path][name].[ext]')
  12. .loader('file-loader')
  13. .end();
  14. // pro 和 开发环境再添加这个插件
  15. if (process.env.APP_TYPE === 'site' || process.env.NODE_ENV !== 'production') {
  16. // 将所有 less 合并为一个供 themePlugin使用
  17. const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
  18. const stylesDir = path.join(__dirname, '../src/');
  19. config.plugin('merge-less').use(MergeLessPlugin, [
  20. {
  21. stylesDir,
  22. outFile,
  23. },
  24. ]);
  25. config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  26. {
  27. antDir: path.join(__dirname, '../node_modules/antd'),
  28. stylesDir,
  29. varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  30. mainLessFile: outFile, // themeVariables: ['@primary-color'],
  31. indexFileName: 'index.html',
  32. generateOne: true,
  33. lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  34. },
  35. ]);
  36. }
  37. if (process.env.APP_TYPE !== 'site' || process.env.NODE_ENV !== 'development') {
  38. config.merge({
  39. optimization: {
  40. minimize: true,
  41. splitChunks: {
  42. chunks: 'async',
  43. // chunks(chunk) {
  44. // // exclude `my-excluded-chunk`
  45. // return chunk.name !== 'my-excluded-chunk';
  46. // },
  47. minSize: 20000, //生成块的最小大小(以字节为单位)1024字节=1KB。
  48. minChunks: 1, //拆分前必须共享模块的最小块数。
  49. maxInitialRequests: 30, //入口点的最大并行请求数。
  50. automaticNameDelimiter: '.',
  51. cacheGroups: {
  52. vendor: {
  53. name: 'vendors',
  54. test: /^.*node_modules[\\/](?!ag-grid-|lodash|wangeditor|react-virtualized|rc-drawer|rc-time-picker|rc-tree|echarts|echarts-gl|xlsx|@ant-design|antd|ali-oss).*$/,
  55. priority: -10,
  56. enforce: true,
  57. },
  58. virtualized: {
  59. name: 'virtualized',
  60. test: /[\\/]node_modules[\\/]react-virtualized/,
  61. priority: 10,
  62. },
  63. antd: {
  64. name: 'antd',
  65. test: /[\\/]node_modules[\\/]antd[\\/]/,
  66. priority: 9,
  67. },
  68. // antv: {
  69. // name: "@antv",
  70. // test: /[\\/]node_modules[\\/]@antv[\\/]/,
  71. // priority: 9,
  72. // },
  73. echarts: {
  74. // 1.27MB
  75. name: 'echarts',
  76. test: /[\\/]node_modules[\\/](echarts|echarts-gl)[\\/]/,
  77. priority: 10,
  78. enforce: true,
  79. },
  80. bizcharts: {
  81. name: 'bizcharts',
  82. test: /[\\/]node_modules[\\/]bizcharts[\\/]/,
  83. chunks: 'all',
  84. priority: 10,
  85. },
  86. hlsJs: {
  87. name: 'hlsJs',
  88. test: /[\\/]node_modules[\\/]hls.js[\\/]/,
  89. chunks: 'all',
  90. priority: 10,
  91. },
  92. // moment: {
  93. // name: "moment",
  94. // test: /[\\/]node_modules[\\/]moment[\\/]/,
  95. // chunks: "all",
  96. // priority: 10
  97. // },
  98. fileManage: {
  99. name: 'fileManage',
  100. test: /[\\/]node_modules[\\/]@opuscapita[\\/]/,
  101. chunks: 'all',
  102. priority: 10,
  103. },
  104. pdfJs: {
  105. name: 'pdfjs-dist',
  106. test: /[\\/]node_modules[\\/]pdfjs-dist[\\/]/,
  107. chunks: 'all',
  108. priority: 10,
  109. },
  110. ggEditor: {
  111. name: 'ggEditor',
  112. test: /[\\/]node_modules[\\/]gg-editor-core[\\/]/,
  113. chunks: 'all',
  114. priority: 10,
  115. },
  116. // zrender: {
  117. // name: "zrender",
  118. // test: /[\\/]node_modules[\\/]zrender[\\/]/,
  119. // chunks: "all",
  120. // priority: 10
  121. // },
  122. swiper: {
  123. name: 'swiper',
  124. test: /[\\/]node_modules[\\/]swiper[\\/]/,
  125. chunks: 'all',
  126. priority: 10,
  127. },
  128. flv: {
  129. name: 'flv',
  130. test: /[\\/]node_modules[\\/]flv.js[\\/]/,
  131. chunks: 'all',
  132. priority: 10,
  133. },
  134. xlsx: {
  135. name: 'xlsx',
  136. test: /[\\/]node_modules[\\/]xlsx[\\/]/,
  137. chunks: 'async',
  138. priority: 10,
  139. },
  140. // rctree: {
  141. // name: "rctree",
  142. // test: /[\\/]node_modules[\\/]rc-tree/,
  143. // chunks: "all",
  144. // priority: -1
  145. // },
  146. // rccalendar: {
  147. // name: "rccalendar",
  148. // test: /[\\/]node_modules[\\/]rc-calendar[\\/]/,
  149. // chunks: "all",
  150. // priority: -1
  151. // },
  152. wang: {
  153. name: 'wang',
  154. test: /[\\/]node_modules[\\/]wangeditor[\\/]/,
  155. chunks: 'all',
  156. priority: -1,
  157. },
  158. // lodash: {
  159. // name: "lodash",
  160. // test: /[\\/]node_modules[\\/]lodash[\\/]/,
  161. // chunks: "all",
  162. // priority: -2
  163. // },
  164. // rcdrawer: {
  165. // name: "rcdrawer",
  166. // test: /[\\/]node_modules[\\/]rc-drawer/,
  167. // chunks: "all",
  168. // priority: 10
  169. // },
  170. ag: {
  171. name: 'ag',
  172. test: /[\\/]node_modules[\\/]ag-grid-/,
  173. chunks: 'all',
  174. priority: 10,
  175. },
  176. },
  177. },
  178. },
  179. });
  180. }
  181. if (process.env.NODE_ENV === 'production') {
  182. // 生产模式开启
  183. config.plugin('compression-webpack-plugin').use(
  184. new CompressionWebpackPlugin({
  185. // filename: 文件名称,这里我们不设置,让它保持和未压缩的文件同一个名称
  186. algorithm: 'gzip', // 指定生成gzip格式
  187. test: new RegExp('\\.(' + prodGzipList.join('|') + ')$'), // 匹配哪些格式文件需要压缩
  188. threshold: 10240, //对超过10k的数据进行压缩
  189. minRatio: 0.6, // 压缩比例,值为0 ~ 1
  190. })
  191. );
  192. }
  193. //过滤掉momnet的那些不使用的国际化文件
  194. config
  195. .plugin('replace')
  196. .use(require('webpack').ContextReplacementPlugin)
  197. .tap(() => {
  198. return [/moment[/\\]locale$/, /zh-cn/];
  199. });
  200. // config.output.filename = '[name].[contenthash].js'
  201. };