Skip to content

Commit

Permalink
Refactored code to reuse helper instead of having a separate method f…
Browse files Browse the repository at this point in the history
…or inline tag writers. Merged tests into helper.
  • Loading branch information
blakevanlan committed Apr 10, 2016
1 parent 6ef4eb6 commit 06ac2b7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 123 deletions.
14 changes: 6 additions & 8 deletions index.js
Expand Up @@ -12,12 +12,11 @@ var connectAssets = module.exports = function (options, configureCallback) {
var waiting = [];

options.helperContext.css = assets.helper(tagWriters.css, "css");
options.helperContext.cssInline = assets.helper(tagWriters.cssInline, "css");
options.helperContext.js = assets.helper(tagWriters.js, "js");
options.helperContext.jsInline = assets.helper(tagWriters.jsInline, "js");
options.helperContext.assetPath = assets.helper(tagWriters.noop);

options.helperContext.css_inline = assets.helperInline(tagWriters.css_inline);
options.helperContext.js_inline = assets.helperInline(tagWriters.js_inline);

if (configureCallback) {
configureCallback(assets);
}
Expand Down Expand Up @@ -103,10 +102,9 @@ var parseUrl = function (string) {
};

var tagWriters = {
css: function (url, attr) { return '<link rel="stylesheet" href="' + url + '"' + pasteAttr(attr) + ' />'; },
js: function (url, attr) { return '<script src="' + url + '"' + pasteAttr(attr) + '></script>'; },
css: function (url, contentProvider, attr) { return '<link rel="stylesheet" href="' + url + '"' + pasteAttr(attr) + ' />'; },
cssInline: function (url, contentProvider, attr) { return '<style' + pasteAttr(attr) + '>' + contentProvider() + '</style>'; },
js: function (url, contentProvider, attr) { return '<script src="' + url + '"' + pasteAttr(attr) + '></script>'; },
jsInline : function (url, contentProvider, attr) { return '<script' + pasteAttr(attr) + '>' + contentProvider() + '</script>'; },
noop: function (url) { return url; },

css_inline: function (buffer, attr) { return '<style' + pasteAttr(attr) + '>' + buffer + '</style>'; },
js_inline : function (buffer, attr) { return '<script' + pasteAttr(attr) + '>' + buffer + '</script>'; },
};
47 changes: 19 additions & 28 deletions lib/assets.js
Expand Up @@ -182,7 +182,7 @@ Assets.prototype.helper = function (tagWriter, ext) {
throw new Error("Asset '" + path + "' not found in " + searchPath);
}

var getTag = function (asset) {
var getTag = function (path, asset) {
var servePath = instance.options.servePath;
var path = servePath + "/" + (asset.logicalPath || asset.logical_path);
var attributes = parseAttributes(options);
Expand All @@ -195,61 +195,52 @@ Assets.prototype.helper = function (tagWriter, ext) {
path = path.replace(/(\.[^.]+)$/, "-" + asset.digest + "$1");
}

return tagWriter(path, attributes);
var contentProvider = instance.getAssetContentSync.bind(instance, asset);
return tagWriter(path, contentProvider, attributes);
};

if (!instance.options.bundle && asset.type === "bundled") {
var assets = asset.toArray();
var tags = [];

for (var i = 0; i < assets.length; i++) {
tags.push(getTag(assets[i]));
tags.push(getTag(path, assets[i]));
};

return tags.join("\n");
}
else {
return getTag(asset);
return getTag(path, asset);
}
};
};

Assets.prototype.helperInline = function (tagWriter, ext) {
var instance = this,
extRE = new RegExp("\\." + ext + "$")
;

return function (given, options) {
var asset, path;

path = (ext && !extRE.test(given))
? given + "." + ext
: given
;

try {
asset = instance.getAssetByPath(path, { bundle: true });
} catch (err) {}

if (!asset)
throw new Error("Asset '" + path + "' not found");

return tagWriter(asset.buffer, parseAttributes(options));
};
};

Assets.prototype.getAssetByPath = function (pathname, options) {
var asset;
if (this.manifest && this.manifest.assets[pathname]) {
var assetTitle = this.manifest.assets[pathname];
asset = this.manifest.files[assetTitle];

}
if (!asset && this.options.compile) {
return this.environment.findAsset(pathname, options);
}
return asset;
};

