@@ -9,17 +9,18 @@ const chalk = require('chalk');
9
9
const tempy = require ( 'tempy' ) ;
10
10
const path = require ( 'path' ) ;
11
11
const bolt = require ( 'bolt' ) ;
12
- const fs = require ( 'fs' ) ;
13
12
const os = require ( 'os' ) ;
13
+ const fs = require ( 'fs' ) ;
14
14
15
- const writeFile = promisify ( fs . writeFile ) ;
16
15
const ensureDir = promisify ( mkdirp ) ;
16
+ const writeFile = promisify ( fs . writeFile ) ;
17
17
18
18
const WEBPACK_BIN = require . resolve ( 'webpack-cli/bin/webpack' ) ;
19
19
20
20
/*::
21
21
type Opts = {
22
- outputDir?: string,
22
+ output?: string,
23
+ json?: boolean,
23
24
webpackArgs?: Array<string>,
24
25
only?: string,
25
26
ignore?: string,
@@ -31,74 +32,88 @@ type Opts = {
31
32
};
32
33
*/
33
34
34
- function boltWebpackStats ( opts /*: Opts | void */ ) {
35
+ async function boltWebpackStats ( opts /*: Opts | void */ ) {
35
36
opts = opts || { } ;
36
37
37
38
let cwd = opts . cwd || process . cwd ( ) ;
38
- let outputDir = path . resolve ( cwd , opts . outputDir || './webpack-stats' ) ;
39
+ let output = opts . output || false ;
39
40
let tempDir = tempy . directory ( ) ;
40
41
let webpackArgs = opts . webpackArgs || [ ] ;
41
42
let concurrency = opts . concurrency || os . cpus ( ) . length ;
42
43
let continueOnError = opts . continueOnError || false ;
44
+ let json = typeof opts . json === 'undefined' ? true : opts . json ;
43
45
44
46
if ( ! webpackArgs . length ) {
45
47
webpackArgs . push ( '--mode' , 'production' ) ;
46
48
}
47
49
48
50
let limit = pLimit ( concurrency ) ;
51
+ let files = [ ] ;
52
+ let stats = { packages : [ ] } ;
53
+ let error = null ;
49
54
50
- return bolt . getWorkspaces ( {
55
+ let packages = await bolt . getWorkspaces ( {
51
56
cwd,
52
57
only : opts . only ,
53
58
ignore : opts . ignore ,
54
59
onlyFs : opts . onlyFs ,
55
60
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 ) ;
77
93
} ) ;
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,
99
99
} ) ;
100
100
} ) ;
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 ;
102
117
}
103
118
104
119
module . exports = boltWebpackStats ;
0 commit comments