Skip to content

Commit d3d9b91

Browse files
committed
Change output to giant object
1 parent 079672c commit d3d9b91

File tree

5 files changed

+95
-64
lines changed

5 files changed

+95
-64
lines changed

README.md

+3-12
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,13 @@ yarn add --dev bolt-webpack-stats
1414
bolt-webpack-stats
1515
```
1616

17-
This will create a directory `./webpack-stats` relative to your `cwd` which has
18-
files like this:
17+
To write stats to a file, you can run:
1918

2019
```sh
21-
/webpack-stats/
22-
package-name-1.json
23-
package-name-2.json
24-
package-name-3.json
25-
/@maybe-npm-scope/
26-
package-name-4.json
27-
package-name-5.json
28-
package-name-6.json
20+
bolt-webpack-stats -o stats.json
21+
bolt-webpack-stats > stats.json
2922
```
3023

31-
These will match up with all of your package names.
32-
3324
You can also specify whatever Webpack flags you want to the `webpack` CLI by
3425
passing them in with `--`:
3526

cli.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,34 @@ const boltWebpackStats = require('./');
1111
let cli = meow({
1212
help: `
1313
Usage
14-
$ bolt-webpack-stats [path/to/output] <...flags> [-- <...webpack-cli-flags>]
14+
$ bolt-webpack-stats <...flags> [-- <...webpack-cli-flags>]
1515
1616
Flags
17+
--output, -o [file name] Write JSON out to a file
1718
--only [name glob] Filter workspaces by name
1819
--ignore [name glob] Filter workspaces out by name
1920
--only-fs [file glob] Filter workspaces by file path
2021
--ignore-fs [file glob] Filter workspaces out by file path
2122
--concurrency [number] Number of Webpack processes to run at once (Default: # of CPUs)
2223
--continue-on-error Continue running when there are errors in some workspaces
24+
--json Return JSON stdout (default when not TTY)
2325
2426
Examples
2527
Get Webpack stats for all your workspaces
2628
$ bolt-webpack-stats
2729
30+
Write all stats information to a file
31+
$ bolt-webpack-stats > stats.json
32+
$ bolt-webpack-stats --output stats.json
33+
2834
Only get Webpack stats for workspaces in a sub directory:
2935
$ bolt-webpack-stats --only-fs "packages/frontend/**"
3036
3137
Specify your own Webpack config file:
3238
$ bolt-webpack-stats -- --config $(pwd)/configs/webpack/production.config.js
3339
`,
3440
flags: {
41+
output: { type: 'string', alias: 'o' },
3542
only: { type: 'string' },
3643
onlyFs: { type: 'string' },
3744
ignore: { type: 'string' },
@@ -42,8 +49,17 @@ let cli = meow({
4249
},
4350
});
4451

52+
let json;
53+
54+
if (typeof cli.flags.json === 'boolean') {
55+
json = cli.flags.json;
56+
} else {
57+
json = !('isTTY' in process.stdout);
58+
}
59+
4560
boltWebpackStats({
46-
outputDir: cli.input[0],
61+
output: cli.flags.output,
62+
json,
4763
only: cli.flags.only,
4864
ignore: cli.flags.ignore,
4965
onlyFs: cli.flags.onlyFs,

index.js

+64-49
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ const chalk = require('chalk');
99
const tempy = require('tempy');
1010
const path = require('path');
1111
const bolt = require('bolt');
12-
const fs = require('fs');
1312
const os = require('os');
13+
const fs = require('fs');
1414

15-
const writeFile = promisify(fs.writeFile);
1615
const ensureDir = promisify(mkdirp);
16+
const writeFile = promisify(fs.writeFile);
1717

1818
const WEBPACK_BIN = require.resolve('webpack-cli/bin/webpack');
1919

2020
/*::
2121
type Opts = {
22-
outputDir?: string,
22+
output?: string,
23+
json?: boolean,
2324
webpackArgs?: Array<string>,
2425
only?: string,
2526
ignore?: string,
@@ -31,74 +32,88 @@ type Opts = {
3132
};
3233
*/
3334

34-
function boltWebpackStats(opts /*: Opts | void */) {
35+
async function boltWebpackStats(opts /*: Opts | void */) {
3536
opts = opts || {};
3637

3738
let cwd = opts.cwd || process.cwd();
38-
let outputDir = path.resolve(cwd, opts.outputDir || './webpack-stats');
39+
let output = opts.output || false;
3940
let tempDir = tempy.directory();
4041
let webpackArgs = opts.webpackArgs || [];
4142
let concurrency = opts.concurrency || os.cpus().length;
4243
let continueOnError = opts.continueOnError || false;
44+
let json = typeof opts.json === 'undefined' ? true : opts.json;
4345

4446
if (!webpackArgs.length) {
4547
webpackArgs.push('--mode', 'production');
4648
}
4749

4850
let limit = pLimit(concurrency);
51+
let files = [];
52+
let stats = { packages: [] };
53+
let error = null;
4954

50-
return bolt.getWorkspaces({
55+
let packages = await bolt.getWorkspaces({
5156
cwd,
5257
only: opts.only,
5358
ignore: opts.ignore,
5459
onlyFs: opts.onlyFs,
5560
ignoreFs: opts.ignoreFs,
56-
}).then(packages => {
57-
return Promise.all(packages.map(pkg => {
58-
let args = [...webpackArgs];
59-
60-
args.push('--output-path', tempDir);
61-
args.push('--output-filename', pkg.name + '.js');
62-
args.push('--json');
63-
64-
return limit(() => {
65-
console.log(chalk.cyan(`Building ${pkg.name}...`));
66-
67-
return spawndamnit(WEBPACK_BIN, args, { cwd: pkg.dir }).then(res => {
68-
return { name: pkg.name, stdout: res.stdout };
69-
}).catch(err => {
70-
if (continueOnError) {
71-
console.error(chalk.red(`Errored in ${pkg.name}!`));
72-
return { name: pkg.name, error: err };
73-
} else {
74-
throw err;
75-
}
76-
});
61+
});
62+
63+
await Promise.all(packages.map(async pkg => {
64+
let args = [...webpackArgs];
65+
66+
args.push('--output-path', tempDir);
67+
args.push('--output-filename', pkg.name + '.js');
68+
args.push('--json');
69+
70+
await limit(async () => {
71+
let res;
72+
73+
try {
74+
console.error(chalk.cyan(`Building ${pkg.name}...`));
75+
res = await spawndamnit(WEBPACK_BIN, args, { cwd: pkg.dir });
76+
} catch (err) {
77+
if (continueOnError) {
78+
console.error(chalk.red(`Errored in ${pkg.name}!`));
79+
if (!error) error = err;
80+
return;
81+
} else {
82+
throw err;
83+
}
84+
}
85+
86+
let bundleStatsJson = res.stdout.toString();
87+
let bundleStats = JSON.parse(bundleStatsJson);
88+
let depTrees = webpackBundleSizeAnalyzer.dependencySizeTree(bundleStats);
89+
90+
depTrees.forEach(tree => {
91+
console.error(chalk.yellow.bold.underline(pkg.name));
92+
webpackBundleSizeAnalyzer.printDependencySizeTree(tree, true, 0, console.error);
7793
});
78-
})).then(results => {
79-
let error = null;
80-
81-
return ensureDir(outputDir).then(() => {
82-
return Promise.all(results.map((res, index) => {
83-
if (!res.error) {
84-
return writeFile(path.resolve(outputDir, res.name + '.json'), res.stdout).then(() => {
85-
let bundleStats = JSON.parse(res.stdout.toString());
86-
let depTrees = webpackBundleSizeAnalyzer.dependencySizeTree(bundleStats);
87-
88-
depTrees.forEach(tree => {
89-
console.log(chalk.yellow.bold.underline(res.name));
90-
webpackBundleSizeAnalyzer.printDependencySizeTree(tree, true);
91-
});
92-
});
93-
} else if (!error) {
94-
error = res.error;
95-
}
96-
}))
97-
}).then(() => {
98-
if (error) throw error;
94+
95+
stats.packages.push({
96+
pkgName: pkg.name,
97+
bundleStats,
98+
depTrees,
9999
});
100100
});
101-
});
101+
}));
102+
103+
if (json) {
104+
console.log(JSON.stringify(stats, null, 2));
105+
}
106+
107+
if (output) {
108+
await ensureDir(path.dirname(output));
109+
await writeFile(output, JSON.stringify(stats, null, 2));
110+
}
111+
112+
if (error) {
113+
throw error;
114+
}
115+
116+
return stats;
102117
}
103118

104119
module.exports = boltWebpackStats;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"dependencies": {
1717
"bolt": "^0.20.0",
1818
"chalk": "^2.3.2",
19+
"fs-vacuum": "^1.2.10",
1920
"meow": "^4.0.0",
2021
"p-limit": "^1.2.0",
2122
"spawndamnit": "^1.0.0",

yarn.lock

+9-1
Original file line numberDiff line numberDiff line change
@@ -2322,6 +2322,14 @@ from2@^2.1.0, from2@^2.1.1:
23222322
inherits "^2.0.1"
23232323
readable-stream "^2.0.0"
23242324

2325+
fs-vacuum@^1.2.10:
2326+
version "1.2.10"
2327+
resolved "https://registry.yarnpkg.com/fs-vacuum/-/fs-vacuum-1.2.10.tgz#b7629bec07a4031a2548fdf99f5ecf1cc8b31e36"
2328+
dependencies:
2329+
graceful-fs "^4.1.2"
2330+
path-is-inside "^1.0.1"
2331+
rimraf "^2.5.2"
2332+
23252333
fs-write-stream-atomic@^1.0.8:
23262334
version "1.0.10"
23272335
resolved "https://registry.yarnpkg.com/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz#b47df53493ef911df75731e70a9ded0189db40c9"
@@ -4733,7 +4741,7 @@ ret@~0.1.10:
47334741
version "0.1.15"
47344742
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
47354743

4736-
rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
4744+
rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
47374745
version "2.6.2"
47384746
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
47394747
dependencies:

0 commit comments

Comments
 (0)