Assets.prototype.getAssetContentSync = function (asset) {
if (asset.buffer) {
return asset.buffer;
}
if (this.manifest) {
var path = (asset.logicalPath || asset.logical_path);
if (path && this.manifest.assets[path]) {
return fs.readFileSync(pathModule.join(this.options.buildDir, this.manifest.assets[path]));
}
}
return "";
};

var isInvalidPath = function (pathname) {
return pathname.indexOf("..") !== -1 || pathname.indexOf("\u0000") !== -1;
};
Expand Down
93 changes: 93 additions & 0 deletions test/helpers.js
@@ -1,18 +1,23 @@
var expect = require("expect.js");
var mocha = require("mocha");
var assets = require("..");
var rmrf = require("./testHelpers/rmrf");

describe("helper functions", function () {
it("do not pollute global scope if helperContext is passed", function () {
var ctx = {};
var instance = assets({ helperContext: ctx });

expect(ctx.css).to.be.a("function");
expect(ctx.cssInline).to.be.a("function");
expect(ctx.js).to.be.a("function");
expect(ctx.jsInline).to.be.a("function");
expect(ctx.assetPath).to.be.a("function");

expect(global.css).to.equal(undefined);
expect(global.cssInline).to.equal(undefined);
expect(global.js).to.equal(undefined);
expect(global.jsInline).to.equal(undefined);
expect(global.assetPath).to.equal(undefined);
});

Expand Down Expand Up @@ -89,7 +94,49 @@ describe("helper functions", function () {
'<link rel="stylesheet" href="/assets/asset.css" data-turbolinks-track />'
);
});
});

describe("cssInline", function () {
it("returns a <style> tag for each asset found (separated by \\n)", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css" });
var tag = ctx.cssInline("depends-on-blank.css");
expect(tag).to.equal(
"<style>\n</style>\n" +
"<style>/*(=) require blank */\n\n</style>"
);
});

it("should serve correct asset even if extention is not supplied", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css" });
var tag = ctx.cssInline("unminified");
expect(tag).to.equal("<style>body\n{\n background-color: #000000;\n color: #ffffff;\n}\n\na\n{\n display: none;\n}\n\n</style>");
});

it("should have additional attributes in result tag", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css" });
var tag = ctx.cssInline("asset.css", { 'data-turbolinks-track': true });
expect(tag).to.equal("<style data-turbolinks-track>\n</style>");
});

it("returns a single <style> tag containing the bundled asset when bundle=true", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css", bundle: true });
var tag = ctx.cssInline("depends-on-blank.css");
expect(tag).to.equal("<style>\n/*(=) require blank */\n\n</style>");
});

it("should serve asset from manifest", function (done) {
var dir = "testBuiltAssets";
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css", build: true, buildDir: dir, compile: true });
var tag = ctx.cssInline("unminified");

expect(tag).to.equal("<style>body\n{\n background-color: #000000;\n color: #ffffff;\n}\n\na\n{\n display: none;\n}\n\n</style>");
rmrf(dir, done);
});
});

describe("js", function () {
Expand Down Expand Up @@ -125,6 +172,52 @@ describe("helper functions", function () {
});
});

describe("jsInline", function () {
it("returns a <script> tag for each asset found (separated by \\n)", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js" });
var script = ctx.jsInline("depends-on-blank.js");

expect(script).to.equal(
"<script>\n;\n</script>\n" +
"<script>//(=) require blank\n;\n</script>"
);
});

it("should serve correct asset even if extention is not supplied", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js" });
var script = ctx.jsInline("asset.js");

expect(script).to.equal("<script>\n;\n</script>");
});

it("should have additional attributes in result tag", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js" });
var script = ctx.jsInline("asset.js", { async: true });

expect(script).to.equal("<script async>\n;\n</script>");
});

it("returns a single <script> tag containing the bundled asset when bundle=true", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js", bundle: true });
var tag = ctx.jsInline("depends-on-blank.js");
expect(tag).to.equal("<script>\n;\n//(=) require blank\n;\n</script>");
});

it("should serve asset from manifest", function (done) {
var dir = "testBuiltAssets";
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js", build: true, buildDir: dir, compile: true });
var tag = ctx.jsInline("depends-on-blank.js");

expect(tag).to.equal("<script>\n;\n//(=) require blank\n;\n</script>");
rmrf(dir, done);
});
});

describe("assetPath", function () {
it("returns a file path for each asset found (separated by \\n)", function () {
var ctx = {};
Expand Down
87 changes: 0 additions & 87 deletions test/inlineHelpers.js

This file was deleted.

0 comments on commit 06ac2b7

Please sign in to comment.