Skip to content

Commit

Permalink
- add function to scripts
Browse files Browse the repository at this point in the history
- refactoring
  • Loading branch information
s00d committed Oct 18, 2019
1 parent 8fa1a1e commit 9f267da
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 243 deletions.
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ In `webpack.config.js`:

```js
const WebpackShellPluginNext = require('webpack-shell-plugin-next');

...
module.exports = {
...
...
plugins: [
new WebpackShellPluginNext({
Expand All @@ -41,27 +40,28 @@ module.exports = {
...
}
```
**More example in webpack.config.ts**

### API
* `onBeforeBuild`: array of scripts to execute before every build.

**Default: ```{scripts: [],blocking: false,parallel: false}```**
***Default: ```{scripts: [],blocking: false,parallel: false}```***

* `onBuildError`: array of scripts to execute when there is an error during compilation.

**Default: ```{scripts: [],blocking: false,parallel: false}```**
***Default: ```{scripts: [],blocking: false,parallel: false}```***

* `onBuildStart`: configuration object for scripts that execute before a compilation.

**Default: ```{scripts: [],blocking: false,parallel: false}```**
***Default: ```{scripts: [],blocking: false,parallel: false}```***

* `onBuildEnd`: configuration object for scripts that execute after files are emitted at the end of the compilation.

**Default: ```{scripts: [],blocking: false,parallel: false}```**
***Default: ```{scripts: [],blocking: false,parallel: false}```***

* `onBuildExit`: configuration object for scripts that execute after webpack's process is complete. *Note: this event also fires in `webpack --watch` when webpack has finished updating the bundle.*

**Default: ```{scripts: [],blocking: false,parallel: false}```**
***Default: ```{scripts: [],blocking: false,parallel: false}```***


