11'use strict' ;
22
3- var babelHelpers = { } ;
4-
5- babelHelpers . classCallCheck = function ( instance , Constructor ) {
6- if ( ! ( instance instanceof Constructor ) ) {
7- throw new TypeError ( "Cannot call a class as a function" ) ;
8- }
9- } ;
10-
11- babelHelpers . createClass = function ( ) {
12- function defineProperties ( target , props ) {
13- for ( var i = 0 ; i < props . length ; i ++ ) {
14- var descriptor = props [ i ] ;
15- descriptor . enumerable = descriptor . enumerable || false ;
16- descriptor . configurable = true ;
17- if ( "value" in descriptor ) descriptor . writable = true ;
18- Object . defineProperty ( target , descriptor . key , descriptor ) ;
19- }
20- }
21-
22- return function ( Constructor , protoProps , staticProps ) {
23- if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ;
24- if ( staticProps ) defineProperties ( Constructor , staticProps ) ;
25- return Constructor ;
26- } ;
27- } ( ) ;
28-
29- babelHelpers ;
30-
31- var exec = require ( 'child_process' ) . exec ;
32- var defaultOptions = {
3+ const exec = require ( 'child_process' ) . exec ;
4+ const defaultOptions = {
335 onBuildStart : [ ] ,
346 onBuildEnd : [ ] ,
357 onBuildExit : [ ] ,
368 dev : true ,
37- verbose : false
9+ verbose : false ,
10+ throwOnExecError : false
3811} ;
3912
4013function puts ( error , stdout , stderr ) {
@@ -43,6 +16,12 @@ function puts(error, stdout, stderr) {
4316 }
4417}
4518
19+ function putsThrow ( error , stdout , stderr ) {
20+ if ( error ) {
21+ throw error ;
22+ }
23+ }
24+
4625function spreadStdoutAndStdErr ( proc ) {
4726 proc . stdout . pipe ( process . stdout ) ;
4827 proc . stderr . pipe ( process . stdout ) ;
@@ -62,65 +41,70 @@ function validateInput(options) {
6241}
6342
6443function mergeOptions ( options , defaults ) {
65- for ( var key in defaults ) {
44+ for ( const key in defaults ) {
6645 if ( options . hasOwnProperty ( key ) ) {
6746 defaults [ key ] = options [ key ] ;
6847 }
6948 }
7049 return defaults ;
7150}
7251
73- var WebpackShellPlugin = function ( ) {
74- function WebpackShellPlugin ( options ) {
75- babelHelpers . classCallCheck ( this , WebpackShellPlugin ) ;
76-
52+ class WebpackShellPlugin {
53+ constructor ( options ) {
7754 this . options = validateInput ( mergeOptions ( options , defaultOptions ) ) ;
7855 }
7956
80- babelHelpers . createClass ( WebpackShellPlugin , [ {
81- key : 'apply' ,
82- value : function apply ( compiler ) {
83- var _this = this ;
84-
85- compiler . plugin ( 'compilation' , function ( compilation ) {
86- if ( _this . options . verbose ) {
87- console . log ( 'Report compilation: ' + compilation ) ;
88- }
89- if ( _this . options . onBuildStart . length ) {
90- console . log ( 'Executing pre-build scripts' ) ;
91- _this . options . onBuildStart . forEach ( function ( script ) {
57+ apply ( compiler ) {
58+
59+ compiler . plugin ( 'compilation' , compilation => {
60+ if ( this . options . verbose ) {
61+ console . log ( `Report compilation: ${ compilation } ` ) ;
62+ }
63+ if ( this . options . onBuildStart . length ) {
64+ console . log ( 'Executing pre-build scripts' ) ;
65+ this . options . onBuildStart . forEach ( script => {
66+ if ( this . options . throwOnExecError ) {
67+ spreadStdoutAndStdErr ( exec ( script , putsThrow ) ) ;
68+ } else {
9269 spreadStdoutAndStdErr ( exec ( script , puts ) ) ;
93- } ) ;
94- if ( _this . options . dev ) {
95- _this . options . onBuildStart = [ ] ;
9670 }
71+ } ) ;
72+ if ( this . options . dev ) {
73+ this . options . onBuildStart = [ ] ;
9774 }
98- } ) ;
99-
100- compiler . plugin ( 'emit' , function ( compilation , callback ) {
101- if ( _this . options . onBuildEnd . length ) {
102- console . log ( 'Executing post-build scripts' ) ;
103- _this . options . onBuildEnd . forEach ( function ( script ) {
75+ }
76+ } ) ;
77+
78+ compiler . plugin ( 'emit' , ( compilation , callback ) => {
79+ if ( this . options . onBuildEnd . length ) {
80+ console . log ( 'Executing post-build scripts' ) ;
81+ this . options . onBuildEnd . forEach ( script => {
82+ if ( this . options . throwOnExecError ) {
83+ spreadStdoutAndStdErr ( exec ( script , putsThrow ) ) ;
84+ } else {
10485 spreadStdoutAndStdErr ( exec ( script , puts ) ) ;
105- } ) ;
106- if ( _this . options . dev ) {
107- _this . options . onBuildEnd = [ ] ;
10886 }
87+ } ) ;
88+ if ( this . options . dev ) {
89+ this . options . onBuildEnd = [ ] ;
10990 }
110- callback ( ) ;
111- } ) ;
112-
113- compiler . plugin ( 'done' , function ( ) {
114- if ( _this . options . onBuildExit . length ) {
115- console . log ( 'Executing additional scripts before exit' ) ;
116- _this . options . onBuildExit . forEach ( function ( script ) {
91+ }
92+ callback ( ) ;
93+ } ) ;
94+
95+ compiler . plugin ( 'done' , ( ) => {
96+ if ( this . options . onBuildExit . length ) {
97+ console . log ( 'Executing additional scripts before exit' ) ;
98+ this . options . onBuildExit . forEach ( script => {
99+ if ( this . options . throwOnExecError ) {
100+ spreadStdoutAndStdErr ( exec ( script , putsThrow ) ) ;
101+ } else {
117102 spreadStdoutAndStdErr ( exec ( script , puts ) ) ;
118- } ) ;
119- }
120- } ) ;
121- }
122- } ] ) ;
123- return WebpackShellPlugin ;
124- } ( ) ;
103+ }
104+ } ) ;
105+ }
106+ } ) ;
107+ }
108+ }
125109
126110module . exports = WebpackShellPlugin ;
0 commit comments