-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathindex.js
204 lines (150 loc) · 4.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var toArray = function (arr) {
return Array.isArray(arr) ? arr : Array.from(arr);
};
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var os = require('os');
var defaultOptions = {
onBuildStart: [],
onBuildEnd: [],
onBuildExit: [],
dev: true,
verbose: false,
safe: false,
swallowError: false
};
var WebpackShellPlugin = function () {
function WebpackShellPlugin(options) {
classCallCheck(this, WebpackShellPlugin);
this.options = this.validateInput(this.mergeOptions(options, defaultOptions));
}
createClass(WebpackShellPlugin, [{
key: 'puts',
value: function puts(error) {
if (error && !this.options.swallowError) {
throw error;
}
}
}, {
key: 'spreadStdoutAndStdErr',
value: function spreadStdoutAndStdErr(proc) {
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stdout);
}
}, {
key: 'serializeScript',
value: function serializeScript(script) {
if (typeof script === 'string') {
var _script$split = script.split(' '),
_script$split2 = toArray(_script$split),
_command = _script$split2[0],
_args = _script$split2.slice(1);
return { command: _command, args: _args };
}
var command = script.command,
args = script.args;
return { command: command, args: args };
}
}, {
key: 'handleScript',
value: function handleScript(script) {
if (os.platform() === 'win32' || this.options.safe) {
this.spreadStdoutAndStdErr(exec(script, this.puts.bind(this)));
} else {
var _serializeScript = this.serializeScript(script),
command = _serializeScript.command,
args = _serializeScript.args;
var proc = spawn(command, args, { stdio: 'inherit' });
proc.on('close', this.puts.bind(this));
}
}
}, {
key: 'validateInput',
value: function validateInput(options) {
if (typeof options.onBuildStart === 'string') {
options.onBuildStart = options.onBuildStart.split('&&');
}
if (typeof options.onBuildEnd === 'string') {
options.onBuildEnd = options.onBuildEnd.split('&&');
}
if (typeof options.onBuildExit === 'string') {
options.onBuildExit = options.onBuildExit.split('&&');
}
return options;
}
}, {
key: 'mergeOptions',
value: function mergeOptions(options, defaults$$1) {
for (var key in defaults$$1) {
if (options.hasOwnProperty(key)) {
defaults$$1[key] = options[key];
}
}
return defaults$$1;
}
}, {
key: 'apply',
value: function apply(compiler) {
var _this = this;
compiler.plugin('compilation', function (compilation) {
if (_this.options.verbose) {
console.log('Report compilation: ' + compilation);
console.warn('WebpackShellPlugin [' + new Date() + ']: Verbose is being deprecated, please remove.');
}
if (_this.options.onBuildStart.length) {
console.log('Executing pre-build scripts');
for (var i = 0; i < _this.options.onBuildStart.length; i++) {
_this.handleScript(_this.options.onBuildStart[i]);
}
if (_this.options.dev) {
_this.options.onBuildStart = [];
}
}
});
compiler.plugin('after-emit', function (compilation, callback) {
if (_this.options.onBuildEnd.length) {
console.log('Executing post-build scripts');
for (var i = 0; i < _this.options.onBuildEnd.length; i++) {
_this.handleScript(_this.options.onBuildEnd[i]);
}
if (_this.options.dev) {
_this.options.onBuildEnd = [];
}
}
callback();
});
compiler.plugin('done', function () {
if (_this.options.onBuildExit.length) {
console.log('Executing additional scripts before exit');
for (var i = 0; i < _this.options.onBuildExit.length; i++) {
_this.handleScript(_this.options.onBuildExit[i]);
}
}
});
}
}]);
return WebpackShellPlugin;
}();
module.exports = WebpackShellPlugin;