UNPKG

3 kBJavaScriptView Raw
1/**
2 * 生成 Webpack `optimization` 配置,用于拆分代码
3 * - 仅针对: Webpack 配置生成
4 * @param {Object} [options={}] 追加配置
5 * @param {string[]} [options.extraLibs] 追加库名到 libs 包中
6 * @returns {Object} Webpack `optimization` 配置
7 */
8module.exports = (options = {}) => {
9 const { extraLibs = [] } = options;
10 const cacheGroups = {
11 libs: {
12 name: 'libs',
13 priority: 100,
14 chunks: 'all',
15 minChunks: 1,
16 reuseExistingChunk: true,
17 test: new RegExp(
18 `[\\\\/]node_modules[\\\\/](${[
19 // react
20 'react',
21 'react-dom',
22 'react-redux',
23 'react-router',
24 'react-router-redux',
25 'redux',
26 'redux-thunk',
27
28 // babel, webpack & other tools
29 // 'regenerator-runtime',
30
31 // common libraries
32 // 'axios',
33 // 'classnames',
34 // 'history',
35 // 'js-cookie',
36 // 'lodash',
37 // 'underscore'
38
39 ...extraLibs
40 ].join('|')})[\\\\/]`
41 )
42 },
43 libsAntd: {
44 name: 'libs-ant-design-related',
45 priority: 90,
46 chunks: 'all',
47 minChunks: 1,
48 reuseExistingChunk: true,
49 test: new RegExp(
50 `[\\\\/]node_modules[\\\\/](${[
51 '@ant-design',
52 'antd',
53 'moment',
54 'rc-align',
55 'rc-animate',
56 'rc-calendar',
57 'rc-checkbox',
58 'rc-form',
59 'rc-menu',
60 'rc-notification',
61 'rc-pagination',
62 'rc-progress',
63 'rc-resize-observer',
64 'rc-select',
65 'rc-tabs',
66 'rc-tooltip',
67 'rc-trigger',
68 'rc-upload',
69 'rc-util'
70 ].join('|')})[\\\\/]`
71 )
72 },
73 libsOthers: {
74 name: 'libs-others',
75 priority: 10,
76 chunks: 'all',
77 minChunks: 2,
78 reuseExistingChunk: true
79 }
80 };
81
82 // const isKootTest =
83 // global.kootTest ||
84 // (process.env.KOOT_TEST_MODE && JSON.parse(process.env.KOOT_TEST_MODE));
85 // if (isKootTest) {
86 // delete cacheGroups.libsOthers
87 // }
88
89 return {
90 minimize: true,
91 noEmitOnErrors: true,
92
93 splitChunks: {
94 maxAsyncRequests: 8,
95 maxInitialRequests: 6,
96
97 cacheGroups
98 }
99 };
100};