diff --git a/README.md b/README.md index b12e190..6b09b15 100755 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ $ nodemcu-tool mkfs --port=/dev/ttyUSB0 **Hint** include the native [encoder Module](http://nodemcu.readthedocs.io/en/master/en/modules/encoder/) into your firmware to speed-up the uploading by factor 4..10! ```shell -$ nodemcu-tool upload --port=/dev/ttyUSB0 --optimize helloworld.lua +$ nodemcu-tool upload --port=/dev/ttyUSB0 --minify helloworld.lua [NodeMCU-Tool] Connected [NodeMCU] Version: 0.9.5 | ChipID: 0xd1aa | FlashID: 0x1640e0 [NodeMCU-Tool] Uploading "main.lua" ... @@ -230,14 +230,14 @@ This will create a JSON based configuration file named `.nodemcutool` in your ** ### Example Configuration ### -In this Example, the baudrate is changed to 19.2k and COM3 is selected as default port. Additionally the `--optimize` and `--compile` flags are set permanently. +In this Example, the baudrate is changed to 19.2k and COM3 is selected as default port. Additionally the `--minify` and `--compile` flags are set permanently. ```json { "baudrate": "19200", "port": "COM3", "compile": true, - "optimize": true, + "minify": true, "keeppath": true } ``` @@ -249,7 +249,8 @@ All configuration options are **optional** * **baudrate** (int) - the default baudrate in bits per second * **port** (string) - the comport to use * **compile** (boolean) - compile lua files after upload -* **optimize** (boolean) - optimize files before uploading +* **minify** (boolean) - minifies files before uploading +* **optimize** (boolean) - optimize files before uploading (Deprecated! Use minify instead.) * **keeppath** (boolean) - keep the relative file path in the destination filename (i.e: static/test.html will be named static/test.html) @@ -325,7 +326,7 @@ Of course, check the [Examples](docs/Examples.md) file (tool usage) as well as t #### The serial file transfer is pretty slow #### By default, the serial connection uses a 9600 baud with 8N1 - this means maximal 960 bytes/s raw data rate. Due to the limitations of a line-wise file upload, these maximal transfer rate cannot be reached, because every line has to be processed by the lua interpreter and NodeMCU-Tool is waiting for it's response. -It's recommended to use the `--optimize` flag to strip whitespaces before uploading. Additionally, newer firmware versions `1.x.x` using an auto-baudrate detection algorithm - this means you can increase the baudrate to e.g. 115200 `--baud 115200` to speed up the transfer +It's recommended to use the `--minify` flag to minify the code before uploading. Additionally, newer firmware versions `1.x.x` using an auto-baudrate detection algorithm - this means you can increase the baudrate to e.g. 115200 `--baud 115200` to speed up the transfer Additionally include the native [encoder Module](http://nodemcu.readthedocs.io/en/master/en/modules/encoder/) into your firmware to speed-up the uploading by factor 4..10! diff --git a/bin/nodemcu-tool.js b/bin/nodemcu-tool.js index c860906..29ea245 100755 --- a/bin/nodemcu-tool.js +++ b/bin/nodemcu-tool.js @@ -69,6 +69,7 @@ function cliPrepare(options){ // command specific flags optimize: options.optimize || false, + minify: options.minify || false, compile: options.compile || false, keeppath: options.keeppath || false, remotename: options.remotename || null, @@ -91,6 +92,7 @@ function cliPrepare(options){ // extract values defaultConfig.baudrate = d.baudrate || defaultConfig.baudrate; defaultConfig.port = d.port || defaultConfig.port; + defaultConfig.minify = (d.minify && d.minify === true); defaultConfig.optimize = (d.optimize && d.optimize === true); defaultConfig.compile = (d.compile && d.compile === true); defaultConfig.keeppath = (d.keeppath && d.keeppath === true); @@ -169,6 +171,9 @@ _cli .command('upload [files...]') .description('Upload Files to NodeMCU (ESP8266) target') + // file minification + .option('-m, --minify', 'Minifies the file before uploading', false) + // file cleanup .option('-o, --optimize', 'Removes comments and empty lines from file before uploading', false) @@ -346,6 +351,7 @@ _cli _logger.error(err); }else{ // set defaults + data.minify = false; data.optimize = false; data.compile = false; data.keeppath = false; diff --git a/docs/CommandReference.md b/docs/CommandReference.md index d7ecd35..422352b 100755 --- a/docs/CommandReference.md +++ b/docs/CommandReference.md @@ -78,17 +78,18 @@ The most important task of this tool: upload local files to the module. **Options** -* `--optimize` | Remove Comments, Whitespaces and empty lines from the file before upload +* `--optimize` | Deprecated! - Remove Comments, Whitespaces and empty lines from the file before upload +* `--minify` | Minifies the code before upload * `--compile` | Compiles the uploaded .lua file into executable bytecode and removes the source .lua file (performance) * `--keeppath` | Keeps the relative file path in the destination filename (i.e: static/test.html will be named static/test.html) * `--remotename` | Set the destination file name **Example 1** -Upload and optimize the file "test.lua" +Upload and minify the file "test.lua" ```shell -$ nodemcu-tool --port=/dev/ttyUSB1 --optimize --compile upload test.lua +$ nodemcu-tool --port=/dev/ttyUSB1 --minify --compile upload test.lua [NodeMCU-Tool] Connected [NodeMCU] Version: 0.9.5 | ChipID: 0xd1aa | FlashID: 0x1640e0 [NodeMCU-Tool] Uploading "toolkit/test.lua" ... diff --git a/lib/LuaMinifier.js b/lib/LuaMinifier.js new file mode 100755 index 0000000..707a2d4 --- /dev/null +++ b/lib/LuaMinifier.js @@ -0,0 +1,13 @@ +var luamin = require('luamin'); +// strip comments and whitespaces from lua content +function minifyLuaContent(rawContent){ + // apply minification + var t = luamin.minify((rawContent.toString('utf-8'))); + + // re-convert to buffer + return new Buffer(t, 'utf-8'); +}; + +module.exports = { + minify: minifyLuaContent +}; diff --git a/lib/LuaOptimizer.js b/lib/LuaOptimizer.js index 38bd788..9956fd5 100755 --- a/lib/LuaOptimizer.js +++ b/lib/LuaOptimizer.js @@ -1,6 +1,8 @@ - +var _logger = require('logging-facility').getLogger('NodeMCU-Connector'); // strip comments and whitespaces from lua content function optimizeLuaContent(rawContent){ + // Deprication warning + _logger.warn('--optimize is deprecated. Please use --minify instead.') // apply optimizations var t = rawContent.toString('utf-8') .replace(/--.*$/gim, '') @@ -14,4 +16,4 @@ function optimizeLuaContent(rawContent){ module.exports = { optimize: optimizeLuaContent -}; \ No newline at end of file +}; diff --git a/lib/NodeMcuConnector.js b/lib/NodeMcuConnector.js index 9797901..9145bd6 100755 --- a/lib/NodeMcuConnector.js +++ b/lib/NodeMcuConnector.js @@ -3,6 +3,7 @@ var _serialport = require('serialport'); var _fs = require('fs'); var _path = require('path'); var _luaOptimizer = require('./LuaOptimizer'); +var _luaMinifier = require('./LuaMinifier'); var _luaCommandBuilder = require('./LuaCommandBuilder'); // NodeMCU-Connector Context Logger @@ -205,9 +206,11 @@ NodeMcuConnector.prototype.upload = function(localName, remoteName, options, com // get file content var rawContent = _fs.readFileSync(localName); - - // remove lua comments and empty lines ? - if (options.optimize && _path.extname(localName).toLowerCase() == '.lua'){ + if (options.minify && _path.extname(localName).toLowerCase() == '.lua'){ + // minify + rawContent = _luaMinifier.minify(rawContent); + } else if (options.optimize && _path.extname(localName).toLowerCase() == '.lua'){ + // remove lua comments and empty lines ? // apply optimizations rawContent = _luaOptimizer.optimize(rawContent); } diff --git a/package.json b/package.json index 61bfad6..7eae700 100755 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "colors": "^1.1.2", "commander": "^2.9.0", "logging-facility": "^1.1.0", + "luamin": "^1.0.4", "prompt": "^0.2.14", "serialport": "^4.0.0" }