diff --git a/.babelrc b/.babelrc index 13b7688..c73500d 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,5 @@ { - "presets": ["es2015-rollup"] + "presets": [["env", {"modules": false}]], + "plugins": ["transform-runtime"], + "runtimeHelpers": true } \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index d6c468e..92d766a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,148 +1,16 @@ 'use strict'; -var asyncGenerator = function () { - function AwaitValue(value) { - this.value = value; - } - - function AsyncGenerator(gen) { - var front, back; - - function send(key, arg) { - return new Promise(function (resolve, reject) { - var request = { - key: key, - arg: arg, - resolve: resolve, - reject: reject, - next: null - }; - - if (back) { - back = back.next = request; - } else { - front = back = request; - resume(key, arg); - } - }); - } - - function resume(key, arg) { - try { - var result = gen[key](arg); - var value = result.value; - - if (value instanceof AwaitValue) { - Promise.resolve(value.value).then(function (arg) { - resume("next", arg); - }, function (arg) { - resume("throw", arg); - }); - } else { - settle(result.done ? "return" : "normal", result.value); - } - } catch (err) { - settle("throw", err); - } - } - - function settle(type, value) { - switch (type) { - case "return": - front.resolve({ - value: value, - done: true - }); - break; - - case "throw": - front.reject(value); - break; - - default: - front.resolve({ - value: value, - done: false - }); - break; - } - - front = front.next; - - if (front) { - resume(front.key, front.arg); - } else { - back = null; - } - } - - this._invoke = send; - - if (typeof gen.return !== "function") { - this.return = undefined; - } - } - - if (typeof Symbol === "function" && Symbol.asyncIterator) { - AsyncGenerator.prototype[Symbol.asyncIterator] = function () { - return this; - }; - } - - AsyncGenerator.prototype.next = function (arg) { - return this._invoke("next", arg); - }; - - AsyncGenerator.prototype.throw = function (arg) { - return this._invoke("throw", arg); - }; - - AsyncGenerator.prototype.return = function (arg) { - return this._invoke("return", arg); - }; - - return { - wrap: function (fn) { - return function () { - return new AsyncGenerator(fn.apply(this, arguments)); - }; - }, - await: function (value) { - return new AwaitValue(value); - } - }; -}(); - -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; - }; -}(); +function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } -var toArray = function (arr) { - return Array.isArray(arr) ? arr : Array.from(arr); -}; +var _getIterator = _interopDefault(require('babel-runtime/core-js/get-iterator')); +var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator')); +var _Promise = _interopDefault(require('babel-runtime/core-js/promise')); +var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator')); +var _toArray = _interopDefault(require('babel-runtime/helpers/toArray')); +var _classCallCheck = _interopDefault(require('babel-runtime/helpers/classCallCheck')); +var _createClass = _interopDefault(require('babel-runtime/helpers/createClass')); +var child_process = require('child_process'); -var spawn = require('child_process').spawn; -var exec = require('child_process').exec; var os = require('os'); var defaultOptions = { @@ -151,17 +19,22 @@ var defaultOptions = { onBuildExit: [], dev: true, verbose: false, - safe: false + safe: false, + sync: false }; var WebpackShellPlugin = function () { function WebpackShellPlugin(options) { - classCallCheck(this, WebpackShellPlugin); + _classCallCheck(this, WebpackShellPlugin); this.options = this.validateInput(this.mergeOptions(options, defaultOptions)); + + this.onCompilation = this.onCompilation.bind(this); + this.onAfterEmit = this.onAfterEmit.bind(this); + this.onDone = this.onDone.bind(this); } - createClass(WebpackShellPlugin, [{ + _createClass(WebpackShellPlugin, [{ key: 'puts', value: function puts(error, stdout, stderr) { if (error) { @@ -179,7 +52,7 @@ var WebpackShellPlugin = function () { value: function serializeScript(script) { if (typeof script === 'string') { var _script$split = script.split(' '), - _script$split2 = toArray(_script$split), + _script$split2 = _toArray(_script$split), _command = _script$split2[0], _args = _script$split2.slice(1); @@ -190,18 +63,83 @@ var WebpackShellPlugin = function () { return { command: command, args: args }; } + }, { + key: 'sleep', + value: function () { + var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(ms) { + return _regeneratorRuntime.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + return _context.abrupt('return', new _Promise(function (resolve) { + return setTimeout(resolve, ms); + })); + + case 1: + case 'end': + return _context.stop(); + } + } + }, _callee, this); + })); + + function sleep(_x) { + return _ref.apply(this, arguments); + } + + return sleep; + }() }, { key: 'handleScript', value: function handleScript(script) { + var proc = null; + if (os.platform() === 'win32' || this.options.safe) { - this.spreadStdoutAndStdErr(exec(script, this.puts)); + if (this.options.sync) { + proc = child_process.execSync(script, { stdio: [0, 1, 2] }); + } else { + proc = child_process.exec(script, this.puts); + this.spreadStdoutAndStdErr(proc); + } } else { var _serializeScript = this.serializeScript(script), command = _serializeScript.command, args = _serializeScript.args; - var proc = spawn(command, args, { stdio: 'inherit' }); - proc.on('close', this.puts); + if (this.options.sync) { + proc = child_process.spawnSync(command, args, { stdio: 'inherit' }); + } else { + proc = child_process.spawn(command, args, { stdio: 'inherit' }); + proc.on('close', this.puts); + } + } + } + }, { + key: 'handleScriptsOn', + value: function handleScriptsOn(data) { + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = _getIterator(data), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var item = _step.value; + + this.handleScript(item); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } } } }, { @@ -231,48 +169,48 @@ var WebpackShellPlugin = function () { }, { 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 = []; - } + compiler.plugin('compilation', this.onCompilation); + compiler.plugin('after-emit', this.onAfterEmit); + compiler.plugin('done', this.onDone); + } + }, { + key: 'onCompilation', + value: function onCompilation(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'); + this.handleScriptsOn(this.options.onBuildStart); + if (this.options.dev) { + this.options.onBuildStart = []; } - 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]); - } + } + } + }, { + key: 'onAfterEmit', + value: function onAfterEmit(compilation, callback) { + if (this.options.onBuildEnd.length) { + console.log('Executing post-build scripts'); + this.handleScriptsOn(this.options.onBuildEnd); + if (this.options.dev) { + this.options.onBuildEnd = []; } - }); + } + callback(); + } + }, { + key: 'onDone', + value: function onDone() { + if (this.options.onBuildExit.length) { + console.log('Executing additional scripts before exit'); + this.handleScriptsOn(this.options.onBuildExit); + } } }]); + return WebpackShellPlugin; }(); -module.exports = WebpackShellPlugin; \ No newline at end of file +module.exports = WebpackShellPlugin; diff --git a/package.json b/package.json index a51d3ea..e294e24 100644 --- a/package.json +++ b/package.json @@ -34,12 +34,18 @@ }, "homepage": "https://github.com/1337programming/webpack-shell-plugin", "devDependencies": { - "babel-core": "^6.7.6", - "babel-preset-es2015-rollup": "^1.1.1", + "babel-core": "^6.26.0", + "babel-plugin-external-helpers": "^6.22.0", + "babel-plugin-transform-runtime": "^6.23.0", + "babel-polyfill": "^6.26.0", + "babel-preset-env": "^1.6.1", + "babel-runtime": "^6.26.0", + "babelrc-rollup": "^3.0.0", "css-loader": "^0.23.1", "eslint": "^2.7.0", - "rollup": "^0.25.8", - "rollup-plugin-babel": "^2.4.0", + "polyfill": "^0.1.0", + "rollup": "^0.55.3", + "rollup-plugin-babel": "^3.0.3", "style-loader": "^0.13.1", "webpack": "^1.13.1" } diff --git a/rollup.config.js b/rollup.config.js index 918c345..f6a1bbd 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,10 +1,13 @@ import babel from 'rollup-plugin-babel'; +import babelRc from 'babelrc-rollup'; export default { - entry: 'src/webpack-shell-plugin.js', - format: 'cjs', + input: 'src/webpack-shell-plugin.js', + output: { + file: 'lib/index.js', + format: 'cjs', + }, plugins: [ - babel() + babel(babelRc()) ], - dest: 'lib/index.js' }; diff --git a/src/webpack-shell-plugin.js b/src/webpack-shell-plugin.js index b287508..077642d 100644 --- a/src/webpack-shell-plugin.js +++ b/src/webpack-shell-plugin.js @@ -1,5 +1,4 @@ -const spawn = require('child_process').spawn; -const exec = require('child_process').exec; +import {spawn, spawnSync, exec, execSync} from 'child_process'; const os = require('os'); const defaultOptions = { @@ -8,12 +7,17 @@ const defaultOptions = { onBuildExit: [], dev: true, verbose: false, - safe: false + safe: false, + sync: false }; export default class WebpackShellPlugin { constructor(options) { this.options = this.validateInput(this.mergeOptions(options, defaultOptions)); + + this.onCompilation = this.onCompilation.bind(this); + this.onAfterEmit = this.onAfterEmit.bind(this); + this.onDone = this.onDone.bind(this) } puts(error, stdout, stderr) { @@ -36,13 +40,34 @@ export default class WebpackShellPlugin { return {command, args}; } + async sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + handleScript(script) { + let proc = null; + if (os.platform() === 'win32' || this.options.safe) { - this.spreadStdoutAndStdErr(exec(script, this.puts)); + if (this.options.sync) { + proc = execSync(script, {stdio:[0, 1, 2]}); + } else { + proc = exec(script, this.puts); + this.spreadStdoutAndStdErr(proc); + } } else { const {command, args} = this.serializeScript(script); - const proc = spawn(command, args, {stdio: 'inherit'}); - proc.on('close', this.puts); + if (this.options.sync) { + proc = spawnSync(command, args, {stdio: 'inherit'}); + } else { + proc = spawn(command, args, {stdio: 'inherit'}); + proc.on('close', this.puts); + } + } + } + + handleScriptsOn(data) { + for (let item of data) { + this.handleScript(item) } } @@ -69,43 +94,40 @@ export default class WebpackShellPlugin { } apply(compiler) { + compiler.plugin('compilation', this.onCompilation); + compiler.plugin('after-emit', this.onAfterEmit); + compiler.plugin('done', this.onDone); + } - compiler.plugin('compilation', (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 (let i = 0; i < this.options.onBuildStart.length; i++) { - this.handleScript(this.options.onBuildStart[i]); - } - if (this.options.dev) { - this.options.onBuildStart = []; - } + onCompilation(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'); + this.handleScriptsOn(this.options.onBuildStart); + if (this.options.dev) { + this.options.onBuildStart = []; } - }); - - compiler.plugin('after-emit', (compilation, callback) => { - if (this.options.onBuildEnd.length) { - console.log('Executing post-build scripts'); - for (let i = 0; i < this.options.onBuildEnd.length; i++) { - this.handleScript(this.options.onBuildEnd[i]); - } - if (this.options.dev) { + } + } + + onAfterEmit(compilation, callback) { + if (this.options.onBuildEnd.length) { + console.log('Executing post-build scripts'); + this.handleScriptsOn(this.options.onBuildEnd); + if (this.options.dev) { this.options.onBuildEnd = []; - } } - callback(); - }); - - compiler.plugin('done', () => { - if (this.options.onBuildExit.length) { - console.log('Executing additional scripts before exit'); - for (let i = 0; i < this.options.onBuildExit.length; i++) { - this.handleScript(this.options.onBuildExit[i]); - } - } - }); + } + callback(); + } + + onDone() { + if (this.options.onBuildExit.length) { + console.log('Executing additional scripts before exit'); + this.handleScriptsOn(this.options.onBuildExit); + } } } diff --git a/webpack.config.js b/webpack.config.js index 95ef485..a559839 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -19,7 +19,15 @@ module.exports = { ] }, plugins: [ - new WebpackShellPlugin({onBuildStart:['node test.js'], onBuildEnd:['echo "Webpack End"'], safe: true, verbose: true}), + new WebpackShellPlugin( + { + onBuildStart:['echo "wait sleep 20"', 'sleep 20', 'node test.js', 'echo "end node.test"'], + onBuildEnd:['echo "Webpack End"'], + safe: true, + verbose: true, + sync: true + } + ), new webpack.HotModuleReplacementPlugin() ] };