Skip to content

Commit e3c6fc0

Browse files
committed
2 parents 361c341 + 5b5d1d0 commit e3c6fc0

File tree

5 files changed

+201
-74
lines changed

5 files changed

+201
-74
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015-rollup"]
3+
}

index.js

Lines changed: 56 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
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

311
function puts(error, stdout, stderr) {
412
if (error) {
@@ -20,80 +28,57 @@ function validateInput(options) {
2028
return options;
2129
}
2230

23-
function WebpackShellPlugin(options) {
24-
var defaultOptions = {
25-
onBuildStart: [],
26-
onBuildEnd: [],
27-
onBuildExit: [],
28-
dev: true,
29-
verbose: false
30-
};
31-
32-
if (!options.onBuildStart) {
33-
options.onBuildStart = defaultOptions.onBuildStart;
34-
}
35-
36-
if (!options.onBuildEnd) {
37-
options.onBuildEnd = defaultOptions.onBuildEnd;
38-
}
39-
40-
if (!options.onBuildExit) {
41-
options.onBuildExit = defaultOptions.onBuildExit;
42-
}
43-
44-
if (!options.dev) {
45-
options.dev = defaultOptions.dev;
31+
function mergeOptions(options, defaults) {
32+
for (var key in defaults) {
33+
if (options.hasOwnProperty(key)) {
34+
defaults[key] = options[key];
35+
}
4636
}
37+
return defaults;
38+
}
4739

48-
if (!options.verbose) {
49-
options.verbose = defaultOptions.verbose;
40+
export default class WebpackShellPlugin {
41+
constructor(options) {
42+
this.options = validateInput(mergeOptions(options, defaultOptions));
5043
}
5144

52-
options = validateInput(options);
53-
54-
this.options = options;
55-
56-
}
45+
apply(compiler) {
5746

58-
WebpackShellPlugin.prototype.apply = function (compiler) {
59-
var options = this.options;
60-
61-
compiler.plugin('compilation', function (compilation) {
62-
if (options.verbose) {
63-
console.log('Report compilation:', compilation);
64-
}
65-
if (options.onBuildStart.length) {
66-
console.log('Executing pre-build scripts');
67-
options.onBuildStart.forEach(function (script) {
68-
exec(script, puts);
69-
});
70-
if (options.dev) {
71-
options.onBuildStart = [];
47+
compiler.plugin('compilation', (compilation) => {
48+
if (this.options.verbose) {
49+
console.log(`Report compilation: ${compilation}`);
7250
}
73-
}
74-
});
75-
76-
compiler.plugin('emit', function (compilation, callback) {
77-
if (options.onBuildEnd.length) {
78-
console.log('Executing post-build scripts');
79-
options.onBuildEnd.forEach(function (script) {
80-
exec(script, puts);
81-
});
82-
if (options.dev) {
83-
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+
}
8459
}
85-
}
86-
callback();
87-
});
88-
89-
compiler.plugin("done", function () {
90-
if (options.onBuildExit.length) {
91-
console.log("Executing addiotn scripts befor exit");
92-
options.onBuildExit.forEach(function (script) {
93-
exec(script, puts);
94-
});
95-
}
96-
});
97-
};
98-
99-
module.exports = WebpackShellPlugin;
60+
});
61+
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+
});
74+
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+
}

lib/index.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
'use strict';
2+
3+
var babelHelpers = {};
4+
5+
babelHelpers.classCallCheck = function (instance, Constructor) {
6+
if (!(instance instanceof Constructor)) {
7+
throw new TypeError("Cannot call a class as a function");
8+
}
9+
};
10+
11+
babelHelpers.createClass = function () {
12+
function defineProperties(target, props) {
13+
for (var i = 0; i < props.length; i++) {
14+
var descriptor = props[i];
15+
descriptor.enumerable = descriptor.enumerable || false;
16+
descriptor.configurable = true;
17+
if ("value" in descriptor) descriptor.writable = true;
18+
Object.defineProperty(target, descriptor.key, descriptor);
19+
}
20+
}
21+
22+
return function (Constructor, protoProps, staticProps) {
23+
if (protoProps) defineProperties(Constructor.prototype, protoProps);
24+
if (staticProps) defineProperties(Constructor, staticProps);
25+
return Constructor;
26+
};
27+
}();
28+
29+
babelHelpers;
30+
31+
var exec = require('child_process').exec;
32+
33+
var defaultOptions = {
34+
onBuildStart: [],
35+
onBuildEnd: [],
36+
onBuildExit: [],
37+
dev: true,
38+
verbose: false
39+
};
40+
41+
function puts(error, stdout, stderr) {
42+
if (error) {
43+
console.log('Error: ', error, stderr);
44+
}
45+
console.log(stdout);
46+
}
47+
48+
function validateInput(options) {
49+
if (typeof options.onBuildStart === 'string') {
50+
options.onBuildStart = options.onBuildStart.split('&&');
51+
}
52+
if (typeof options.onBuildEnd === 'string') {
53+
options.onBuildEnd = options.onBuildEnd.split('&&');
54+
}
55+
if (typeof options.onBuildExit === 'string') {
56+
options.onBuildExit = options.onBuildExit.split('&&');
57+
}
58+
return options;
59+
}
60+
61+
function mergeOptions(options, defaults) {
62+
for (var key in defaults) {
63+
if (options.hasOwnProperty(key)) {
64+
defaults[key] = options[key];
65+
}
66+
}
67+
return defaults;
68+
}
69+
70+
var WebpackShellPlugin = function () {
71+
function WebpackShellPlugin(options) {
72+
babelHelpers.classCallCheck(this, WebpackShellPlugin);
73+
74+
this.options = validateInput(mergeOptions(options, defaultOptions));
75+
}
76+
77+
babelHelpers.createClass(WebpackShellPlugin, [{
78+
key: 'apply',
79+
value: function apply(compiler) {
80+
var _this = this;
81+
82+
compiler.plugin('compilation', function (compilation) {
83+
if (_this.options.verbose) {
84+
console.log('Report compilation: ' + compilation);
85+
}
86+
if (_this.options.onBuildStart.length) {
87+
console.log('Executing pre-build scripts');
88+
_this.options.onBuildStart.forEach(function (script) {
89+
exec(script, puts);
90+
});
91+
if (_this.options.dev) {
92+
_this.options.onBuildStart = [];
93+
}
94+
}
95+
});
96+
97+
compiler.plugin('emit', function (compilation, callback) {
98+
if (_this.options.onBuildEnd.length) {
99+
console.log('Executing post-build scripts');
100+
_this.options.onBuildEnd.forEach(function (script) {
101+
exec(script, puts);
102+
});
103+
if (_this.options.dev) {
104+
_this.options.onBuildEnd = [];
105+
}
106+
}
107+
callback();
108+
});
109+
110+
compiler.plugin('done', function () {
111+
if (_this.options.onBuildExit.length) {
112+
console.log('Executing addiotn scripts befor exit');
113+
_this.options.onBuildExit.forEach(function (script) {
114+
exec(script, puts);
115+
});
116+
}
117+
});
118+
}
119+
}]);
120+
return WebpackShellPlugin;
121+
}();
122+
123+
module.exports = WebpackShellPlugin;

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "webpack-shell-plugin",
33
"version": "0.4.0",
44
"description": "Run shell commands before and after webpack builds",
5-
"main": "index.js",
5+
"main": "lib/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "rollup -c"
89
},
910
"repository": {
1011
"type": "git",
@@ -20,12 +21,19 @@
2021
"browser",
2122
"script",
2223
"opie",
24+
"manion",
2325
"1337"
2426
],
2527
"author": "Patrick Opie <opiepj@plu.edu>",
2628
"license": "MIT",
2729
"bugs": {
2830
"url": "https://github.com/1337programming/webpack-shell-plugin/issues"
2931
},
30-
"homepage": "https://github.com/1337programming/webpack-shell-plugin"
32+
"homepage": "https://github.com/1337programming/webpack-shell-plugin",
33+
"devDependencies": {
34+
"babel-core": "^6.7.6",
35+
"babel-preset-es2015-rollup": "^1.1.1",
36+
"rollup": "^0.25.8",
37+
"rollup-plugin-babel": "^2.4.0"
38+
}
3139
}

rollup.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import babel from 'rollup-plugin-babel';
2+
3+
export default {
4+
entry: 'index.js',
5+
format: 'cjs',
6+
plugins: [babel()],
7+
dest: 'lib/index.js'
8+
};

0 commit comments

Comments
 (0)