|
4 | 4 | const path = require( 'path' ); |
5 | 5 | const webpack = require( 'webpack' ); |
6 | 6 | const ESLintPlugin = require( 'eslint-webpack-plugin' ); |
| 7 | +const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' ).BundleAnalyzerPlugin; |
7 | 8 | const { Exception, Timer, FsInterface, isPojo } = require( '@squirrel-forge/node-util' ); |
8 | 9 |
|
9 | 10 | /** |
@@ -72,6 +73,14 @@ class SimpleWebpack { |
72 | 73 | * @type {boolean} |
73 | 74 | */ |
74 | 75 | this.production = process.env.NODE_ENV === 'production'; |
| 76 | + |
| 77 | + /** |
| 78 | + * Analyzer options |
| 79 | + * @public |
| 80 | + * @property |
| 81 | + * @type {Object} |
| 82 | + */ |
| 83 | + this.analyzer = null; |
75 | 84 | } |
76 | 85 |
|
77 | 86 | /** |
@@ -253,6 +262,31 @@ class SimpleWebpack { |
253 | 262 | optimization : { minimize : this.production }, |
254 | 263 | }; |
255 | 264 |
|
| 265 | + // Add analyzer plugin if options available |
| 266 | + if ( this.analyzer ) { |
| 267 | + |
| 268 | + // If we have true but invalid analyzer options |
| 269 | + if ( !isPojo( this.analyzer ) ) { |
| 270 | + |
| 271 | + // Error if not a boolean in strict mode |
| 272 | + if ( this.analyzer !== true ) { |
| 273 | + this.error( new SimpleWebpackException( 'Invalid analyzer config' ), false ); |
| 274 | + } |
| 275 | + |
| 276 | + // Set default config |
| 277 | + this.analyzer = { |
| 278 | + analyzerMode : 'static', |
| 279 | + defaultSizes : 'gzip', |
| 280 | + }; |
| 281 | + } |
| 282 | + |
| 283 | + // Always force silent if in strict mode |
| 284 | + if ( this.strict ) { |
| 285 | + this.analyzer.logLevel = 'silent'; |
| 286 | + } |
| 287 | + config.plugins.push( new BundleAnalyzerPlugin( this.analyzer ) ); |
| 288 | + } |
| 289 | + |
256 | 290 | // Custom extend |
257 | 291 | if ( typeof options.extend === 'function' ) { |
258 | 292 | options.extend( config, source, target, this ); |
@@ -320,7 +354,7 @@ class SimpleWebpack { |
320 | 354 | */ |
321 | 355 | _compile( config, statsOptions = null ) { |
322 | 356 | return new Promise( ( resolve ) => { |
323 | | - webpack( config, ( err, stats ) => { |
| 357 | + webpack( config, async function _webpack_complete( err, stats ) { |
324 | 358 | if ( err || stats.hasErrors() ) { |
325 | 359 | if ( err ) { |
326 | 360 | resolve( new SimpleWebpackException( 'Compile error', err ) ); |
|
0 commit comments