-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
191 lines (168 loc) · 4.57 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
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
import { readFile as origReadFile } from "fs";
import { promisify } from "util";
import { dirname, resolve as pathResolve, join } from "path";
import fileSize from "filesize";
import gzip from "gzip-size";
import terser from "terser";
import brotli from "brotli-size";
import pacote from "pacote";
const readFile = promisify(origReadFile);
const isWindows = process.platform === "win32";
function fixWindowsPath(path) {
return path.slice(
// istanbul ignore next
isWindows ? 1 : 0
);
}
// Node should be ok with this, but transpiling
// to `require` doesn't work, so detect Windows
// to remove slash instead
// "file://" +
const thisDirectory = fixWindowsPath(
dirname(
decodeURI(
new URL(
// `import.meta.url` is giving backslashes in Windows currently
// (at least in how it is compiled to CJS) which makes for an
// invalid URL
import.meta.url.replace(/\\/g, "/")
).pathname
)
)
);
export default function filesize(options = {}, env) {
let {
render,
format = {},
theme = "dark",
showBeforeSizes = "none",
showGzippedSize = true,
showBrotliSize = false,
showMinifiedSize = true,
} = options;
const getLoggingData = async function (outputOptions, bundle) {
const { code, fileName } = bundle;
const info = {};
let codeBefore;
if (showBeforeSizes !== "none") {
let file = outputOptions.file || outputOptions.dest;
if (showBeforeSizes !== "build") {
const { name } = await import(join(process.cwd(), "./package.json"));
try {
const output = join(thisDirectory, "../.cache");
const { resolved } = await pacote.extract(`${name}@latest`, output);
const idx = resolved.lastIndexOf(name);
const lastVersion = resolved.slice(
idx + name.length + 1,
-".tgz".length
);
info.lastVersion = lastVersion;
file = pathResolve(output, file);
} catch (err) {
// Package might not exist
console.log(`Package, "${name}", was not found.`);
file = null;
}
}
if (file) {
try {
codeBefore = await readFile(file, "utf8");
} catch (err) {
console.log(`File, "${file}", was not found.`);
// File might not exist
}
}
}
info.fileName = fileName;
info.bundleSize = fileSize(Buffer.byteLength(code), format);
info.brotliSize = showBrotliSize
? fileSize(await brotli(code), format)
: "";
if (showMinifiedSize || showGzippedSize) {
const minifiedCode = (await terser.minify(code)).code;
info.minSize = showMinifiedSize
? fileSize(minifiedCode.length, format)
: "";
info.gzipSize = showGzippedSize
? fileSize(gzip.sync(minifiedCode), format)
: "";
}
if (codeBefore) {
info.bundleSizeBefore = fileSize(Buffer.byteLength(codeBefore), format);
info.brotliSizeBefore = showBrotliSize
? fileSize(await brotli(codeBefore), format)
: "";
if (showMinifiedSize || showGzippedSize) {
const minifiedCode = (await terser.minify(codeBefore)).code;
info.minSizeBefore = showMinifiedSize
? fileSize(minifiedCode.length, format)
: "";
info.gzipSizeBefore = showGzippedSize
? fileSize(gzip.sync(minifiedCode), format)
: "";
}
}
const opts = {
format,
theme,
render,
showBeforeSizes,
showGzippedSize,
showBrotliSize,
showMinifiedSize,
};
if (render) {
console.warn(
"`render` is now deprecated. Please use `reporter` instead."
);
return opts.render(opts, outputOptions, info);
}
const reporters = options.reporter
? Array.isArray(options.reporter)
? options.reporter
: [options.reporter]
: ["boxen"];
return (
await Promise.all(
reporters.map(async (reporter) => {
if (typeof reporter === "string") {
let p;
if (reporter === "boxen") {
p = import(join(thisDirectory, "/reporters/boxen.js"));
} else {
p = import(pathResolve(process.cwd(), reporter));
}
reporter = (await p).default;
}
return reporter(opts, outputOptions, info);
})
)
).join("");
};
if (env === "test") {
return getLoggingData;
}
return {
name: "filesize",
async generateBundle(outputOptions, bundle /* , isWrite */) {
const dataStrs = await Promise.all(
Object.keys(bundle)
.map((fileName) => bundle[fileName])
.filter((currentBundle) => {
if ({}.hasOwnProperty.call(currentBundle, "type")) {
return currentBundle.type !== "asset";
}
return !currentBundle.isAsset;
})
.map((currentBundle) => {
return getLoggingData(outputOptions, currentBundle);
})
);
dataStrs.forEach((str) => {
if (str) {
console.log(str);
}
});
},
};
}