From 480378649db2b87b4c627ddc6ef772d0307115e5 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Sun, 29 Apr 2018 20:54:55 +0100 Subject: [PATCH] feat: (client-js) allow functions in client:js hook --- lib/client.js | 35 ++++++++++++++++++++--------------- lib/connect-utils.js | 1 + lib/hooks.js | 6 +++--- lib/snippet.js | 14 ++++++++------ 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/client.js b/lib/client.js index 8efbd34c9..b8ca19274 100644 --- a/lib/client.js +++ b/lib/client.js @@ -48,6 +48,20 @@ function notModified(res) { res.end(); } +function processItems(items) { + return [].concat(items) + .filter(Boolean) + .reduce((stringOutput, item) => { + if (typeof item === 'string') { + return stringOutput + item; + } + if (typeof item === 'function') { + return stringOutput + item(); + } + return stringOutput; + }, ""); +} + /** * Public method for returning either a middleware fn * or the content as a string @@ -57,13 +71,11 @@ function notModified(res) { * @returns {*} */ function init(options, requestBody, type) { - var gzipCached; - /** * If the user asked for a file, simply return the string. */ if (type && type === "file") { - return requestBody; + return processItems(requestBody); } /** @@ -74,13 +86,12 @@ function init(options, requestBody, type) { * default to using the uncompressed string * @type {String} */ - var output = requestBody; + var output = processItems(requestBody); /** * Set the appropriate headers for caching */ setHeaders(res, output); - if (isConditionalGet(req) && fresh(req.headers, res._headers)) { return notModified(res); } @@ -91,16 +102,10 @@ function init(options, requestBody, type) { */ if (supportsGzip(req)) { res.setHeader("Content-Encoding", "gzip"); - - if (!gzipCached) { - var buf = new Buffer(output, "utf-8"); - zlib.gzip(buf, function(_, result) { - gzipCached = result; - res.end(result); - }); - } else { - res.end(gzipCached); - } + var buf = new Buffer(output, "utf-8"); + zlib.gzip(buf, function(_, result) { + res.end(result); + }); } else { res.end(output); } diff --git a/lib/connect-utils.js b/lib/connect-utils.js index 325364a4c..dcc7c440e 100644 --- a/lib/connect-utils.js +++ b/lib/connect-utils.js @@ -123,6 +123,7 @@ var connectUtils = { template = template .replace("%config%", JSON.stringify(clientConfig.toJS())) + .replace("%options%", JSON.stringify(options)) .replace("%url%", url); return template; diff --git a/lib/hooks.js b/lib/hooks.js index 1a40fd854..8e97d0558 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -13,9 +13,9 @@ module.exports = { "client:js": function(hooks, data) { var js = snippetUtils.getClientJs(data.port, data.options); - return hooks.reduce(function(joined, hook) { - return joined + hook; - }, js); + return hooks.reduce(function(acc, hook) { + return acc.concat(hook); + }, [js]); }, /** * @this {BrowserSync} diff --git a/lib/snippet.js b/lib/snippet.js index be547dca8..8620bcb46 100644 --- a/lib/snippet.js +++ b/lib/snippet.js @@ -91,12 +91,14 @@ var snippetUtils = { * @returns {String} */ getClientJs: function(port, options) { - var script = options.get("minify") ? "index.min.js" : "index.js"; - var client = fs.readFileSync( - path.join(__dirname, "..", "client", "dist", script), - "utf8" - ); - return [connectUtils.socketConnector(options), client].join(";\n"); + return () => { + const script = options.get("minify") ? "index.min.js" : "index.js"; + const client = fs.readFileSync( + path.join(__dirname, "..", "client", "dist", script), + "utf8" + ); + return [connectUtils.socketConnector(options), client].join(";\n"); + } } };