Skip to content

Commit

Permalink
feat: added option snippet: boolean - fixes #1882
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jun 24, 2021
1 parent f6336ea commit 525fff9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 30 deletions.
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/browser-sync/lib/async.js
Expand Up @@ -152,7 +152,9 @@ module.exports = {
done(null, {
options: {
urls: utils.getUrlOptions(bs.options),
snippet: connectUtils.scriptTags(bs.options),
snippet: connectUtils.enabled(bs.options)
? connectUtils.scriptTags(bs.options)
: false,
scriptPaths: Immutable.fromJS(
connectUtils.clientScript(bs.options, true)
),
Expand Down
12 changes: 12 additions & 0 deletions packages/browser-sync/lib/connect-utils.js
Expand Up @@ -13,6 +13,18 @@ function getPath(options, relative, port) {
}

var connectUtils = {
/**
* Allow users to disable the Browsersync snippet
* @param {Immutable.Map} options
* @returns {Boolean}
*/
enabled: function(options) {
const userValue = options.get("snippet");
if (typeof userValue === "boolean") {
return userValue
}
return true
},
/**
* @param {Immutable.Map} options
* @returns {String}
Expand Down
14 changes: 8 additions & 6 deletions packages/browser-sync/lib/server/utils.js
Expand Up @@ -304,12 +304,14 @@ var serverUtils = {
);

// Snippet
rules.push(
snippetUtils.getRegex(
bs.options.get("snippet"),
bs.options.get("snippetOptions")
)
);
if (bs.options.get("snippet")) {
rules.push(
snippetUtils.getRegex(
bs.options.get("snippet"),
bs.options.get("snippetOptions")
)
);
}

// User
bs.options.get("rewriteRules").forEach(function(rule) {
Expand Down
1 change: 1 addition & 0 deletions packages/browser-sync/package.json
Expand Up @@ -23,6 +23,7 @@
"scripts": {
"build": "npm run build:server",
"build:server": "tsc",
"build:watch": "tsc --watch",
"env": "node ./test/env.js",
"lodash": "lodash include=isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject,set exports=node",
"prepublishOnly": "npm run build",
Expand Down
37 changes: 35 additions & 2 deletions packages/browser-sync/test/specs/e2e/e2e.options.snippet.js
Expand Up @@ -166,8 +166,41 @@ describe("E2E snippet: false", function() {
.set("accept", "text/html")
.expect(200)
.end(function(err, res) {
if (res.text.indexOf(instance.options.get("snippet")) > -1) {
return done(new Error("snippet present"));
if (res.text.indexOf("__bs_script__") > -1) {
return done(new Error("snippet present when it shouldn't be"));
}
done();
});
});
});

describe("E2E snippet: true", function() {
var instance;

before(function(done) {
browserSync.reset();
var config = {
server: {
baseDir: "test/fixtures"
},
open: false,
snippet: true,
};
instance = browserSync(config, done).instance;
});

after(function() {
instance.cleanup();
});

it("does not add the snippet when snippet: false", function(done) {
request(instance.server)
.get("/iframe.html")
.set("accept", "text/html")
.expect(200)
.end(function(err, res) {
if (res.text.indexOf("__bs_script__") === -1) {
return done(new Error("snippet absent when it shouldn't be"));
}
done();
});
Expand Down

0 comments on commit 525fff9

Please sign in to comment.