buildDevUtils.js
10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getBundleAndConfigs = getBundleAndConfigs;
exports.cleanTmpPathExceptCache = cleanTmpPathExceptCache;
exports.printFileSizes = printFileSizes;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _bundlerWebpack() {
const data = require("@umijs/bundler-webpack");
_bundlerWebpack = function _bundlerWebpack() {
return data;
};
return data;
}
function _types() {
const data = require("@umijs/types");
_types = function _types() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _zlib() {
const data = _interopRequireDefault(require("zlib"));
_zlib = function _zlib() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function getBundleAndConfigs(_x) {
return _getBundleAndConfigs.apply(this, arguments);
}
function _getBundleAndConfigs() {
_getBundleAndConfigs = _asyncToGenerator(function* ({
api,
mfsu,
port
}) {
// bundler
const Bundler = yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'modifyBundler',
initialValue: _bundlerWebpack().Bundler
});
const bundleImplementor = yield api.applyPlugins({
key: 'modifyBundleImplementor',
type: api.ApplyPluginsType.modify,
initialValue: undefined
}); // get config
function getConfig(_x2) {
return _getConfig.apply(this, arguments);
}
function _getConfig() {
_getConfig = _asyncToGenerator(function* ({
type
}) {
const env = api.env === 'production' ? 'production' : 'development';
const getConfigOpts = yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'modifyBundleConfigOpts',
initialValue: {
env,
type,
port,
mfsu,
hot: type === _types().BundlerConfigType.csr && process.env.HMR !== 'none',
entry: {
umi: (0, _path().join)(api.paths.absTmpPath, 'umi.ts')
},
// @ts-ignore
bundleImplementor,
modifyBabelOpts(opts, args) {
return _asyncToGenerator(function* () {
return yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'modifyBabelOpts',
initialValue: opts,
args
});
})();
},
modifyBabelPresetOpts(opts, args) {
return _asyncToGenerator(function* () {
return yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'modifyBabelPresetOpts',
initialValue: opts,
args
});
})();
},
chainWebpack(webpackConfig, opts) {
return _asyncToGenerator(function* () {
return yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'chainWebpack',
initialValue: webpackConfig,
args: _objectSpread({}, opts)
});
})();
}
},
args: _objectSpread(_objectSpread({}, bundlerArgs), {}, {
type,
mfsu
})
});
return yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'modifyBundleConfig',
initialValue: yield bundler.getConfig(getConfigOpts),
args: _objectSpread(_objectSpread({}, bundlerArgs), {}, {
type,
mfsu
})
});
});
return _getConfig.apply(this, arguments);
}
const bundler = new Bundler({
cwd: api.cwd,
config: api.config
});
const bundlerArgs = {
env: api.env,
bundler: {
id: Bundler.id,
version: Bundler.version
}
};
const bundleConfigs = yield api.applyPlugins({
type: api.ApplyPluginsType.modify,
key: 'modifyBundleConfigs',
initialValue: [yield getConfig({
type: _types().BundlerConfigType.csr
})].filter(Boolean),
args: _objectSpread(_objectSpread({}, bundlerArgs), {}, {
getConfig
})
});
return {
bundleImplementor,
bundler,
bundleConfigs
};
});
return _getBundleAndConfigs.apply(this, arguments);
}
function cleanTmpPathExceptCache({
absTmpPath
}) {
if (!(0, _fs().existsSync)(absTmpPath)) return;
(0, _fs().readdirSync)(absTmpPath).forEach(file => {
if (file === `.cache`) return;
_utils().rimraf.sync((0, _path().join)(absTmpPath, file));
});
} // These sizes are pretty large. We'll warn for bundles exceeding them.
const WARN_AFTER_BUNDLE_GZIP_SIZE = 1.8 * 1024 * 1024;
const WARN_AFTER_CHUNK_GZIP_SIZE = 1 * 1024 * 1024;
function printFileSizes(stats, dir) {
var _json$children;
const ui = require('@umijs/deps/compiled/cliui')({
width: 80
});
const json = stats.toJson({
hash: false,
modules: false,
chunks: false
});
const filesize = bytes => {
bytes = Math.abs(bytes);
const radix = 1024;
const unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let loop = 0; // calculate
while (bytes >= radix) {
bytes /= radix;
++loop;
}
return `${bytes.toFixed(1)} ${unit[loop]}`;
};
const assets = json.assets ? json.assets : // @ts-ignore
json === null || json === void 0 ? void 0 : (_json$children = json.children) === null || _json$children === void 0 ? void 0 : _json$children.reduce((acc, child) => acc.concat(child === null || child === void 0 ? void 0 : child.assets), []);
const seenNames = new Map();
const isJS = val => /\.js$/.test(val);
const isCSS = val => /\.css$/.test(val);
const orderedAssets = assets === null || assets === void 0 ? void 0 : assets.map(a => {
a.name = a.name.split('?')[0]; // These sizes are pretty large
const isMainBundle = a.name.indexOf('umi.') === 0;
const maxRecommendedSize = isMainBundle ? WARN_AFTER_BUNDLE_GZIP_SIZE : WARN_AFTER_CHUNK_GZIP_SIZE;
const isLarge = maxRecommendedSize && a.size > maxRecommendedSize;
return _objectSpread(_objectSpread({}, a), {}, {
suggested: isLarge && isJS(a.name)
});
}).filter(a => {
if (seenNames.has(a.name)) {
return false;
}
seenNames.set(a.name, true);
return isJS(a.name) || isCSS(a.name);
}).sort((a, b) => {
if (isJS(a.name) && isCSS(b.name)) return -1;
if (isCSS(a.name) && isJS(b.name)) return 1;
return b.size - a.size;
});
function getGzippedSize(asset) {
const filepath = (0, _path().resolve)((0, _path().join)(dir, asset.name));
if ((0, _fs().existsSync)(filepath)) {
const buffer = (0, _fs().readFileSync)(filepath);
return filesize(_zlib().default.gzipSync(buffer).length);
}
return filesize(0);
}
function makeRow(a, b, c) {
return ` ${a}\t ${b}\t ${c}`;
}
ui.div(makeRow(_utils().chalk.cyan.bold(`File`), _utils().chalk.cyan.bold(`Size`), _utils().chalk.cyan.bold(`Gzipped`)) + `\n\n` + (orderedAssets === null || orderedAssets === void 0 ? void 0 : orderedAssets.map(asset => makeRow(/js$/.test(asset.name) ? asset.suggested ? // warning for large bundle
_utils().chalk.yellow((0, _path().join)(dir, asset.name)) : _utils().chalk.green((0, _path().join)(dir, asset.name)) : _utils().chalk.blue((0, _path().join)(dir, asset.name)), filesize(asset.size), getGzippedSize(asset))).join(`\n`)));
console.log(`${ui.toString()}\n\n ${_utils().chalk.gray(`Images and other types of assets omitted.`)}\n`);
if (orderedAssets === null || orderedAssets === void 0 ? void 0 : orderedAssets.some(asset => asset.suggested)) {
// We'll warn for bundles exceeding them.
// TODO: use umi docs
console.log();
console.log(_utils().chalk.yellow('The bundle size is significantly larger than recommended.'));
console.log(_utils().chalk.yellow('Consider reducing it with code splitting: https://umijs.org/docs/load-on-demand'));
console.log(_utils().chalk.yellow('You can also analyze the project dependencies using ANALYZE=1'));
console.log();
}
}