@@ -260,7 +260,7 @@ function done( error ) {
260260*
261261* @private
262262* @param {string } name - bundle name
263- * @param {boolean } raw - boolean indicating whether to save a raw bundle
263+ * @param {boolean } raw - boolean indicating whether to write a raw bundle to file
264264* @param {boolean } minify - boolean indicating whether to minify a bundle
265265* @returns {Callback } callback to be invoked upon generating a bundle
266266*/
@@ -285,38 +285,68 @@ function clbk( name, raw, minify ) {
285285 debug ( 'Finished creating bundle: %s' , name ) ;
286286 b = bundle ; // parent scope
287287
288+ if ( raw ) {
289+ return writeBundle ( bundle , false ) ;
290+ }
291+ if ( minify ) {
292+ return minifyBundle ( bundle ) ;
293+ }
294+ // NOTE: we assume that: if we are supposed to neither write a "raw" bundle to file nor minify a generated bundle, then the generated bundle was minified during generation.
295+ writeBundle ( bundle , true ) ;
296+ }
297+
298+ /**
299+ * Minifies a bundle and writes the bundle to file.
300+ *
301+ * @private
302+ * @param {(string|Buffer) } bundle - bundle to minify
303+ */
304+ function minifyBundle ( bundle ) {
305+ debug ( 'Minifying...' ) ;
306+ bundle = uglify . minify ( bundle . toString ( ) ) ;
307+ debug ( 'Finished minifying bundle: %s' , name ) ;
308+ debug ( 'Minification errors: %s' , ( bundle . error ) ? bundle . error : '(none)' ) ;
309+ writeBundle ( bundle . code , true ) ;
310+ }
311+
312+ /**
313+ * Writes a bundle to file.
314+ *
315+ * @private
316+ * @param {(string|Buffer) } bundle - bundle to write to file
317+ * @param {boolean } isMinified - boolean indicating whether a bundle is minified
318+ * @returns {void }
319+ */
320+ function writeBundle ( bundle , isMinified ) {
321+ var fpath ;
322+ if ( isMinified ) {
323+ fpath = join ( dir , name + '.min.js' ) ;
324+ debug ( 'Writing minified bundle to file: %s' , fpath ) ;
325+ return writeFile ( fpath , bundle , onWriteMinify ) ;
326+ }
288327 fpath = join ( dir , name + '.js' ) ;
289328 debug ( 'Writing bundle to file: %s' , fpath ) ;
290- writeFile ( fpath , b , onWrite ) ;
329+ writeFile ( fpath , bundle , onWrite ) ;
291330 }
292331
293332 /**
294- * Callback invoked upon writing a bundle to file.
333+ * Callback invoked upon writing an un-minified bundle to file.
295334 *
296335 * @private
297336 * @param {Error } [error] - error object
298337 * @returns {void }
299338 */
300339 function onWrite ( error ) {
301- var bundle ;
302340 var fpath ;
303341 if ( error ) {
304342 debug ( 'Encountered an error when writing bundle to file: %s' , error . message ) ;
305343 return done ( error ) ;
306344 }
307345 debug ( 'Finished writing bundle `%s` to file.' , name ) ;
308- if ( ! minify ) {
309- return done ( ) ;
346+ if ( minify ) {
347+ return minifyBundle ( b ) ;
310348 }
311- debug ( 'Minifying...' ) ;
312- bundle = uglify . minify ( b . toString ( ) ) ;
313- debug ( 'Finished minifying bundle: %s' , name ) ;
314- debug ( 'Minification errors: %s' , ( bundle . error ) ? bundle . error : '(none)' ) ;
315- bundle = bundle . code ;
316-
317- fpath = join ( dir , name + '.min.js' ) ;
318- debug ( 'Writing minified bundle to file: %s' , fpath ) ;
319- writeFile ( fpath , bundle , onWriteMinify ) ;
349+ done ( ) ;
320350 }
321351
322352 /**
0 commit comments