UNPKG

5.82 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5function _interopNamespace(e) {
6 if (e && e.__esModule) { return e; } else {
7 var n = {};
8 if (e) {
9 Object.keys(e).forEach(function (k) {
10 var d = Object.getOwnPropertyDescriptor(e, k);
11 Object.defineProperty(n, k, d.get ? d : {
12 enumerable: true,
13 get: function () {
14 return e[k];
15 }
16 });
17 });
18 }
19 n['default'] = e;
20 return n;
21 }
22}
23
24var fs = require('fs');
25var util = require('util');
26var path = require('path');
27var fileSize = _interopDefault(require('filesize'));
28var gzip = _interopDefault(require('gzip-size'));
29var terser = _interopDefault(require('terser'));
30var brotli = _interopDefault(require('brotli-size'));
31var pacote = _interopDefault(require('pacote'));
32
33const readFile = util.promisify(fs.readFile);
34const isWindows = process.platform === "win32";
35
36function fixWindowsPath(path) {
37 return path.slice( // istanbul ignore next
38 isWindows ? 1 : 0);
39} // Node should be ok with this, but transpiling
40// to `require` doesn't work, so detect Windows
41// to remove slash instead
42// "file://" +
43
44
45const thisDirectory = fixWindowsPath(path.dirname(new URL( // `import.meta.url` is giving backslashes in Windows currently
46// (at least in how it is compiled to CJS) which makes for an
47// invalid URL
48(typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.js', document.baseURI).href)).replace(/\\/g, "/")).pathname));
49function filesize(options = {}, env) {
50 let {
51 render,
52 format = {},
53 theme = "dark",
54 showBeforeSizes = "none",
55 showGzippedSize = true,
56 showBrotliSize = false,
57 showMinifiedSize = true
58 } = options;
59
60 const getLoggingData = async function (outputOptions, bundle) {
61 const {
62 code,
63 fileName
64 } = bundle;
65 const info = {};
66 let codeBefore;
67
68 if (showBeforeSizes !== "none") {
69 let file = outputOptions.file || outputOptions.dest;
70
71 if (showBeforeSizes !== "build") {
72 const {
73 name
74 } = await Promise.resolve().then(function () { return _interopNamespace(require(path.join(process.cwd(), "./package.json"))); });
75
76 try {
77 const output = path.join(thisDirectory, "../.cache");
78 const {
79 resolved
80 } = await pacote.extract(`${name}@latest`, output);
81 const idx = resolved.lastIndexOf(name);
82 const lastVersion = resolved.slice(idx + name.length + 1, -".tgz".length);
83 info.lastVersion = lastVersion;
84 file = path.resolve(output, file);
85 } catch (err) {
86 // Package might not exist
87 console.log(`Package, "${name}", was not found.`);
88 file = null;
89 }
90 }
91
92 if (file) {
93 try {
94 codeBefore = await readFile(file, "utf8");
95 } catch (err) {
96 console.log(`File, "${file}", was not found.`); // File might not exist
97 }
98 }
99 }
100
101 info.fileName = fileName;
102 info.bundleSize = fileSize(Buffer.byteLength(code), format);
103 info.brotliSize = showBrotliSize ? fileSize(await brotli(code), format) : "";
104
105 if (showMinifiedSize || showGzippedSize) {
106 const minifiedCode = (await terser.minify(code)).code;
107 info.minSize = showMinifiedSize ? fileSize(minifiedCode.length, format) : "";
108 info.gzipSize = showGzippedSize ? fileSize(gzip.sync(minifiedCode), format) : "";
109 }
110
111 if (codeBefore) {
112 info.bundleSizeBefore = fileSize(Buffer.byteLength(codeBefore), format);
113 info.brotliSizeBefore = showBrotliSize ? fileSize(await brotli(codeBefore), format) : "";
114
115 if (showMinifiedSize || showGzippedSize) {
116 const minifiedCode = (await terser.minify(codeBefore)).code;
117 info.minSizeBefore = showMinifiedSize ? fileSize(minifiedCode.length, format) : "";
118 info.gzipSizeBefore = showGzippedSize ? fileSize(gzip.sync(minifiedCode), format) : "";
119 }
120 }
121
122 const opts = {
123 format,
124 theme,
125 render,
126 showBeforeSizes,
127 showGzippedSize,
128 showBrotliSize,
129 showMinifiedSize
130 };
131
132 if (render) {
133 console.warn("`render` is now deprecated. Please use `reporter` instead.");
134 return opts.render(opts, outputOptions, info);
135 }
136
137 const reporters = options.reporter ? Array.isArray(options.reporter) ? options.reporter : [options.reporter] : ["boxen"];
138 return (await Promise.all(reporters.map(async reporter => {
139 if (typeof reporter === "string") {
140 let p;
141
142 if (reporter === "boxen") {
143 p = Promise.resolve().then(function () { return _interopNamespace(require(path.join(thisDirectory, "/reporters/boxen.js"))); });
144 } else {
145 p = Promise.resolve().then(function () { return _interopNamespace(require(path.resolve(process.cwd(), reporter))); });
146 }
147
148 reporter = (await p).default;
149 }
150
151 return reporter(opts, outputOptions, info);
152 }))).join("");
153 };
154
155 if (env === "test") {
156 return getLoggingData;
157 }
158
159 return {
160 name: "filesize",
161
162 async generateBundle(outputOptions, bundle
163 /* , isWrite */
164 ) {
165 const dataStrs = await Promise.all(Object.keys(bundle).map(fileName => bundle[fileName]).filter(currentBundle => {
166 if ({}.hasOwnProperty.call(currentBundle, "type")) {
167 return currentBundle.type !== "asset";
168 }
169
170 return !currentBundle.isAsset;
171 }).map(currentBundle => {
172 return getLoggingData(outputOptions, currentBundle);
173 }));
174 dataStrs.forEach(str => {
175 if (str) {
176 console.log(str);
177 }
178 });
179 }
180
181 };
182}
183
184module.exports = filesize;
185//# sourceMappingURL=index.js.map