Skip to content

Commit c0e6db1

Browse files
committed
wip
1 parent 028f716 commit c0e6db1

File tree

12 files changed

+428
-248
lines changed

12 files changed

+428
-248
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('../src/index');

apps/bundle-size/index.js

Lines changed: 0 additions & 248 deletions
This file was deleted.

apps/bundle-size/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "bundle-size",
3+
"version": "1.0.0",
4+
"private": true,
5+
"bin": {
6+
"bundle-size": "./bin/bundle-size.js"
7+
},
8+
"dependencies": {
9+
"@babel/core": "^7.10.4",
10+
"ajv": "^6.12.5",
11+
"chalk": "2.4.2",
12+
"cli-table3": "~0.6.0",
13+
"gzip-size": "^6.0.0",
14+
"fs-extra": "^8.1.0",
15+
"terser": "^5.5.1",
16+
"webpack": "5.21.2"
17+
},
18+
"scripts": {
19+
"build": "tsc --noEmit"
20+
}
21+
}

apps/bundle-size/src/build.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { default: chalk } = require('chalk');
2+
const Table = require('cli-table3');
3+
const fs = require('fs-extra');
4+
const glob = require('glob');
5+
const path = require('path');
6+
7+
const buildFixture = require('./utils/buildFixture');
8+
const { hrToSeconds } = require('./utils/helpers');
9+
const prepareFixture = require('./utils/prepareFixture');
10+
11+
/**
12+
* @param {{ verbose: boolean }} options
13+
*/
14+
module.exports = async function build(options) {
15+
const { verbose } = options;
16+
17+
const startTime = process.hrtime();
18+
const artifactsDir = path.resolve(process.cwd(), 'dist', 'bundle-size');
19+
20+
await fs.remove(artifactsDir);
21+
22+
if (verbose) {
23+
console.log(`${chalk.blue('[i]')} artifacts dir is cleared`);
24+
}
25+
26+
const fixtures = glob.sync('bundle-size/*.fixture.js', {
27+
cwd: process.cwd(),
28+
});
29+
30+
if (verbose) {
31+
console.log(`${chalk.blue('[i]')} Measuring bundle size for ${fixtures.length} fixture(s)...`);
32+
console.log(fixtures.map(fixture => ` - ${fixture}`).join('\n'));
33+
}
34+
35+
const preparedFixtures = await Promise.all(fixtures.map(prepareFixture));
36+
const measurements = [];
37+
38+
for (const preparedFixture of preparedFixtures) {
39+
measurements.push(await buildFixture(preparedFixture, verbose));
40+
}
41+
42+
measurements.sort((a, b) => a.path.localeCompare(b.path));
43+
44+
await fs.writeJSON(path.resolve(process.cwd(), 'dist', 'bundle-size', 'bundle-size.json'), measurements);
45+
46+
if (verbose) {
47+
const table = new Table({
48+
head: ['Fixture', 'Minified size', 'GZIP size'],
49+
});
50+
51+
measurements.forEach(r => {
52+
table.push([r.name, r.minifiedSize, r.gzippedSize]);
53+
});
54+
55+
console.log(table.toString());
56+
}
57+
58+
console.log(`Completed in ${hrToSeconds(process.hrtime(startTime))}`);
59+
};

0 commit comments

Comments
 (0)