From 5efa3b0be4e801a3458b1560252180a2fcbc15af Mon Sep 17 00:00:00 2001 From: VandeurenGlenn Date: Sun, 3 Sep 2017 16:57:54 +0200 Subject: [PATCH] :metal: Add uglify & prepare muliple servers --- bin/backed.js | 215 +++++++++++++++++++++++++++++++++++++++----------- package.json | 3 +- src/backed.js | 5 ++ src/server.js | 26 ++++-- src/tasks.js | 28 +++++++ yarn.lock | 9 ++- 6 files changed, 234 insertions(+), 52 deletions(-) diff --git a/bin/backed.js b/bin/backed.js index 622e69d..ef91adb 100644 --- a/bin/backed.js +++ b/bin/backed.js @@ -399,8 +399,8 @@ var Config = function () { return Config; }(); -var _require$2 = require('rollup'); -var rollup = _require$2.rollup; +var _require$3 = require('rollup'); +var rollup = _require$3.rollup; var _require2$1 = require('path'); var join = _require2$1.join; @@ -966,10 +966,14 @@ var Server = function () { use: [{ path: null, static: null }], bowerPath: 'bower_components', nodeModulesPath: 'node_modules', + port: 3000, index: null }; return new Promise(function (resolve, reject) { if (config) { + _this.appname = config.name || global.config.name; + _this.entry = config.entry || global.config.entry; + _this.port = config.port || global.config.port; _this.handleOldOptions(config); if (config.use) { var _iteratorNormalCompletion = true; @@ -1026,18 +1030,31 @@ var Server = function () { app.use('/license', express.static(files[0])); }); - server.listen(3000, function (error) { - if (error) { - return logger$3.warn(error); - } - logger$3.log(global.config.name + '::serving from http://localhost:' + config.port + '/' + config.entry.replace('/', '')); - opn('http://localhost:' + config.port + '/' + config.entry.replace('/', '')); + app.get('/', function (request, response) { + console.log(request.params); + }); + _this.listen(); + + server.on('error', function (error) { + logger$3.warn(error); + _this.port += 1; + return _this.listen(); }); } else { reject(logger$3.warn(global.config.name + '::server config not found [example](https://raw.githubusercontent.com/VandeurenGlenn/backed-cli/master/config/backed.json)')); } }); } + }, { + key: 'listen', + value: function listen() { + var _this2 = this; + + server.listen(this.port, function () { + logger$3.log(_this2.appname + '::serving from http://localhost:' + _this2.port + '/' + _this2.entry.replace('/', '')); + opn('http://localhost:' + _this2.port + '/' + _this2.entry.replace('/', '')); + }); + } /** * @param {string} path - location of the file @@ -1081,8 +1098,8 @@ var Server = function () { var server$1 = new Server(); -var _require$3 = require('child_process'); -var fork$1 = _require$3.fork; +var _require$4 = require('child_process'); +var fork$1 = _require$4.fork; var chokidar = require('chokidar'); var path$1 = require('path'); @@ -1243,7 +1260,12 @@ var Watcher = function (_EventEmitter) { var watcher = new Watcher(); var fs = require('backed-fs'); + +var _require$2 = require('fs'); +var readFile = _require$2.readFile; + var webup$1 = require('webup'); +var uglifyEs = require('uglify-es'); var build = function build(config) { return new Promise(function (resolve, reject) { if (config.entry && config.sources) { @@ -1257,6 +1279,106 @@ var build = function build(config) { }); }; +var read = function read(src) { + return new Promise(function (resolve, reject) { + readFile(src, 'utf-8', function (error, code) { + if (error) { + reject(error); + } + resolve(code); + }); + }); +}; + +var uglify = function uglify(config) { + return new Promise(function (resolve, reject) { + function generator() { + return __async(_regeneratorRuntime.mark(function _callee() { + var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, bundle, file, result, done; + + return _regeneratorRuntime.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _iteratorNormalCompletion = true; + _didIteratorError = false; + _iteratorError = undefined; + _context.prev = 3; + _iterator = config.bundles[Symbol.iterator](); + + case 5: + if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { + _context.next = 17; + break; + } + + bundle = _step.value; + _context.next = 9; + return read(bundle.dest); + + case 9: + file = _context.sent; + result = uglifyEs.minify(file, { compress: true }); + _context.next = 13; + return fs.write({ contents: result.code }, bundle.dest.replace('.js', '.min.js')); + + case 13: + done = _context.sent; + + case 14: + _iteratorNormalCompletion = true; + _context.next = 5; + break; + + case 17: + _context.next = 23; + break; + + case 19: + _context.prev = 19; + _context.t0 = _context['catch'](3); + _didIteratorError = true; + _iteratorError = _context.t0; + + case 23: + _context.prev = 23; + _context.prev = 24; + + if (!_iteratorNormalCompletion && _iterator.return) { + _iterator.return(); + } + + case 26: + _context.prev = 26; + + if (!_didIteratorError) { + _context.next = 29; + break; + } + + throw _iteratorError; + + case 29: + return _context.finish(26); + + case 30: + return _context.finish(23); + + case 31: + resolve(); + + case 32: + case 'end': + return _context.stop(); + } + } + }, _callee, this, [[3, 19, 23, 31], [24,, 26, 30]]); + })()); + } + generator(); + }); +}; + var copy = function copy(config) { return new Promise(function (resolve, reject) { return fs.copySources(config.copy).then(function () { @@ -1278,6 +1400,7 @@ var watch = function watch(config) { var tasks = { build: build, + uglify: uglify, copy: copy, serve: serve, watch: watch @@ -1292,10 +1415,11 @@ var version = _require.version; var webup = require('webup'); var logger = require('backed-logger'); -commander.version(version).option('-w, --watch', 'watch for file changes & rebuild on change').option('-b, --build', 'build your app/component').option('-s, --serve', 'serve your app/component').option('-c, --copy', 'copy files from your app/component src folder to it distribution folder').option('-d, --debug', 'show all warnings').option('-v, --version', 'current version').parse(process.argv); +commander.version(version).option('-w, --watch', 'watch for file changes & rebuild on change').option('-u, --uglify', 'minimize code ouput').option('-b, --build', 'build your app/component').option('-s, --serve', 'serve your app/component').option('-c, --copy', 'copy files from your app/component src folder to it distribution folder').option('-d, --debug', 'show all warnings').option('-v, --version', 'current version').parse(process.argv); var commands = { build: Boolean(commander.build), + uglify: Boolean(commander.uglify), serve: Boolean(commander.serve) || Boolean(commander.watch), watch: Boolean(commander.watch), copy: Boolean(commander.build) || Boolean(commander.copy) @@ -1315,15 +1439,18 @@ new Config().then(function (config) { while (1) { switch (_context.prev = _context.next) { case 0: + if (config.uglify) { + commands.uglify = true; + } _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; - _context.prev = 3; + _context.prev = 4; _iterator = Object.entries(commands)[Symbol.iterator](); - case 5: + case 6: if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { - _context.next = 26; + _context.next = 27; break; } @@ -1332,86 +1459,86 @@ new Config().then(function (config) { enabled = task[1]; if (!enabled) { - _context.next = 23; + _context.next = 24; break; } - _context.prev = 10; + _context.prev = 11; if (!(name === 'serve' && commands.watch)) { - _context.next = 15; + _context.next = 16; break; } tasks[name](config); - _context.next = 18; + _context.next = 19; break; - case 15: - _context.next = 17; + case 16: + _context.next = 18; return tasks[name](config); - case 17: + case 18: done = _context.sent; - case 18: - _context.next = 23; + case 19: + _context.next = 24; break; - case 20: - _context.prev = 20; - _context.t0 = _context['catch'](10); + case 21: + _context.prev = 21; + _context.t0 = _context['catch'](11); logger.warn('task::function ' + name + ' ' + _context.t0); - case 23: + case 24: _iteratorNormalCompletion = true; - _context.next = 5; + _context.next = 6; break; - case 26: - _context.next = 32; + case 27: + _context.next = 33; break; - case 28: - _context.prev = 28; - _context.t1 = _context['catch'](3); + case 29: + _context.prev = 29; + _context.t1 = _context['catch'](4); _didIteratorError = true; _iteratorError = _context.t1; - case 32: - _context.prev = 32; + case 33: _context.prev = 33; + _context.prev = 34; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } - case 35: - _context.prev = 35; + case 36: + _context.prev = 36; if (!_didIteratorError) { - _context.next = 38; + _context.next = 39; break; } throw _iteratorError; - case 38: - return _context.finish(35); - case 39: - return _context.finish(32); + return _context.finish(36); case 40: - process.exit(0); + return _context.finish(33); case 41: + process.exit(0); + + case 42: case 'end': return _context.stop(); } } - }, _callee, this, [[3, 28, 32, 40], [10, 20], [33,, 35, 39]]); + }, _callee, this, [[4, 29, 33, 41], [11, 21], [34,, 36, 40]]); })()); } run(config); diff --git a/package.json b/package.json index f728834..8078c8b 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "gulp-nsp": "^2.1.0", "gulp-plumber": "^1.0.0", "merge-stream": "^1.0.1", - "rollup-plugin-uglify": "^1.0.2" + "rollup-plugin-uglify": "^1.0.2", + "uglify-es": "^3.0.28" }, "eslintConfig": { "extends": "xo-space", diff --git a/src/backed.js b/src/backed.js index 287e32a..f9bff92 100644 --- a/src/backed.js +++ b/src/backed.js @@ -11,6 +11,7 @@ import tasks from './tasks.js'; commander .version(version) .option('-w, --watch', 'watch for file changes & rebuild on change') + .option('-u, --uglify', 'minimize code ouput') .option('-b, --build', 'build your app/component') .option('-s, --serve', 'serve your app/component') .option('-c, --copy', 'copy files from your app/component src folder to it distribution folder') @@ -20,6 +21,7 @@ commander const commands = { build: Boolean(commander.build), + uglify: Boolean(commander.uglify), serve: Boolean(commander.serve) || Boolean(commander.watch), watch: Boolean(commander.watch), copy: Boolean(commander.build) || Boolean(commander.copy) @@ -32,6 +34,9 @@ global.debug = commander.debug; */ new Config().then(config => { async function run(config) { + if (config.uglify) { + commands.uglify = true; + } for (const task of Object.entries(commands)) { const name = task[0]; const enabled = task[1]; diff --git a/src/server.js b/src/server.js index 9c5ae2d..fa9bef7 100644 --- a/src/server.js +++ b/src/server.js @@ -46,9 +46,13 @@ class Server { use: [{path: null, static: null}], bowerPath: 'bower_components', nodeModulesPath: 'node_modules', + port: 3000, index: null}) { return new Promise((resolve, reject) => { if (config) { + this.appname = config.name || global.config.name; + this.entry = config.entry || global.config.entry; + this.port = config.port || global.config.port; this.handleOldOptions(config); if (config.use) { for (let use of config.use) { @@ -94,12 +98,15 @@ class Server { app.use('/license', express.static(files[0])); }); - server.listen(3000, error => { - if (error) { - return logger.warn(error); - } - logger.log(`${global.config.name}::serving from http://localhost:${config.port}/${config.entry.replace('/', '')}`); - opn(`http://localhost:${config.port}/${config.entry.replace('/', '')}`); + app.get('/', (request, response) => { + console.log(request.params); + }); + this.listen(); + + server.on('error', error => { + logger.warn(error); + this.port += 1; + return this.listen(); }); } else { reject(logger.warn(`${global.config.name}::server config not found [example](https://raw.githubusercontent.com/VandeurenGlenn/backed-cli/master/config/backed.json)`)); @@ -107,6 +114,13 @@ class Server { }); } + listen() { + server.listen(this.port, () => { + logger.log(`${this.appname}::serving from http://localhost:${this.port}/${this.entry.replace('/', '')}`); + opn(`http://localhost:${this.port}/${this.entry.replace('/', '')}`); + }); + } + /** * @param {string} path - location of the file * @param {string} alternate - returns when path is undefined diff --git a/src/tasks.js b/src/tasks.js index 8af02be..8c461aa 100644 --- a/src/tasks.js +++ b/src/tasks.js @@ -1,6 +1,8 @@ 'use strict'; const fs = require('backed-fs'); +const {readFile} = require('fs'); const webup = require('webup'); +const uglifyEs = require('uglify-es'); import builder from './../node_modules/backed-builder/src/builder.js'; import server from './server.js'; import watcher from './watcher.js'; @@ -16,6 +18,31 @@ const build = config => { }); }; +const read = src => { + return new Promise((resolve, reject) => { + readFile(src, 'utf-8', (error, code) => { + if (error) { + reject(error); + } + resolve(code); + }); + }); +}; + +const uglify = config => { + return new Promise((resolve, reject) => { + async function generator() { + for (const bundle of config.bundles) { + const file = await read(bundle.dest); + const result = uglifyEs.minify(file, {compress: true}); + const done = await fs.write({contents: result.code}, bundle.dest.replace('.js', '.min.js')); + } + resolve(); + } + generator(); + }); +}; + const copy = config => { return new Promise((resolve, reject) => { return fs.copySources(config.copy).then(() => { @@ -37,6 +64,7 @@ const watch = config => { export default { build, + uglify, copy, serve, watch diff --git a/yarn.lock b/yarn.lock index 54f14d2..2d6d58e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1188,7 +1188,7 @@ commander@2.9.0, commander@~2.9.0: dependencies: graceful-readlink ">= 1.0.0" -commander@^2.9.0: +commander@^2.9.0, commander@~2.11.0: version "2.11.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" @@ -5093,6 +5093,13 @@ typical@^2.6.0: version "2.6.1" resolved "https://registry.yarnpkg.com/typical/-/typical-2.6.1.tgz#5c080e5d661cbbe38259d2e70a3c7253e873881d" +uglify-es@^3.0.28: + version "3.0.28" + resolved "https://registry.yarnpkg.com/uglify-es/-/uglify-es-3.0.28.tgz#1cdedbbcdb7865223065281ad7b2347629851d4b" + dependencies: + commander "~2.11.0" + source-map "~0.5.1" + uglify-js@^2.6, uglify-js@^2.6.1: version "2.8.29" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"