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 ) {
@@ -21,66 +29,56 @@ function validateInput(options) {
2129}
2230
2331function mergeOptions ( options , defaults ) {
24- for ( key in defaults ) {
32+ for ( var key in defaults ) {
2533 if ( options . hasOwnProperty ( key ) ) {
26- defaults [ key ] = options [ key ] ;
34+ defaults [ key ] = options [ key ] ;
2735 }
2836 }
29-
3037 return defaults ;
3138}
3239
33- function WebpackShellPlugin ( options ) {
34- var defaults = {
35- onBuildStart : [ ] ,
36- onBuildEnd : [ ] ,
37- onBuildExit : [ ] ,
38- dev : true ,
39- verbose : false
40- } ;
41-
42- this . options = validateInput ( mergeOptions ( options , defaults ) ) ;
43- }
40+ export default class WebpackShellPlugin {
41+ constructor ( options ) {
42+ this . options = validateInput ( mergeOptions ( options , defaultOptions ) ) ;
43+ }
4444
45- WebpackShellPlugin . prototype . apply = function ( compiler ) {
46- var options = this . options ;
45+ apply ( compiler ) {
4746
48- compiler . plugin ( 'compilation' , function ( compilation ) {
49- if ( options . verbose ) {
50- console . log ( 'Report compilation:' , compilation ) ;
51- }
52- if ( options . onBuildStart . length ) {
53- console . log ( 'Executing pre-build scripts' ) ;
54- options . onBuildStart . forEach ( function ( script ) {
55- exec ( script , puts ) ;
56- } ) ;
57- if ( options . dev ) {
58- options . onBuildStart = [ ] ;
47+ compiler . plugin ( 'compilation' , ( compilation ) => {
48+ if ( this . options . verbose ) {
49+ console . log ( `Report compilation: ${ compilation } ` ) ;
5950 }
60- }
61- } ) ;
62-
63- compiler . plugin ( 'emit' , function ( compilation , callback ) {
64- if ( options . onBuildEnd . length ) {
65- console . log ( 'Executing post-build scripts' ) ;
66- options . onBuildEnd . forEach ( function ( script ) {
67- exec ( script , puts ) ;
68- } ) ;
69- if ( options . dev ) {
70- 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+ }
7159 }
72- }
73- callback ( ) ;
74- } ) ;
60+ } ) ;
7561
76- compiler . plugin ( "done" , function ( ) {
77- if ( options . onBuildExit . length ) {
78- console . log ( "Executing addiotn scripts befor exit" ) ;
79- options . onBuildExit . forEach ( function ( script ) {
80- exec ( script , puts ) ;
81- } ) ;
82- }
83- } ) ;
84- } ;
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+ } ) ;
8574
86- module . exports = WebpackShellPlugin ;
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