/
index.js
152 lines (128 loc) · 4.32 KB
/
index.js
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
// @flow
import { relative, dirname, join } from "path";
import { minify } from "terser";
import gzipSize from "gzip-size";
import bytes from "bytes";
import chalk from "chalk";
import { treeshakeWithRollup } from "./treeshakeWithRollup.js";
import { treeshakeWithWebpack } from "./treeshakeWithWebpack.js";
import * as snapshot from "./snapshot.js";
type Options = {
snapshotPath?: string,
matchSnapshot?: boolean,
threshold?: number,
printInfo?: boolean
};
type OutputOptions = {
format: string,
file: string
};
type Plugin = {
name: string,
renderChunk: (
code: string,
chunk: mixed,
options: OutputOptions
) => null | Promise<null>
};
const validateOptions = options => {
const optionsKeys: $ReadOnlyArray<$Keys<Options>> = [
"snapshotPath",
"matchSnapshot",
"threshold",
"printInfo"
];
const invalidKeys = Object.keys(options).filter(
d => !optionsKeys.includes(d)
);
const wrap = d => `"${d}"`;
if (1 === invalidKeys.length) {
throw Error(`Option ${wrap(invalidKeys[0])} is invalid`);
}
if (1 < invalidKeys.length) {
throw Error(`Options ${invalidKeys.map(wrap).join(", ")} are invalid`);
}
};
const bytesConfig = { thousandsSeparator: ",", unitSeparator: " ", unit: "B" };
const formatSize = d => chalk.bold(bytes.format(d, bytesConfig));
export const sizeSnapshot = (options?: Options = {}): Plugin => {
validateOptions(options);
const snapshotPath =
options.snapshotPath || join(process.cwd(), ".size-snapshot.json");
const shouldMatchSnapshot = options.matchSnapshot === true;
const shouldPrintInfo = options.printInfo !== false;
const threshold = options.threshold == null ? 0 : options.threshold;
return {
name: "size-snapshot",
renderChunk(rawSource, chunk, outputOptions) {
// remove windows specific newline character
const source = rawSource.replace(/\r/g, "");
const format = outputOptions.format;
const output = outputOptions.file;
const shouldTreeshake = format === "es" || format === "esm";
if (typeof output !== "string") {
throw Error("output file in rollup options should be specified");
}
const outputName = relative(dirname(snapshotPath), output);
const minified = minify(source).code;
const treeshakeSize = code =>
Promise.all([treeshakeWithRollup(code), treeshakeWithWebpack(code)]);
return Promise.all([
gzipSize(minified),
shouldTreeshake
? treeshakeSize(source)
: [{ code: 0, import_statements: 0 }, { code: 0 }]
]).then(([gzippedSize, [rollupSize, webpackSize]]) => {
const sizes: Object = {
bundled: source.length,
minified: minified.length,
gzipped: gzippedSize
};
const prettyFormat = format === "es" ? "esm" : format;
const prettyBundled = formatSize(sizes.bundled);
const prettyMinified = formatSize(sizes.minified);
const prettyGzipped = formatSize(sizes.gzipped);
let infoString =
"\n" +
`Computed sizes of "${outputName}" with "${prettyFormat}" format\n` +
` bundler parsing size: ${prettyBundled}\n` +
` browser parsing size (minified with terser): ${prettyMinified}\n` +
` download size (minified and gzipped): ${prettyGzipped}\n`;
const formatMsg = (msg, size) => ` ${msg}: ${formatSize(size)}\n`;
if (shouldTreeshake) {
sizes.treeshaked = {
rollup: rollupSize,
webpack: webpackSize
};
infoString += formatMsg(
"treeshaked with rollup with production NODE_ENV and minified",
rollupSize.code
);
infoString += formatMsg(
" import statements size of it",
rollupSize.import_statements
);
infoString += formatMsg(
"treeshaked with webpack in production mode",
webpackSize.code
);
}
const snapshotParams = {
snapshotPath,
name: outputName,
data: sizes,
threshold
};
if (shouldMatchSnapshot) {
snapshot.match(snapshotParams);
} else {
if (shouldPrintInfo) {
console.info(infoString);
}
snapshot.write(snapshotParams);
}
return null;
});
}
};
};