-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (115 loc) · 3.49 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
// @flow
'use strict';
const webpackBundleSizeAnalyzer = require('webpack-bundle-size-analyzer');
const prettyBytes = require('pretty-bytes');
const spawndamnit = require('spawndamnit');
const promisify = require('util.promisify');
const pLimit = require('p-limit');
const mkdirp = require('mkdirp');
const chalk = require('chalk');
const tempy = require('tempy');
const zlib = require('zlib');
const path = require('path');
const bolt = require('bolt');
const os = require('os');
const fs = require('fs');
const ensureDir = promisify(mkdirp);
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const gzip = promisify(zlib.gzip);
const WEBPACK_BIN = require.resolve('webpack-cli/bin/webpack');
/*::
type Opts = {
output?: string,
json?: boolean,
webpackArgs?: Array<string>,
only?: string,
ignore?: string,
onlyFs?: string,
ignoreFs?: string,
cwd?: string,
concurrency?: number,
continueOnError?: boolean,
};
*/
async function boltWebpackStats(opts /*: Opts | void */) {
opts = opts || {};
let cwd = opts.cwd || process.cwd();
let output = opts.output || false;
let tempDir = tempy.directory();
let webpackArgs = opts.webpackArgs || [];
let concurrency = opts.concurrency || os.cpus().length;
let continueOnError = opts.continueOnError || false;
let json = typeof opts.json === 'undefined' ? true : opts.json;
let log = json ? console.error : console.log;
if (!webpackArgs.length) {
webpackArgs.push('--mode', 'production');
}
let limit = pLimit(concurrency);
let files = [];
let stats = { packages: [] };
let error = null;
let packages = await bolt.getWorkspaces({
cwd,
only: opts.only,
ignore: opts.ignore,
onlyFs: opts.onlyFs,
ignoreFs: opts.ignoreFs,
});
await Promise.all(packages.map(async pkg => {
let args = [...webpackArgs];
args.push('--output-path', tempDir);
args.push('--output-filename', pkg.name + '.js');
args.push('--json');
await limit(async () => {
let res;
try {
log(chalk.cyan(`Building ${pkg.name}...`));
res = await spawndamnit(WEBPACK_BIN, args, { cwd: pkg.dir });
} catch (err) {
if (continueOnError) {
log(chalk.red(`Errored in ${pkg.name}!`));
if (!error) error = err;
return;
} else {
throw err;
}
}
let fileContents = await readFile(path.join(tempDir, pkg.name + '.js'));
let fileSize = Buffer.byteLength(fileContents);
let gzipSize = Buffer.byteLength(await gzip(fileContents, { level: 9 }));
let bundleStatsJson = res.stdout.toString();
let bundleStats = JSON.parse(bundleStatsJson);
let depTrees = webpackBundleSizeAnalyzer.dependencySizeTree(bundleStats);
stats.packages.push({
pkgName: pkg.name,
fileSize,
gzipSize,
bundleStats,
depTrees,
});
});
}));
if (json) {
console.log(JSON.stringify(stats, null, 2));
}
if (output) {
await ensureDir(path.dirname(output));
await writeFile(output, JSON.stringify(stats, null, 2));
}
log();
log(chalk.bgMagenta(' Package Stats: '));
for (let pkg of stats.packages) {
log();
log(chalk.yellow.underline(`${pkg.pkgName} (size: ${prettyBytes(pkg.fileSize)}, gzip: ${prettyBytes(pkg.gzipSize)})`));
for (let tree of pkg.depTrees) {
webpackBundleSizeAnalyzer.printDependencySizeTree(tree, true, 0, log);
}
}
log();
if (error) {
throw error;
}
return stats;
}
module.exports = boltWebpackStats;