Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STENCIL-3361 Remove configId from assets urls #112

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions helpers/stylesheet.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
'use strict';

var _ = require('lodash');
const _ = require('lodash');

function helper(paper) {
paper.handlebars.registerHelper('stylesheet', function (assetPath) {
const options = arguments[arguments.length - 1];
const url = paper.cdnify(assetPath);
var attrs = {
rel: 'stylesheet'
};
const configId = paper.settings['theme_config_id'];
const path = configId ? assetPath.replace(/\.css$/, `-${configId}.css`) : assetPath;
const url = paper.cdnify(path);

let attrs = { rel: 'stylesheet' };

// check if there is any extra attribute
if (_.isObject(options.hash)) {
attrs = _.merge(attrs, options.hash);
}

if (!attrs.id) {
attrs.id = url;
}

attrs = _.map(attrs, function (value, key) {
return key + '="' + value + '"';
}).join(' ');

attrs = _.map(attrs, (value, key) => `${key}="${value}"`).join( ' ');

return '<link data-stencil-stylesheet href="' + url + '" ' + attrs + '>';
return `<link data-stencil-stylesheet href="${url}" ${attrs}>`;
});
}

Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ Paper.prototype.loadTranslations = function (acceptLanguage, callback) {
Paper.prototype.cdnify = function (path) {
var cdnUrl = this.settings['cdn_url'] || '';
var versionId = this.settings['theme_version_id'];
var configId = this.settings['theme_config_id'];
var sessionId = this.settings['theme_session_id'];
var protocolMatch = /(.*!?:)/;

Expand Down Expand Up @@ -252,7 +251,7 @@ Paper.prototype.cdnify = function (path) {
path = '/' + path;
}

if (!versionId || !configId) {
if (!versionId) {
return path;
}

Expand All @@ -261,10 +260,10 @@ Paper.prototype.cdnify = function (path) {
}

if (sessionId) {
return [cdnUrl, 'stencil', versionId, configId, 'e', sessionId, path].join('/');
return [cdnUrl, 'stencil', versionId, 'e', sessionId, path].join('/');
}

return [cdnUrl, 'stencil', versionId, configId, path].join('/');
return [cdnUrl, 'stencil', versionId, path].join('/');
};

/**
Expand Down
9 changes: 4 additions & 5 deletions test/helpers/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('cdn helper', function () {
var settings = {
cdn_url: 'https://cdn.bcapp/3dsf74g',
theme_version_id: '123',
theme_config_id: '3245',
};
var themeSettings = {
cdn: {
Expand All @@ -26,20 +25,20 @@ describe('cdn helper', function () {

it('should render the css cdn url', function (done) {
expect(c('{{cdn "assets/css/style.css"}}', context, settings, themeSettings))
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/3245/css/style.css');
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/css/style.css');

expect(c('{{cdn "/assets/css/style.css"}}', context, settings, themeSettings))
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/3245/css/style.css');
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/css/style.css');

done();
});

it('should render normal assets cdn url', function (done) {
expect(c('{{cdn "assets/js/app.js"}}', context, settings, themeSettings))
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/3245/js/app.js');
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/js/app.js');

expect(c('{{cdn "assets/img/image.jpg"}}', context, settings, themeSettings))
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/3245/img/image.jpg');
.to.be.equal('https://cdn.bcapp/3dsf74g/stencil/123/img/image.jpg');

done();
});
Expand Down
29 changes: 18 additions & 11 deletions test/helpers/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,39 @@ var Code = require('code'),
it = lab.it;

function c(template, settings) {
return new Paper(settings).loadTemplatesSync({template: template}).render('template', {});
return new Paper(settings).loadTemplatesSync({ template }).render('template', {});
}

describe('stylesheet helper', function () {
describe('stylesheet helper', () => {
var settings = {
cdn_url: 'https://cdn.bcapp/hash',
theme_version_id: '1',
theme_config_id: '2',
theme_version_id: '123',
theme_config_id: 'xyz',
};
it('should render a link tag with the cdn ulr and stencil-stylesheet data tag', function (done) {
it('should render a link tag with the cdn ulr and stencil-stylesheet data tag', done => {
expect(c('{{{stylesheet "assets/css/style.css"}}}', settings))
.to.be.equal('<link data-stencil-stylesheet href="https://cdn.bcapp/hash/stencil/1/2/css/style.css" rel="stylesheet" id="https://cdn.bcapp/hash/stencil/1/2/css/style.css">');
.to.be.equal('<link data-stencil-stylesheet href="https://cdn.bcapp/hash/stencil/123/css/style-xyz.css" rel="stylesheet">');

done();
});

it('should render a link tag and all extra attributes with no cdn url', function (done) {
expect(c('{{{stylesheet "assets/css/style.css" rel="something" class="myStyle"}}}', {}))
.to.be.equal('<link data-stencil-stylesheet href="/assets/css/style.css" rel="something" class="myStyle" id="/assets/css/style.css">');
it('should render a link tag and all extra attributes with no cdn url', done => {
expect(c('{{{stylesheet "assets/css/style.css" fuck rel="something" class="myStyle"}}}', {}))
.to.be.equal('<link data-stencil-stylesheet href="/assets/css/style.css" rel="something" class="myStyle">');

done();
});

it('should render a link with empty href', function (done) {
it('should render a link with empty href', done => {
expect(c('{{{stylesheet "" }}}'))
.to.be.equal('<link data-stencil-stylesheet href="" rel="stylesheet" id="">');
.to.be.equal('<link data-stencil-stylesheet href="" rel="stylesheet">');

done();
});

it('should add configId to the filename', done => {
expect(c('{{{stylesheet "assets/css/style.css" }}}', { theme_config_id: 'foo' }))
.to.be.equal('<link data-stencil-stylesheet href="/assets/css/style-foo.css" rel="stylesheet">');

done();
});
Expand Down
6 changes: 2 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ describe('cdnify()', function () {
var paper = new Paper({
cdn_url: 'http://cdn.example.com/foo',
theme_version_id: '123',
theme_config_id: '234',
});

expect(paper.cdnify('/assets/image.jpg'))
.to.be.equal('http://cdn.example.com/foo/stencil/123/234/image.jpg');
.to.be.equal('http://cdn.example.com/foo/stencil/123/image.jpg');

done();
});
Expand All @@ -107,12 +106,11 @@ describe('cdnify()', function () {
var paper = new Paper({
cdn_url: 'http://cdn.example.com/foo',
theme_version_id: '123',
theme_config_id: '234',
theme_session_id: '345',
});

expect(paper.cdnify('/assets/image.jpg'))
.to.be.equal('http://cdn.example.com/foo/stencil/123/234/e/345/image.jpg');
.to.be.equal('http://cdn.example.com/foo/stencil/123/e/345/image.jpg');

done();
});
Expand Down