|
1 | 1 | var exec = require('child_process').exec; |
2 | 2 |
|
3 | 3 | function puts(error, stdout, stderr) { |
4 | | - console.log(stdout); |
| 4 | + if (error) { |
| 5 | + console.log('Error: ', error, stderr); |
| 6 | + } |
| 7 | + if (this.options.verbose) { |
| 8 | + console.log(stdout); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +function validateInput(options) { |
| 13 | + if (typeof options.onBuildStart === 'string') { |
| 14 | + options.onBuildStart = options.onBuildStart.split('&&'); |
| 15 | + } |
| 16 | + if (typeof options.onBuildEnd === 'string') { |
| 17 | + options.onBuildEnd = options.onBuildEnd.split('&&'); |
| 18 | + } |
| 19 | + return options; |
5 | 20 | } |
6 | 21 |
|
7 | 22 | function WebpackShellPlugin(options) { |
8 | 23 | var defaultOptions = { |
9 | 24 | onBuildStart: [], |
10 | 25 | onBuildEnd: [], |
11 | | - dev: false |
| 26 | + dev: true, |
| 27 | + verbose: false |
12 | 28 | }; |
| 29 | + |
13 | 30 | if (!options.onBuildStart) { |
14 | 31 | options.onBuildStart = defaultOptions.onBuildStart; |
15 | 32 | } |
16 | 33 |
|
17 | | - if(!options.onBuildEnd) { |
| 34 | + if (!options.onBuildEnd) { |
18 | 35 | options.onBuildEnd = defaultOptions.onBuildEnd; |
19 | 36 | } |
20 | 37 |
|
| 38 | + options = validateInput(options); |
| 39 | + |
| 40 | + |
| 41 | + if (!options.dev) { |
| 42 | + options.dev = defaultOptions.dev; |
| 43 | + } |
| 44 | + |
21 | 45 | this.options = options; |
22 | 46 |
|
23 | 47 | } |
24 | 48 |
|
25 | | -WebpackShellPlugin.prototype.apply = function(compiler) { |
26 | | - const options = this.options; |
| 49 | +WebpackShellPlugin.prototype.apply = function (compiler) { |
| 50 | + var options = this.options; |
27 | 51 |
|
28 | 52 | compiler.plugin("compilation", function (compilation) { |
29 | | - if(options.onBuildStart.length){ |
30 | | - console.log("Executing pre-build scripts"); |
31 | | - options.onBuildStart.forEach(function (script) { exec(script, puts)}); |
32 | | - if (options.dev) { |
33 | | - options.onBuildStart = []; |
| 53 | + if (options.verbose) { |
| 54 | + console.log('Report compilation:', compilation); |
34 | 55 | } |
35 | | - } |
36 | | -}); |
| 56 | + if (options.onBuildStart.length) { |
| 57 | + console.log("Executing pre-build scripts"); |
| 58 | + options.onBuildStart.forEach(function (script) { |
| 59 | + exec(script, puts) |
| 60 | + }); |
| 61 | + if (options.dev) { |
| 62 | + options.onBuildStart = []; |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
37 | 66 |
|
38 | 67 | compiler.plugin("emit", function (compilation, callback) { |
39 | | - if(options.onBuildEnd.length){ |
40 | | - console.log("Executing post-build scripts"); |
41 | | - options.onBuildEnd.forEach(function(script){ exec(script, puts)}); |
42 | | - if (options.dev) { |
43 | | - options.onBuildEnd = []; |
| 68 | + if (options.onBuildEnd.length) { |
| 69 | + console.log("Executing post-build scripts"); |
| 70 | + options.onBuildEnd.forEach(function (script) { |
| 71 | + exec(script, puts) |
| 72 | + }); |
| 73 | + if (options.dev) { |
| 74 | + options.onBuildEnd = []; |
| 75 | + } |
44 | 76 | } |
45 | | - } |
46 | | - callback(); |
47 | | -}); |
| 77 | + callback(); |
| 78 | + }); |
48 | 79 | }; |
49 | 80 |
|
50 | 81 | module.exports = WebpackShellPlugin; |
|
0 commit comments