From fa080e6546717ebec3b5032f8ce05c4863916376 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 26 Jul 2015 13:35:39 -0700 Subject: [PATCH] browserify workaround to avoid changing code. see #158. --- browser/transform.js | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 5 ++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 browser/transform.js diff --git a/browser/transform.js b/browser/transform.js new file mode 100644 index 0000000..2d847e4 --- /dev/null +++ b/browser/transform.js @@ -0,0 +1,38 @@ +var Transform = require('stream').Transform; + +function transform(target) { + var data = ''; + var tr = new Transform; + tr._transform = function(chunk, encoding, callback) { + if (!target) { + return callback(null, chunk); + } + data += chunk; + return callback(null, chunk); + }; + tr._flush = function(callback) { + if (!target) { + return callback(); + } + tr.push(compile(data)); + return callback(); + }; + return tr; +} + +function compile(data) { + var out = ''; + var names = /widget\.classes = (\[[^\]]+\]);/.exec(data)[1]; + names = JSON.parse(names.replace(/'/g, '"')).forEach(function(name) { + name = name.toLowerCase(); + out += '\nrequire(\'./widgets/' + name + '\');'; + }); + return out; +} + +module.exports = function(file) { + if (!~file.indexOf('widget.js')) { + return transform(); + } + return transform(true); +}; diff --git a/package.json b/package.json index 520fad7..558cd55 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,8 @@ "homepage": "https://github.com/chjj/blessed", "bugs": { "url": "http://github.com/chjj/blessed/issues" }, "keywords": ["curses", "tui", "tput", "terminfo", "termcap"], - "tags": ["curses", "tui", "tput", "terminfo", "termcap"] + "tags": ["curses", "tui", "tput", "terminfo", "termcap"], + "browserify": { + "transform": ["./browser/transform.js"] + } }