@@ -43,8 +43,8 @@ const defaults = {
43
43
brotli : false ,
44
44
pattern : '**/*.{mjs,js,jsx,css,html}' ,
45
45
exclude : undefined ,
46
- writeFile :true ,
47
- publish :false ,
46
+ writeFile : true ,
47
+ publish : false ,
48
48
columnWidth : 20
49
49
} ;
50
50
/**
@@ -71,14 +71,19 @@ function bundleSize(_options) {
71
71
initialSizes = await load ( path . resolve ( outputOptions . dir ) ) ;
72
72
outputSizes ( bundle ) . catch ( console . error ) ;
73
73
}
74
- function filterFiles ( files ) {
75
- const isMatched = minimatch . filter ( pattern ) ;
76
- const isExcluded = exclude ? minimatch . filter ( exclude ) : ( ) => false ;
77
- return files . filter ( file => isMatched ( file ) && ! isExcluded ( file ) ) ;
74
+
75
+ async function load ( outputPath ) {
76
+ const data = await readFromDisk ( filename ) ;
77
+ if ( data . length ) {
78
+ const [ { files } ] = data ;
79
+ return toFileMap ( files ) ;
80
+ }
81
+ return getSizes ( outputPath ) ;
78
82
}
83
+
79
84
async function readFromDisk ( filename ) {
80
85
try {
81
- if ( options . writeFile ) {
86
+ if ( options . writeFile ) {
82
87
return [ ] ;
83
88
}
84
89
await fs . ensureFile ( filename ) ;
@@ -88,20 +93,40 @@ function bundleSize(_options) {
88
93
return [ ] ;
89
94
}
90
95
}
96
+
97
+ async function getSizes ( cwd ) {
98
+ const files = await glob ( pattern , { cwd, ignore : exclude } ) ;
99
+
100
+ const sizes = await Promise . all (
101
+ filterFiles ( files ) . map ( file =>
102
+ compressionSize . file ( path . join ( cwd , file ) ) . catch ( ( ) => null )
103
+ )
104
+ ) ;
105
+
106
+ return toMap ( files , sizes ) ;
107
+ }
108
+
109
+ function filterFiles ( files ) {
110
+ const isMatched = minimatch . filter ( pattern ) ;
111
+ const isExcluded = exclude ? minimatch . filter ( exclude ) : ( ) => false ;
112
+ return files . filter ( file => isMatched ( file ) && ! isExcluded ( file ) ) ;
113
+ }
114
+
91
115
async function writeToDisk ( filename , stats ) {
92
116
if (
93
117
process . env . NODE_ENV === 'production' &&
94
118
stats . files . some ( file => file . diff !== 0 )
95
119
) {
96
120
const data = await readFromDisk ( filename ) ;
97
121
data . unshift ( stats ) ;
98
- if ( options . writeFile ) {
122
+ if ( options . writeFile ) {
99
123
await fs . ensureFile ( filename ) ;
100
124
await fs . writeJSON ( filename , data ) ;
101
125
}
102
- options . publish && await publishSizes ( data , options . filename ) ;
126
+ options . publish && ( await publishSizes ( data , options . filename ) ) ;
103
127
}
104
128
}
129
+
105
130
async function save ( files ) {
106
131
const stats = {
107
132
timestamp : Date . now ( ) ,
@@ -112,19 +137,11 @@ function bundleSize(_options) {
112
137
diff : file . size - file . sizeBefore
113
138
} ) )
114
139
} ;
115
- options . publish && await publishDiff ( stats , options . filename ) ;
140
+ options . publish && ( await publishDiff ( stats , options . filename ) ) ;
116
141
options . save && ( await options . save ( stats ) ) ;
117
142
await writeToDisk ( filename , stats ) ;
118
143
}
119
- async function load ( outputPath ) {
120
144
121
- const data = await readFromDisk ( filename ) ;
122
- if ( data . length ) {
123
- const [ { files } ] = data ;
124
- return toFileMap ( files ) ;
125
- }
126
- return getSizes ( outputPath ) ;
127
- }
128
145
async function outputSizes ( assets ) {
129
146
const sizesBefore = await Promise . resolve ( initialSizes ) ;
130
147
const assetNames = filterFiles ( Object . keys ( assets ) ) ;
@@ -190,18 +207,6 @@ function bundleSize(_options) {
190
207
output && console . log ( '\n' + output ) ;
191
208
}
192
209
193
- async function getSizes ( cwd ) {
194
- const files = await glob ( pattern , { cwd, ignore : exclude } ) ;
195
-
196
- const sizes = await Promise . all (
197
- filterFiles ( files ) . map ( file =>
198
- compressionSize . file ( path . join ( cwd , file ) ) . catch ( ( ) => null )
199
- )
200
- ) ;
201
-
202
- return toMap ( files , sizes ) ;
203
- }
204
-
205
210
return {
206
211
name : 'rollup-plugin-size' ,
207
212
generateBundle
0 commit comments