1 | import { SUBTLE, METRO, LAVA } from '@palett/presets';
|
2 | import { Deco as Deco$2 } from '@spare/deco-object';
|
3 | import { COSP } from '@spare/enum-chars';
|
4 | import { dateTime } from '@valjoux/timestamp-pretty';
|
5 | import { COLORANT } from '@palett/enum-colorant-modes';
|
6 | import { fluoVector } from '@palett/fluo-vector';
|
7 | import { Deco } from '@spare/deco-string';
|
8 | import { Deco as Deco$1 } from '@spare/deco-vector';
|
9 | import { zipper } from '@vect/vector-zipper';
|
10 | import { sync } from 'brotli-size';
|
11 | import fileSize from 'filesize';
|
12 | import gzip from 'gzip-size';
|
13 | import terser from 'terser';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const sizeInfo = async function (bundle, p) {
|
28 | const { code, fileName } = bundle, { format } = p;
|
29 | const minifiedCode = (await terser.minify(code))?.code ?? '';
|
30 | const info = {};
|
31 | const sizes = { bundle: fileSize(Buffer.byteLength(code), format) };
|
32 | if (p.showBrotli) Object.assign(sizes, { brotli: fileSize(sync(code), format) });
|
33 | if (p.showMinified) Object.assign(sizes, { min: fileSize(minifiedCode.length, format) });
|
34 | if (p.showGzipped) Object.assign(sizes, { gzip: fileSize(gzip.sync(minifiedCode), format) });
|
35 | info['file'] = decoFileName(fileName);
|
36 | info[decoNames(Object.keys(sizes))] = decoSizeValues(Object.values(sizes), p.preset);
|
37 | return p.render ? p.render(info) : info
|
38 | };
|
39 |
|
40 | const KB = /\s+KB/gi;
|
41 |
|
42 | const decoFileName = Deco({ presets: [SUBTLE, METRO] });
|
43 |
|
44 | const decoNames = Deco$1({ indexed: false, delim: '/', presets: [SUBTLE, LAVA] });
|
45 |
|
46 | const decoSizeValues = (values, preset) => {
|
47 | const colorants = fluoVector.call(COLORANT, values.map(x => +x.replace(KB, '')), [preset]);
|
48 | return zipper(values, colorants, (v, d) => d(v))
|
49 | };
|
50 |
|
51 | const decoSizeInfoObject = Deco$2({ delim: COSP, bracket: true });
|
52 |
|
53 | const fileInfo = (config = {}) => {
|
54 | const defaultConfig = {
|
55 | format: {},
|
56 | preset: METRO,
|
57 | render: o => `${dateTime()} ${decoSizeInfoObject(o)}`,
|
58 | showGzipped: true,
|
59 | showBrotli: false,
|
60 | showMinified: true
|
61 | };
|
62 | config = Object.assign(defaultConfig, config);
|
63 | return {
|
64 | name: 'fileInfo',
|
65 | generateBundle(options, bundle, isWrite) {
|
66 | Object
|
67 | .values(bundle)
|
68 | .filter(({ type }) => type !== 'asset')
|
69 | .forEach(async subBundle => console.log(await sizeInfo(subBundle, config)));
|
70 | }
|
71 | }
|
72 | };
|
73 |
|
74 | export { fileInfo };
|