Skip to content

Commit

Permalink
Merge branch 'feature/allow-for-inline-tags' of git://github.com/OOSh…
Browse files Browse the repository at this point in the history
…oppingnl/connect-assets into OOShoppingnl-feature/allow-for-inline-tags
  • Loading branch information
blakevanlan committed Apr 10, 2016
2 parents 48778cb + eb7466a commit a571450
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -15,6 +15,9 @@ var connectAssets = module.exports = function (options, configureCallback) {
options.helperContext.js = assets.helper(tagWriters.js, "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 @@ -101,5 +104,8 @@ 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>'; },
noop: function (url) { return url; }
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>'; },
};
26 changes: 25 additions & 1 deletion lib/assets.js
Expand Up @@ -9,7 +9,7 @@ var Assets = module.exports = function (Mincer, options) {
options = Mincer
Mincer = require('mincer')
}

this.options = options;

if (this.options.compile) {
Expand Down Expand Up @@ -214,6 +214,30 @@ Assets.prototype.helper = function (tagWriter, ext) {
};
};

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]) {
Expand Down
87 changes: 87 additions & 0 deletions test/inlineHelpers.js
@@ -0,0 +1,87 @@
var expect = require("expect.js");
var mocha = require("mocha");
var assets = require("..");

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

expect(ctx.css_inline).to.be.a("function");
expect(ctx.js_inline).to.be.a("function");

expect(global.css_inline).to.equal(undefined);
expect(global.js_inline).to.equal(undefined);
});

describe("css", function () {
it("throw an Error if asset is not found", function () {
var ctx = {};
var instance = assets({ helperContext: ctx });

expect(function () {
ctx.js_inline("non-existant-asset.js");
}).to.throwError(/'non-existant-asset.js' not found/i);
});

it("returns a <style> tag", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css" });
var tag = ctx.css_inline("unminified.css");

expect(tag).to.match(/<style>([\s\S]+?)<\/style>/m);
});

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.css_inline("unminified");

expect(tag).to.match(/<style>([\s\S]+?)<\/style>/m);
});

it("should have additional attributes in result tag", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/css" });
var tag = ctx.css_inline("asset.css", { 'data-turbolinks-track': true });

expect(tag).to.match(/data-turbolinks-track>/);
});
});

describe("js", function () {
it("throw an Error if asset is not found", function () {
var ctx = {};
var instance = assets({ helperContext: ctx });

expect(function () {
ctx.js_inline("non-existant-asset.js");
}).to.throwError(/'non-existant-asset.js' not found/i);
});

it("returns a <style> tag", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js" });
var tag = ctx.js_inline("simple.js");

expect(tag).to.match(/<script>var a = true;<\/script>/m);
});

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

expect(tag).to.match(/<script>var a = true;<\/script>/m);
});

it("should have additional attributes in result tag", function () {
var ctx = {};
var instance = assets({ helperContext: ctx, paths: "test/assets/js" });
var tag = ctx.js_inline("unminified.js", { 'data-turbolinks-track': true });

expect(tag).to.match(/data-turbolinks-track>/);
});
});

});

0 comments on commit a571450

Please sign in to comment.