1- var exec = require ( 'child_process' ) . exec ;
1+ const exec = require ( 'child_process' ) . exec ;
2+
3+ const defaultOptions = {
4+ onBuildStart : [ ] ,
5+ onBuildEnd : [ ] ,
6+ onBuildExit : [ ] ,
7+ dev : true ,
8+ verbose : false
9+ } ;
210
311function puts ( error , stdout , stderr ) {
412 if ( error ) {
@@ -20,80 +28,57 @@ function validateInput(options) {
2028 return options ;
2129}
2230
23- function WebpackShellPlugin ( options ) {
24- var defaultOptions = {
25- onBuildStart : [ ] ,
26- onBuildEnd : [ ] ,
27- onBuildExit : [ ] ,
28- dev : true ,
29- verbose : false
30- } ;
31-
32- if ( ! options . onBuildStart ) {
33- options . onBuildStart = defaultOptions . onBuildStart ;
34- }
35-
36- if ( ! options . onBuildEnd ) {
37- options . onBuildEnd = defaultOptions . onBuildEnd ;
38- }
39-
40- if ( ! options . onBuildExit ) {
41- options . onBuildExit = defaultOptions . onBuildExit ;
42- }
43-
44- if ( ! options . dev ) {
45- options . dev = defaultOptions . dev ;
31+ function mergeOptions ( options , defaults ) {
32+ for ( var key in defaults ) {
33+ if ( options . hasOwnProperty ( key ) ) {
34+ defaults [ key ] = options [ key ] ;
35+ }
4636 }
37+ return defaults ;
38+ }
4739
48- if ( ! options . verbose ) {
49- options . verbose = defaultOptions . verbose ;
40+ export default class WebpackShellPlugin {
41+ constructor ( options ) {
42+ this . options = validateInput ( mergeOptions ( options , defaultOptions ) ) ;
5043 }
5144
52- options = validateInput ( options ) ;
53-
54- this . options = options ;
55-
56- }
45+ apply ( compiler ) {
5746
58- WebpackShellPlugin . prototype . apply = function ( compiler ) {
59- var options = this . options ;
60-
61- compiler . plugin ( 'compilation' , function ( compilation ) {
62- if ( options . verbose ) {
63- console . log ( 'Report compilation:' , compilation ) ;
64- }
65- if ( options . onBuildStart . length ) {
66- console . log ( 'Executing pre-build scripts' ) ;
67- options . onBuildStart . forEach ( function ( script ) {
68- exec ( script , puts ) ;
69- } ) ;
70- if ( options . dev ) {
71- options . onBuildStart = [ ] ;
47+ compiler . plugin ( 'compilation' , ( compilation ) => {
48+ if ( this . options . verbose ) {
49+ console . log ( `Report compilation: ${ compilation } ` ) ;
7250 }
73- }
74- } ) ;
75-
76- compiler . plugin ( 'emit' , function ( compilation , callback ) {
77- if ( options . onBuildEnd . length ) {
78- console . log ( 'Executing post-build scripts' ) ;
79- options . onBuildEnd . forEach ( function ( script ) {
80- exec ( script , puts ) ;
81- } ) ;
82- if ( options . dev ) {
83- options . onBuildEnd = [ ] ;
51+ if ( this . options . onBuildStart . length ) {
52+ console . log ( 'Executing pre-build scripts' ) ;
53+ this . options . onBuildStart . forEach ( script => {
54+ exec ( script , puts ) ;
55+ } ) ;
56+ if ( this . options . dev ) {
57+ this . options . onBuildStart = [ ] ;
58+ }
8459 }
85- }
86- callback ( ) ;
87- } ) ;
88-
89- compiler . plugin ( "done" , function ( ) {
90- if ( options . onBuildExit . length ) {
91- console . log ( "Executing addiotn scripts befor exit" ) ;
92- options . onBuildExit . forEach ( function ( script ) {
93- exec ( script , puts ) ;
94- } ) ;
95- }
96- } ) ;
97- } ;
98-
99- module . exports = WebpackShellPlugin ;
60+ } ) ;
61+
62+ compiler . plugin ( 'emit' , ( compilation , callback ) => {
63+ if ( this . options . onBuildEnd . length ) {
64+ console . log ( 'Executing post-build scripts' ) ;
65+ this . options . onBuildEnd . forEach ( script => {
66+ exec ( script , puts ) ;
67+ } ) ;
68+ if ( this . options . dev ) {
69+ this . options . onBuildEnd = [ ] ;
70+ }
71+ }
72+ callback ( ) ;
73+ } ) ;
74+
75+ compiler . plugin ( 'done' , ( ) => {
76+ if ( this . options . onBuildExit . length ) {
77+ console . log ( 'Executing addiotn scripts befor exit' ) ;
78+ this . options . onBuildExit . forEach ( script => {
79+ exec ( script , puts ) ;
80+ } ) ;
81+ }
82+ } ) ;
83+ }
84+ }
0 commit comments