* `blocking (onBeforeBuild, onBuildStart, onBuildEnd, onBuildExit, onBuildExit)`: block webpack until scripts finish execution.
Expand All @@ -80,6 +80,35 @@ module.exports = {
}
```

### TypeScript

This project is written in TypeScript, and type declarations are included. You can take advantage of this if your project's webpack configuration is also using TypeScript (e.g. webpack.config.ts and webpack.config.js).

### Function in scripts

how to use functions in the queue?

#### Example:
```js
{
scripts: [
// sync
() => {
console.log('run tTimeout 1');
setTimeout(() => console.log('end Timeout 1'), 1000);
},
() => new Promise((resolve, reject) => {
console.log('run async tTimeout');
setTimeout(() => {
console.log('end async tTimeout');
resolve();
}, 1000);
}),
// async
],
blocking: true
}
```

### Developing

Expand All @@ -89,11 +118,13 @@ Make sure you lint your code by running `npm run lint` and you can build the lib

I appreciate any feed back as well, Thanks for helping!

### Contributions
Pavel Kuzmin

## Change Log

### 1.1.0
```
- add function to scripts
- refactoring
```
### 1.0.0
```
- add ts
Expand Down
6 changes: 3 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import * as webpack from 'webpack';
export default class WebpackShellPlugin {
private options;
constructor(options: Options);
private mergeOptions;
private validateInput;
private putsAsync;
private puts;
private spreadStdoutAndStdErr;
private static spreadStdoutAndStdErr;
private serializeScript;
private handleScript;
private handleScriptAsync;
private executeScripts;
private validateInput;
private mergeOptions;
apply(compiler: webpack.Compiler): void;
private readonly onInvalid;
private readonly onCompilation;
Expand Down
117 changes: 59 additions & 58 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
var child_process_1 = require("child_process");
var os = __importStar(require("os"));
// if (!global._babelPolyfill) {
// require('babel-polyfill');
// }
var defaultTask = {
scripts: [],
blocking: false,
Expand Down Expand Up @@ -178,7 +175,7 @@ var WebpackShellPlugin = /** @class */ (function () {
}
});
}); };
this.options = this.validateInput(this.mergeOptions(options, defaultOptions));
this.options = this.mergeOptions(this.validateInput(options), defaultOptions);
if (this.options.verbose) {
this.warn("WebpackShellPlugin [" + new Date() + "]: Verbose is being deprecated, please remove.");
}
Expand All @@ -189,6 +186,32 @@ var WebpackShellPlugin = /** @class */ (function () {
this.putsAsync = this.putsAsync.bind(this);
this.puts = this.puts.bind(this);
}
WebpackShellPlugin.prototype.mergeOptions = function (provided, defaults) {
var options = {};
for (var key in defaults) {
// @ts-ignore
options[key] = JSON.parse(JSON.stringify(provided.hasOwnProperty(key) ? provided[key] : defaults[key]));
}
return options;
};
WebpackShellPlugin.prototype.validateInput = function (options) {
if (typeof options.onBeforeBuild === 'string') {
options.onBeforeBuild = { scripts: options.onBeforeBuild.split('&&') };
}
if (typeof options.onBuildStart === 'string') {
options.onBuildStart = { scripts: options.onBuildStart.split('&&') };
}
if (typeof options.onBuildEnd === 'string') {
options.onBuildEnd = { scripts: options.onBuildEnd.split('&&') };
}
if (typeof options.onBuildExit === 'string') {
options.onBuildExit = { scripts: options.onBuildExit.split('&&') };
}
if (typeof options.onBuildError === 'string') {
options.onBuildError = { scripts: options.onBuildError.split('&&') };
}
return options;
};
WebpackShellPlugin.prototype.putsAsync = function (resolve) {
var _this = this;
return function (error, stdout, stderr) {
Expand All @@ -203,7 +226,7 @@ var WebpackShellPlugin = /** @class */ (function () {
throw error;
}
};
WebpackShellPlugin.prototype.spreadStdoutAndStdErr = function (proc) {
WebpackShellPlugin.spreadStdoutAndStdErr = function (proc) {
if (!proc.stdout || !proc.stderr)
return;
proc.stdout.pipe(process.stdout);
Expand All @@ -228,9 +251,8 @@ var WebpackShellPlugin = /** @class */ (function () {
child_process_1.spawnSync(command, args, { stdio: this.options.logging ? 'inherit' : undefined, env: env });
}
};
WebpackShellPlugin.prototype.handleScriptAsync = function (script, blocking) {
WebpackShellPlugin.prototype.handleScriptAsync = function (script) {
var _this = this;
if (blocking === void 0) { blocking = false; }
if (os.platform() === 'win32' || this.options.safe) {
return new Promise(function (resolve) {
// @ts-ignore
Expand All @@ -245,73 +267,52 @@ var WebpackShellPlugin = /** @class */ (function () {
if (parallel === void 0) { parallel = false; }
if (blocking === void 0) { blocking = false; }
return __awaiter(this, void 0, void 0, function () {
var i, i, i;
var i, script;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!scripts || scripts.length <= 0) {
return [2 /*return*/];
}
if (!(blocking && !parallel)) return [3 /*break*/, 1];
for (i = 0; i < scripts.length; i++) {
this.handleScript(scripts[i]);
if (blocking && parallel) {
throw new Error("WebpackShellPlugin [" + new Date() + "]: Not supported");
}
return [3 /*break*/, 7];
case 1:
if (!(!blocking && !parallel)) return [3 /*break*/, 6];
i = 0;
_a.label = 2;
_a.label = 1;
case 1:
if (!(i < scripts.length)) return [3 /*break*/, 10];
script = scripts[i];
if (!(typeof script === 'function')) return [3 /*break*/, 5];
if (!blocking) return [3 /*break*/, 3];
return [4 /*yield*/, script()];
case 2:
if (!(i < scripts.length)) return [3 /*break*/, 5];
return [4 /*yield*/, this.handleScriptAsync(scripts[i])];
case 3:
_a.sent();
return [3 /*break*/, 4];
case 3:
script();
_a.label = 4;
case 4:
i++;
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 7];
case 4: return [3 /*break*/, 9];
case 5:
if (!blocking) return [3 /*break*/, 6];
this.handleScript(script);
return [3 /*break*/, 9];
case 6:
if (blocking && parallel) {
throw new Error('Not supported');
}
else if (!blocking && parallel) {
for (i = 0; i < scripts.length; i++) {
this.handleScriptAsync(scripts[i], blocking);
}
}
_a.label = 7;
case 7: return [2 /*return*/];
if (!!blocking) return [3 /*break*/, 9];
if (!parallel) return [3 /*break*/, 7];
this.handleScriptAsync(script);
return [3 /*break*/, 9];
case 7: return [4 /*yield*/, this.handleScriptAsync(script)];
case 8:
_a.sent();
_a.label = 9;
case 9:
i++;
return [3 /*break*/, 1];
case 10: return [2 /*return*/];
}
});
});
};
WebpackShellPlugin.prototype.validateInput = function (options) {
if (typeof options.onBeforeBuild === 'string') {
options.onBeforeBuild = { scripts: options.onBeforeBuild.split('&&') };
}
if (typeof options.onBuildStart === 'string') {
options.onBuildStart = { scripts: options.onBuildStart.split('&&') };
}
if (typeof options.onBuildEnd === 'string') {
options.onBuildEnd = { scripts: options.onBuildEnd.split('&&') };
}
if (typeof options.onBuildExit === 'string') {
options.onBuildExit = { scripts: options.onBuildExit.split('&&') };
}
if (typeof options.onBuildError === 'string') {
options.onBuildError = { scripts: options.onBuildError.split('&&') };
}
return options;
};
WebpackShellPlugin.prototype.mergeOptions = function (provided, defaults) {
var options = {};
for (var key in defaults) {
// @ts-ignore
options[key] = JSON.parse(JSON.stringify(provided.hasOwnProperty(key) ? provided[key] : defaults[key]));
}
return options;
};
WebpackShellPlugin.prototype.apply = function (compiler) {
if (compiler.hooks) {
compiler.hooks.invalid.tap('webpack-shell-plugin-next', this.onInvalid);
Expand Down
19 changes: 10 additions & 9 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
export declare type Task = {
scripts?: string[];
blocking?: false;
parallel?: false;
export declare type Task = Function | string;
export declare type Tasks = {
scripts?: Task[];
blocking?: boolean;
parallel?: boolean;
};
export declare type Script = {
command: string;
args: string[];
};
export declare type Options = {
/** Scripts to execute on the before build. Defaults to []. */
onBeforeBuild?: Task | string;
onBeforeBuild?: Tasks | string;
/** Scripts to execute on the initial build. Defaults to []. */
onBuildStart?: Task | string;
onBuildStart?: Tasks | string;
/**
* Scripts to execute after files are emitted at the end of the
* compilation. Defaults to [].
*/
onBuildEnd?: Task | string;
onBuildEnd?: Tasks | string;
/** Scripts to execute after Webpack's process completes. Defaults to []. */
onBuildExit?: Task | string;
onBuildExit?: Tasks | string;
/** Scripts to execute after Webpack's process Error. Defaults to []. */
onBuildError?: Task | string;
onBuildError?: Tasks | string;
/**
* Switch for development environments. This causes scripts to execute once.
* Useful for running HMR on webpack-dev-server or webpack watch mode.
Expand Down
Loading

0 comments on commit 9f267da

Please sign in to comment.