Skip to content

Commit a30f22f

Browse files
rusluxRuslan Roskoshnyj
authored and
Ruslan Roskoshnyj
committed
Add sync option: resolve #48
1 parent a65ae75 commit a30f22f

File tree

3 files changed

+71
-32
lines changed

3 files changed

+71
-32
lines changed

lib/index.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ var defaultOptions = {
151151
onBuildExit: [],
152152
dev: true,
153153
verbose: false,
154-
safe: false
154+
safe: false,
155+
sync: false
155156
};
156157

157158
var WebpackShellPlugin = function () {
@@ -192,17 +193,42 @@ var WebpackShellPlugin = function () {
192193
}
193194
}, {
194195
key: 'handleScript',
195-
value: function handleScript(script) {
196+
value: function handleScript(script, next) {
197+
var proc = null;
196198
if (os.platform() === 'win32' || this.options.safe) {
197-
this.spreadStdoutAndStdErr(exec(script, this.puts));
199+
proc = exec(script, this.puts);
200+
this.spreadStdoutAndStdErr(proc);
198201
} else {
199202
var _serializeScript = this.serializeScript(script),
200203
command = _serializeScript.command,
201204
args = _serializeScript.args;
202205

203-
var proc = spawn(command, args, { stdio: 'inherit' });
206+
proc = spawn(command, args, { stdio: 'inherit' });
204207
proc.on('close', this.puts);
205208
}
209+
210+
if (this.options.sync) {
211+
proc.on('close', next);
212+
} else {
213+
next();
214+
}
215+
}
216+
}, {
217+
key: 'handleScriptsOn',
218+
value: function handleScriptsOn(data) {
219+
var _this = this;
220+
221+
var next = function next(inbound) {
222+
if (inbound.length > 0) {
223+
var head = inbound[0];
224+
var tail = inbound.slice(1);
225+
226+
_this.handleScript(head, function () {
227+
next(tail);
228+
});
229+
}
230+
};
231+
next(data);
206232
}
207233
}, {
208234
key: 'validateInput',
@@ -231,42 +257,38 @@ var WebpackShellPlugin = function () {
231257
}, {
232258
key: 'apply',
233259
value: function apply(compiler) {
234-
var _this = this;
260+
var _this2 = this;
235261

236262
compiler.plugin('compilation', function (compilation) {
237-
if (_this.options.verbose) {
263+
if (_this2.options.verbose) {
238264
console.log('Report compilation: ' + compilation);
239265
console.warn('WebpackShellPlugin [' + new Date() + ']: Verbose is being deprecated, please remove.');
240266
}
241-
if (_this.options.onBuildStart.length) {
267+
if (_this2.options.onBuildStart.length) {
242268
console.log('Executing pre-build scripts');
243-
for (var i = 0; i < _this.options.onBuildStart.length; i++) {
244-
_this.handleScript(_this.options.onBuildStart[i]);
245-
}
246-
if (_this.options.dev) {
247-
_this.options.onBuildStart = [];
269+
_this2.handleScriptsOn(_this2.options.onBuildStart);
270+
if (_this2.options.dev) {
271+
_this2.options.onBuildStart = [];
248272
}
249273
}
250274
});
251275

252276
compiler.plugin('after-emit', function (compilation, callback) {
253-
if (_this.options.onBuildEnd.length) {
277+
if (_this2.options.onBuildEnd.length) {
254278
console.log('Executing post-build scripts');
255-
for (var i = 0; i < _this.options.onBuildEnd.length; i++) {
256-
_this.handleScript(_this.options.onBuildEnd[i]);
257-
}
258-
if (_this.options.dev) {
259-
_this.options.onBuildEnd = [];
279+
_this2.handleScriptsOn(_this2.options.onBuildEnd);
280+
if (_this2.options.dev) {
281+
_this2.options.onBuildEnd = [];
260282
}
261283
}
262284
callback();
263285
});
264286

265287
compiler.plugin('done', function () {
266-
if (_this.options.onBuildExit.length) {
288+
if (_this2.options.onBuildExit.length) {
267289
console.log('Executing additional scripts before exit');
268-
for (var i = 0; i < _this.options.onBuildExit.length; i++) {
269-
_this.handleScript(_this.options.onBuildExit[i]);
290+
for (var i = 0; i < _this2.options.onBuildExit.length; i++) {
291+
_this2.handleScript(_this2.options.onBuildExit[i]);
270292
}
271293
}
272294
});

src/webpack-shell-plugin.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const defaultOptions = {
88
onBuildExit: [],
99
dev: true,
1010
verbose: false,
11-
safe: false
11+
safe: false,
12+
sync: false
1213
};
1314

1415
export default class WebpackShellPlugin {
@@ -36,14 +37,34 @@ export default class WebpackShellPlugin {
3637
return {command, args};
3738
}
3839

39-
handleScript(script) {
40+
handleScript(script, next) {
41+
let proc = null;
4042
if (os.platform() === 'win32' || this.options.safe) {
41-
this.spreadStdoutAndStdErr(exec(script, this.puts));
43+
proc = exec(script, this.puts);
44+
this.spreadStdoutAndStdErr(proc);
4245
} else {
4346
const {command, args} = this.serializeScript(script);
44-
const proc = spawn(command, args, {stdio: 'inherit'});
47+
proc = spawn(command, args, {stdio: 'inherit'});
4548
proc.on('close', this.puts);
4649
}
50+
51+
if (this.options.sync) {
52+
proc.on('close', next);
53+
} else {
54+
next()
55+
}
56+
}
57+
58+
handleScriptsOn(data) {
59+
let next = (inbound) => {
60+
if (inbound.length > 0) {
61+
let head = inbound[0];
62+
let tail = inbound.slice(1);
63+
64+
this.handleScript(head, () => {next(tail)});
65+
}
66+
};
67+
next(data)
4768
}
4869

4970
validateInput(options) {
@@ -77,9 +98,7 @@ export default class WebpackShellPlugin {
7798
}
7899
if (this.options.onBuildStart.length) {
79100
console.log('Executing pre-build scripts');
80-
for (let i = 0; i < this.options.onBuildStart.length; i++) {
81-
this.handleScript(this.options.onBuildStart[i]);
82-
}
101+
this.handleScriptsOn(this.options.onBuildStart);
83102
if (this.options.dev) {
84103
this.options.onBuildStart = [];
85104
}
@@ -89,9 +108,7 @@ export default class WebpackShellPlugin {
89108
compiler.plugin('after-emit', (compilation, callback) => {
90109
if (this.options.onBuildEnd.length) {
91110
console.log('Executing post-build scripts');
92-
for (let i = 0; i < this.options.onBuildEnd.length; i++) {
93-
this.handleScript(this.options.onBuildEnd[i]);
94-
}
111+
this.handleScriptsOn(this.options.onBuildEnd);
95112
if (this.options.dev) {
96113
this.options.onBuildEnd = [];
97114
}

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
]
2020
},
2121
plugins: [
22-
new WebpackShellPlugin({onBuildStart:['node test.js'], onBuildEnd:['echo "Webpack End"'], safe: true, verbose: true}),
22+
new WebpackShellPlugin({onBuildStart:['node test.js', 'echo "end node.test"'], onBuildEnd:['echo "Webpack End"'], safe: true, verbose: true, sync: true}),
2323
new webpack.HotModuleReplacementPlugin()
2424
]
2525
};

0 commit comments

Comments
 (0)