Skip to content

Commit 9291eef

Browse files
committed
add analyzer options and config extension
1 parent e758c55 commit 9291eef

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/classes/SimpleWebpack.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const path = require( 'path' );
55
const webpack = require( 'webpack' );
66
const ESLintPlugin = require( 'eslint-webpack-plugin' );
7+
const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' ).BundleAnalyzerPlugin;
78
const { Exception, Timer, FsInterface, isPojo } = require( '@squirrel-forge/node-util' );
89

910
/**
@@ -72,6 +73,14 @@ class SimpleWebpack {
7273
* @type {boolean}
7374
*/
7475
this.production = process.env.NODE_ENV === 'production';
76+
77+
/**
78+
* Analyzer options
79+
* @public
80+
* @property
81+
* @type {Object}
82+
*/
83+
this.analyzer = null;
7584
}
7685

7786
/**
@@ -253,6 +262,31 @@ class SimpleWebpack {
253262
optimization : { minimize : this.production },
254263
};
255264

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+
256290
// Custom extend
257291
if ( typeof options.extend === 'function' ) {
258292
options.extend( config, source, target, this );
@@ -320,7 +354,7 @@ class SimpleWebpack {
320354
*/
321355
_compile( config, statsOptions = null ) {
322356
return new Promise( ( resolve ) => {
323-
webpack( config, ( err, stats ) => {
357+
webpack( config, async function _webpack_complete( err, stats ) {
324358
if ( err || stats.hasErrors() ) {
325359
if ( err ) {
326360
resolve( new SimpleWebpackException( 'Compile error', err ) );

src/cli.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module.exports = async function cli() {
3535
// Show more output
3636
stats : [ '-s', '--stats', false, true ],
3737

38+
// Use analyzer
39+
analyze : [ '-a', '--analyze', null, false ],
40+
3841
// Show more output
3942
verbose : [ '-i', '--verbose', false, true ],
4043

@@ -136,6 +139,29 @@ module.exports = async function cli() {
136139
swp.production = false;
137140
}
138141

142+
// Get analyze as boolean flag if empty
143+
if ( !options.analyze ) {
144+
const ana = input.getFlagsOptions( { analyze : [ '-a', '--analyze', false, true ] } );
145+
146+
// Enable all if set as boolean flag
147+
if ( ana.analyze ) {
148+
options.analyze = 'static';
149+
}
150+
}
151+
152+
// Enable bundle analyzer
153+
if ( options.analyze ) {
154+
if ( options.analyze === 'server' ) {
155+
cfx.error( 'WebpackBundleAnalyzer.analyzerMode = "server" is currently not supported' );
156+
process.exit( 1 );
157+
}
158+
swp.analyzer = {
159+
analyzerMode : options.analyze,
160+
generateStatsFile : options.stats,
161+
defaultSizes : 'gzip',
162+
};
163+
}
164+
139165
// Index source mode
140166
if ( options.index ) {
141167
swpOptions.source = 'index';

0 commit comments

Comments
 (0)