Skip to content

Commit 57addb7

Browse files
committed
Merge pull request #4 from 1337programming/feature/hmr-support
Feature/hmr support
2 parents 793d638 + 454bb91 commit 57addb7

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
node_modules
22
npm-debug.log
3+
*.swf
4+
.idea/
5+
.eslintrc.json
6+

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ module.exports = config;
4747
Once the build finishes, a child process is spawned firing both a python and node script.
4848

4949
### API
50-
* onBuildStart: **Array[ ]**, array of scripts to execute on the initial build. **Default: [ ]**
51-
* onBuildEnd: **Array[ ]**, array of scripts to execute after files are emitted. **Default: [ ]**
52-
* dev: **Boolean**, switch for development environments. This causes scripts to only execute once. Useful for running HMR on webpack-dev-server. **Default: false**
50+
* onBuildStart: array of scripts to execute on the initial build. **Default: [ ]**
51+
* onBuildEnd: array of scripts to execute after files are emitted at the end of the compilation. **Default: [ ]**
52+
* dev: switch for development environments. This causes scripts to only execute once. Useful for running HMR on webpack-dev-server. **Default: true**
53+
* verbose: enable for verbose output. **Default: false**
5354

5455
### Contributions
55-
[Yair Tavor]("http://stackoverflow.com/users/3054454/yair-tavor")
56+
Yair Tavor
5657

index.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,81 @@
11
var exec = require('child_process').exec;
22

33
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;
520
}
621

722
function WebpackShellPlugin(options) {
823
var defaultOptions = {
924
onBuildStart: [],
1025
onBuildEnd: [],
11-
dev: false
26+
dev: true,
27+
verbose: false
1228
};
29+
1330
if (!options.onBuildStart) {
1431
options.onBuildStart = defaultOptions.onBuildStart;
1532
}
1633

17-
if(!options.onBuildEnd) {
34+
if (!options.onBuildEnd) {
1835
options.onBuildEnd = defaultOptions.onBuildEnd;
1936
}
2037

38+
options = validateInput(options);
39+
40+
41+
if (!options.dev) {
42+
options.dev = defaultOptions.dev;
43+
}
44+
2145
this.options = options;
2246

2347
}
2448

25-
WebpackShellPlugin.prototype.apply = function(compiler) {
26-
const options = this.options;
49+
WebpackShellPlugin.prototype.apply = function (compiler) {
50+
var options = this.options;
2751

2852
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);
3455
}
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+
});
3766

3867
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+
}
4476
}
45-
}
46-
callback();
47-
});
77+
callback();
78+
});
4879
};
4980

5081
module.exports = WebpackShellPlugin;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-shell-plugin",
3-
"version": "0.2.3",
3+
"version": "0.3.0",
44
"description": "Run shell commands before and after webpack builds",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)