Skip to content

Commit fa80d56

Browse files
Clinton Woodritz078
authored andcommitted
Output bundle/file names for when code splitting (#40)
* add output bundle/file names for when code splitting * make test pass again
1 parent 40e3d52 commit fa80d56

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/index.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import gzip from "gzip-size";
66
import brotli from "brotli-size";
77
import terser from "terser";
88

9-
function render(opt, outputOptions, sizes) {
9+
function render(opt, outputOptions, info) {
1010
const primaryColor = opt.theme === "dark" ? "green" : "black";
1111
const secondaryColor = opt.theme === "dark" ? "yellow" : "blue";
1212

1313
const title = colors[primaryColor].bold;
1414
const value = colors[secondaryColor];
1515

1616
const values = [
17-
...(outputOptions.file ? [`${title("Destination: ")}${value(outputOptions.file)}`] : []),
18-
...[`${title("Bundle Size: ")} ${value(sizes.bundleSize)}`],
19-
...(sizes.minSize ? [`${title("Minified Size: ")} ${value(sizes.minSize)}`] : []),
20-
...(sizes.gzipSize ? [`${title("Gzipped Size: ")} ${value(sizes.gzipSize)}`] : []),
21-
...(sizes.brotliSize ? [`${title("Brotli size: ")}${value(sizes.brotliSize)}`] : [])
17+
...(outputOptions.file ?
18+
[`${title("Destination: ")}${value(outputOptions.file)}`] :
19+
(info.fileName ? [`${title("Bundle Name: ")} ${value(info.fileName)}`] : [])),
20+
...[`${title("Bundle Size: ")} ${value(info.bundleSize)}`],
21+
...(info.minSize ? [`${title("Minified Size: ")} ${value(info.minSize)}`] : []),
22+
...(info.gzipSize ? [`${title("Gzipped Size: ")} ${value(info.gzipSize)}`] : []),
23+
...(info.brotliSize ? [`${title("Brotli size: ")}${value(info.brotliSize)}`] : [])
2224
];
2325

2426
return boxen(values.join("\n"), { padding: 1 });
@@ -39,25 +41,29 @@ export default function filesize(options = {}, env) {
3941
opts.render = options.render;
4042
}
4143

42-
const getData = function(outputOptions, code) {
43-
const sizes = {};
44-
sizes.bundleSize = fileSize(Buffer.byteLength(code), opts.format);
44+
const getData = function(outputOptions, bundle) {
45+
const { code, fileName } = bundle;
46+
const info = {};
4547

46-
sizes.brotliSize = opts.showBrotliSize
48+
info.fileName = fileName;
49+
50+
info.bundleSize = fileSize(Buffer.byteLength(code), opts.format);
51+
52+
info.brotliSize = opts.showBrotliSize
4753
? fileSize(brotli.sync(code), opts.format)
4854
: "";
4955

5056
if (opts.showMinifiedSize || opts.showGzippedSize) {
5157
const minifiedCode = terser.minify(code).code;
52-
sizes.minSize = opts.showMinifiedSize
58+
info.minSize = opts.showMinifiedSize
5359
? fileSize(minifiedCode.length, opts.format)
5460
: "";
55-
sizes.gzipSize = opts.showGzippedSize
61+
info.gzipSize = opts.showGzippedSize
5662
? fileSize(gzip.sync(minifiedCode), opts.format)
5763
: "";
5864
}
5965

60-
return opts.render(opts, outputOptions, sizes);
66+
return opts.render(opts, outputOptions, info);
6167
};
6268

6369
if (env === "test") {
@@ -71,7 +77,7 @@ export default function filesize(options = {}, env) {
7177
.map(fileName => bundle[fileName])
7278
.filter(currentBundle => !currentBundle.isAsset)
7379
.forEach((currentBundle) => {
74-
console.log(getData(outputOptions, currentBundle.code))
80+
console.log(getData(outputOptions, currentBundle))
7581
});
7682
}
7783
};

test/index.test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import filesize from '../src';
33

44
const x = filesize({}, "test");
55

6-
const code = 'function add(first, second) { return first + second; }';
6+
const bundle = {
7+
fileName: 'bundled-file.js',
8+
code: 'function add(first, second) { return first + second; }',
9+
};
710

811
test('fileSize should return a string', t => {
9-
t.is(typeof x({dest: 'abc.js'}, code), 'string');
12+
t.is(typeof x({dest: 'abc.js'}, bundle), 'string');
1013
});
1114

1215
test('fileSize should return correct string', t => {
13-
t.true(x({dest: 'abc.js'}, code).indexOf('54') >= 0)
16+
t.true(x({dest: 'abc.js'}, bundle).indexOf('54') >= 0)
1417
});
1518

1619
test('fileSize should apply correct template', t => {
@@ -22,6 +25,6 @@ test('fileSize should apply correct template', t => {
2225

2326
const z = filesize(options, "test");
2427
const expected = '49 B';
25-
console.log(z({dest: 'abc.js'}, code))
26-
t.is(z({dest: 'abc.js'}, code), expected)
28+
console.log(z({dest: 'abc.js'}, bundle))
29+
t.is(z({dest: 'abc.js'}, bundle), expected)
2730
});

0 commit comments

Comments
 (0)