Skip to content

Commit

Permalink
feat: (client-js) allow functions in client:js hook
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 29, 2018
1 parent e4754c9 commit 4803786
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
35 changes: 20 additions & 15 deletions lib/client.js
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions lib/connect-utils.js
Expand Up @@ -123,6 +123,7 @@ var connectUtils = {

template = template
.replace("%config%", JSON.stringify(clientConfig.toJS()))
.replace("%options%", JSON.stringify(options))
.replace("%url%", url);

return template;
Expand Down
6 changes: 3 additions & 3 deletions lib/hooks.js
Expand Up @@ -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}
Expand Down
14 changes: 8 additions & 6 deletions lib/snippet.js
Expand Up @@ -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");
}
}
};

Expand Down

0 comments on commit 4803786

Please sign in to comment.