Skip to content

Commit 926de1c

Browse files
authored
Before sizes npm (#68)
* test: use non-deprecated form `file` in an additional test * feat: option to check "before" size according to last npm package size Also applies `rimraf` for cross-platform package script * - Switch `showBeforeSizes` to preferred API
1 parent 24fdf7c commit 926de1c

9 files changed

Lines changed: 777 additions & 51 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ node_modules
3232

3333
.idea
3434
dist
35+
36+
# We use to gather npm packages
37+
.cache

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.cache
12
.nyc_output
23
dist

Readme.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,24 @@ default: false
4848
Whether to show [Brotli](https://www.wikiwand.com/en/Brotli) size or not
4949

5050
#### showBeforeSizes
51-
type: `boolean`
52-
default: false
5351

54-
Whether to show a comparison between the `output.file` file size as it was
55-
and as it is now being written. Useful to compare size of a new release to
56-
the previous one (though note that if you run Rollup multiple times, this
57-
info will be lost, except if still in your terminal history).
52+
type: `"release", ``"build"`, or `"none"`
53+
default: `"none"`
54+
55+
Indicates how, if any, comparisons will be shown between the
56+
`output.file` file size as it was and as it is now being written.
57+
58+
If set to `"release"`, will compare the file size at present to that of
59+
the last npm release.
60+
61+
If set to `"build"`, the size of the file that is now being built will
62+
be compared to the immediately previous build. This means that if you run
63+
Rollup multiple times with this option, the info on the previous package
64+
size will be lost (since Rollup will have overwritten your copy), so with
65+
this option, you will need to consult your terminal history to see what the
66+
file size was prior to your changes. This option may be useful if you wish
67+
to compare size changes incrementally as you are developing rather than
68+
comparing to your last release.
5869

5970
#### format
6071

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"test": "nyc ava test/index.test.js",
1515
"pretest": "rollup -c",
1616
"prepublish": "npm run test",
17-
"prebuild": "rm -rf dist/*"
17+
"prebuild": "rimraf dist/*"
1818
},
1919
"engines": {
2020
"node": ">=8.0.0"
@@ -39,13 +39,15 @@
3939
"colors": "^1.4.0",
4040
"filesize": "^6.1.0",
4141
"gzip-size": "^5.1.1",
42+
"pacote": "^11.1.4",
4243
"terser": "^4.6.11"
4344
},
4445
"devDependencies": {
4546
"@babel/core": "^7.9.0",
4647
"@babel/plugin-syntax-import-meta": "^7.8.3",
4748
"@babel/preset-env": "^7.9.5",
4849
"@babel/register": "^7.9.0",
50+
"@rollup/plugin-json": "^4.0.3",
4951
"ava": "^3.7.1",
5052
"babel-eslint": "^10.1.0",
5153
"babel-register": "^6.26.0",
@@ -55,6 +57,7 @@
5557
"esm": "^3.2.25",
5658
"nyc": "^15.0.1",
5759
"prettier": "^2.0.4",
60+
"rimraf": "^3.0.2",
5861
"rollup": "^2.6.1",
5962
"rollup-plugin-babel": "^4.4.0"
6063
},

rollup.config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json from "@rollup/plugin-json";
12
import babel from "rollup-plugin-babel";
23
import pkg from "./package.json";
34

@@ -19,12 +20,15 @@ export default [
1920
{
2021
external: ["path", "fs", "util", ...Object.keys(pkg.dependencies)],
2122
plugins: [
23+
json(),
2224
babel({
2325
babelrc: false,
2426
plugins: ["@babel/plugin-syntax-import-meta"],
2527
presets: [["@babel/preset-env", { targets: { node: 8 } }]],
2628
}),
27-
filesize(),
29+
filesize({
30+
showBeforeSizes: "release",
31+
}),
2832
],
2933
input: "src/index.js",
3034
output: {
@@ -40,7 +44,9 @@ export default [
4044
babelrc: false,
4145
presets: [["@babel/preset-env", { targets: { node: 8 } }]],
4246
}),
43-
filesize(),
47+
filesize({
48+
showBeforeSizes: "release",
49+
}),
4450
],
4551
input: `src/reporters/${reporter}`,
4652
output: {

src/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import { readFile as origReadFile } from "fs";
1+
import { readFile as origReadFile, existsSync } from "fs";
22
import { promisify } from "util";
3-
import { dirname, resolve as pathResolve } from "path";
3+
import { dirname, resolve as pathResolve, join } from "path";
44

55
import fileSize from "filesize";
66
import gzip from "gzip-size";
77
import terser from "terser";
88
import brotli from "brotli-size";
9+
import pacote from "pacote";
910

1011
const readFile = promisify(origReadFile);
1112

13+
const thisDirectory = dirname(new URL(import.meta.url).pathname);
14+
1215
export default function filesize(options = {}, env) {
1316
let {
1417
render,
1518
format = {},
1619
theme = "dark",
17-
showBeforeSizes = false,
20+
showBeforeSizes = "none",
1821
showGzippedSize = true,
1922
showBrotliSize = false,
2023
showMinifiedSize = true,
@@ -25,14 +28,30 @@ export default function filesize(options = {}, env) {
2528
const info = {};
2629

2730
let codeBefore;
28-
if (showBeforeSizes) {
29-
try {
30-
codeBefore = await readFile(
31-
outputOptions.file || outputOptions.dest,
32-
"utf8"
31+
if (showBeforeSizes !== "none") {
32+
let file = outputOptions.file || outputOptions.dest;
33+
if (showBeforeSizes !== "build") {
34+
const { name, version } = await import(
35+
join(process.cwd(), "./package.json")
3336
);
34-
} catch (err) {
35-
// File might not exist
37+
try {
38+
const output = join(thisDirectory, "../.cache");
39+
if (!existsSync(output)) {
40+
await pacote.extract(`${name}@${version}`, output);
41+
}
42+
file = join(output, file);
43+
} catch (err) {
44+
// Package might not exist
45+
file = null;
46+
}
47+
}
48+
49+
if (file) {
50+
try {
51+
codeBefore = await readFile(file, "utf8");
52+
} catch (err) {
53+
// File might not exist
54+
}
3655
}
3756
}
3857

test/fixtures/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@ritz078/nonexistent-package",
3+
"version": "0.0.0"
4+
}

test/index.test.js

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
import { promisify } from "util";
12
import test from "ava";
3+
import rimRaf from "rimraf";
24
import colors from "colors/safe";
35
import filesize from "../src/index.js";
46

7+
const rimraf = promisify(rimRaf);
8+
59
const x = filesize({}, "test");
610

711
const bundle = {
812
fileName: "test/fixtures/bundled-file.js",
913
code: "function add(first, second) { return first + second; }",
1014
};
1115

16+
async function removeCacheDir() {
17+
return await rimraf(".cache");
18+
}
19+
1220
async function getStdout(cb) {
1321
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
1422

@@ -31,6 +39,10 @@ test("fileSize should return a string", async (t) => {
3139
t.is(typeof (await x({ file: "abc.js" }, bundle)), "string");
3240
});
3341

42+
test("fileSize should return a string (with deprecated `dest`)", async (t) => {
43+
t.is(typeof (await x({ dest: "abc.js" }, bundle)), "string");
44+
});
45+
3446
test("fileSize should allow custom reporter", async (t) => {
3547
const getLoggingData = filesize(
3648
{
@@ -44,7 +56,7 @@ test("fileSize should allow custom reporter", async (t) => {
4456
format: {},
4557
theme: "dark",
4658
// reporter: [AsyncFunction: customReporter],
47-
showBeforeSizes: false,
59+
showBeforeSizes: "none",
4860
showGzippedSize: true,
4961
showBrotliSize: false,
5062
showMinifiedSize: true,
@@ -146,30 +158,80 @@ test("fileSize should generate a bundle", async (t) => {
146158
});
147159

148160
test("fileSize should apply `showBeforeSizes` option", async (t) => {
149-
const getLoggingData = filesize({ showBeforeSizes: true }, "test");
150-
const val = await getLoggingData(
151-
{ file: "./test/fixtures/sample.js" },
152-
bundle
153-
);
161+
const getLoggingData = filesize({ showBeforeSizes: "release" }, "test");
162+
const val = await getLoggingData({ file: "./dist/index.js" }, bundle);
154163
if (colors.supportsColor()) {
155164
// eslint-disable-next-line no-control-regex
156-
t.regex(val, /\(was \u001b\[33m21 B/);
165+
t.regex(val, /\(was \u001b\[33m[\d.]+ KB/);
157166
} else {
158-
t.regex(val, /\(was 21 B/);
167+
t.regex(val, /\(was [\d.]+ KB/);
168+
}
169+
});
170+
171+
test('fileSize should apply `showBeforeSizes` option as "build"', async (t) => {
172+
await removeCacheDir();
173+
174+
const getLoggingData = filesize({ showBeforeSizes: "build" }, "test");
175+
const val = await getLoggingData({ file: "./dist/index.js" }, bundle);
176+
if (colors.supportsColor()) {
177+
// eslint-disable-next-line no-control-regex
178+
t.regex(val, /\(was \u001b\[33m[\d.]+ K?B/);
179+
} else {
180+
t.regex(val, /\(was [\d.]+ K?B/);
181+
}
182+
});
183+
184+
test('fileSize should ignore before sizes with package-missing file and `showBeforeSizes: "release"`', async (t) => {
185+
await removeCacheDir();
186+
187+
let getLoggingData = filesize({ showBeforeSizes: "release" }, "test");
188+
let val = await getLoggingData({ file: "./test/fixtures/sample.js" }, bundle);
189+
if (colors.supportsColor()) {
190+
// eslint-disable-next-line no-control-regex
191+
t.notRegex(val, /\(was /);
192+
} else {
193+
t.notRegex(val, /\(was /);
194+
}
195+
196+
// Run again (without deleting cache directory) to get coverage of cache
197+
getLoggingData = filesize({ showBeforeSizes: "release" }, "test");
198+
val = await getLoggingData({ file: "./test/fixtures/sample.js" }, bundle);
199+
if (colors.supportsColor()) {
200+
// eslint-disable-next-line no-control-regex
201+
t.notRegex(val, /\(was /);
202+
} else {
203+
t.notRegex(val, /\(was /);
204+
}
205+
});
206+
207+
test("fileSize should ignore before sizes with bad package", async (t) => {
208+
await removeCacheDir();
209+
210+
const oldCwd = process.cwd();
211+
const getLoggingData = filesize({ showBeforeSizes: "release" }, "test");
212+
213+
// Change directory without giving a chance for other tests to use
214+
process.chdir("./test/fixtures");
215+
const prom = getLoggingData({ file: "./sample.js" }, bundle);
216+
process.chdir(oldCwd);
217+
const val = await prom;
218+
219+
if (colors.supportsColor()) {
220+
// eslint-disable-next-line no-control-regex
221+
t.notRegex(val, /\(was /);
222+
} else {
223+
t.notRegex(val, /\(was /);
159224
}
160225
});
161226

162227
test("fileSize should apply `showBeforeSizes` option (with deprecated `dest`)", async (t) => {
163-
const getLoggingData = filesize({ showBeforeSizes: true }, "test");
164-
const val = await getLoggingData(
165-
{ dest: "./test/fixtures/sample.js" },
166-
bundle
167-
);
228+
const getLoggingData = filesize({ showBeforeSizes: "release" }, "test");
229+
const val = await getLoggingData({ dest: "./dist/index.js" }, bundle);
168230
if (colors.supportsColor()) {
169231
// eslint-disable-next-line no-control-regex
170-
t.regex(val, /\(was \u001b\[33m21 B/);
232+
t.regex(val, /\(was \u001b\[33m[\d.]+ KB/);
171233
} else {
172-
t.regex(val, /\(was 21 B/);
234+
t.regex(val, /\(was [\d.]+ KB/);
173235
}
174236
});
175237

@@ -232,7 +294,7 @@ test("fileSize should show Brotli size when configured", async (t) => {
232294
test("fileSize should show before Brotli size when configured", async (t) => {
233295
let getLoggingData = filesize(
234296
{
235-
showBeforeSizes: true,
297+
showBeforeSizes: "build",
236298
showBrotliSize: true,
237299
},
238300
"test"
@@ -251,7 +313,7 @@ test("fileSize should show before Brotli size when configured", async (t) => {
251313

252314
getLoggingData = filesize(
253315
{
254-
showBeforeSizes: true,
316+
showBeforeSizes: "build",
255317
showBrotliSize: true,
256318
showMinifiedSize: false,
257319
showGzippedSize: false,
@@ -271,7 +333,7 @@ test("fileSize should show before Brotli size when configured", async (t) => {
271333

272334
getLoggingData = filesize(
273335
{
274-
showBeforeSizes: true,
336+
showBeforeSizes: "build",
275337
showBrotliSize: true,
276338
showMinifiedSize: true,
277339
showGzippedSize: false,
@@ -292,7 +354,7 @@ test("fileSize should show before Brotli size when configured", async (t) => {
292354

293355
getLoggingData = filesize(
294356
{
295-
showBeforeSizes: true,
357+
showBeforeSizes: "build",
296358
showBrotliSize: true,
297359
showMinifiedSize: false,
298360
showGzippedSize: true,

0 commit comments

Comments
 (0)