forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Split asset helper for admin & theme
refs TryGhost#7488, TryGhost#7491 - At the moment, we use the same asset helper function for both admin and theme - Both functions are (rightly or wrongly) a wrapper around meta.getAssetUrl() - The usecases in these contexts are v. different: - asset for the admin is used only in the generated index.html file - ghost="true" is always true in this case, except for the favicon which is ALREADY a separate case - minifyInProduction is only used to the admin panel, not documented or supposed to be used for themes - For themes, the helper doesn't take any arguments, it should just call to getAssetUrl() If we do want to introduce some sort of .min path adjuster we should give it a better name! - For the admin panel, minifyInProduction means exactly what it says - minify if env === production, it makes no sense IMO to move this to config because we control it and also plus "minifyAssets" made it sound like we had an asset pipeline when all this did was change the output from .js to .min.js or .css to .min.css
- Loading branch information
Showing
8 changed files
with
152 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// # Admin Asset helper | ||
// Usage: `{{asset "css/screen.css"}}`, `{{asset "css/screen.css" ghost="true"}}` | ||
// | ||
// Returns the path to the specified asset. The ghost flag outputs the asset path for the Ghost admin | ||
|
||
var config = require('../config'), | ||
getAssetUrl = require('../data/meta/asset_url'), | ||
hbs = require('express-hbs'), | ||
adminHbs = hbs.create(), | ||
helpers = {}; | ||
|
||
helpers.adminAsset = function adminAsset(path, options) { | ||
// This helper is only ever used in admin | ||
var isAdmin = true, | ||
minify = false; | ||
|
||
if (options && options.hash && options.hash.minifyInProduction) { | ||
// we deliberately use config.get('env') here because the setting is referring explicitly to production | ||
// this is exclusively for the admin panel where we control the logic, not config | ||
minify = config.get('env') === 'production'; | ||
} | ||
|
||
return new hbs.handlebars.SafeString( | ||
getAssetUrl(path, isAdmin, minify) | ||
); | ||
}; | ||
|
||
module.exports.engine = function setupHandlebars() { | ||
adminHbs.registerHelper('asset', helpers.adminAsset); | ||
return adminHbs.express3(); | ||
}; | ||
|
||
// exported for tests only | ||
module.exports._helpers = helpers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,5 @@ | |
"privacy": { | ||
"useTinfoil": true, | ||
"useStructuredData": true | ||
}, | ||
"minifyAssets": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,5 @@ | |
"privacy": { | ||
"useTinfoil": true, | ||
"useStructuredData": true | ||
}, | ||
"minifyAssets": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
var should = require('should'), | ||
sinon = require('sinon'), | ||
configUtils = require('../utils/configUtils'), | ||
settingsCache = require('../../server/settings/cache'), | ||
|
||
adminHbs = require('../../server/admin/handlebars'), | ||
helpers = adminHbs._helpers, | ||
|
||
sandbox = sinon.sandbox.create(); | ||
|
||
describe('ADMIN {{asset}} helper', function () { | ||
var rendered, localSettingsCache = {}; | ||
|
||
before(function () { | ||
configUtils.set({assetHash: 'abc'}); | ||
|
||
sandbox.stub(settingsCache, 'get', function (key) { | ||
return localSettingsCache[key]; | ||
}); | ||
}); | ||
|
||
after(function () { | ||
configUtils.restore(); | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('no subdirectory', function () { | ||
it('handles favicon correctly', function () { | ||
rendered = helpers.adminAsset('favicon.ico'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/favicon.ico'); | ||
}); | ||
|
||
it('handles custom favicon correctly', function () { | ||
localSettingsCache.icon = '/content/images/favicon.png'; | ||
|
||
// png | ||
rendered = helpers.adminAsset('favicon.png'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/favicon.ico'); | ||
|
||
localSettingsCache.icon = '/content/images/favicon.ico'; | ||
|
||
// ico | ||
rendered = helpers.adminAsset('favicon.ico'); | ||
should.exist(rendered); | ||
}); | ||
|
||
it('handles shared assets correctly', function () { | ||
localSettingsCache.icon = ''; | ||
|
||
rendered = helpers.adminAsset('shared/asset.js'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/shared/asset.js?v=abc'); | ||
}); | ||
|
||
it('handles admin assets correctly', function () { | ||
rendered = helpers.adminAsset('js/asset.js'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/ghost/assets/js/asset.js?v=abc'); | ||
}); | ||
}); | ||
|
||
describe('with /blog subdirectory', function () { | ||
before(function () { | ||
configUtils.set({url: 'http://testurl.com/blog'}); | ||
}); | ||
|
||
it('handles favicon correctly', function () { | ||
rendered = helpers.adminAsset('favicon.ico'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/blog/favicon.ico'); | ||
}); | ||
|
||
it('handles custom favicon correctly', function () { | ||
localSettingsCache.icon = '/content/images/favicon.png'; | ||
|
||
// png | ||
rendered = helpers.adminAsset('favicon.png'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/blog/favicon.ico'); | ||
|
||
localSettingsCache.icon = '/content/images/favicon.ico'; | ||
|
||
// ico | ||
rendered = helpers.adminAsset('favicon.ico'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/blog/favicon.ico'); | ||
}); | ||
|
||
it('handles shared assets correctly', function () { | ||
rendered = helpers.adminAsset('shared/asset.js'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/blog/shared/asset.js?v=abc'); | ||
}); | ||
|
||
it('handles admin assets correctly', function () { | ||
rendered = helpers.adminAsset('js/asset.js'); | ||
should.exist(rendered); | ||
String(rendered).should.equal('/blog/ghost/assets/js/asset.js?v=abc'); | ||
}); | ||
|
||
configUtils.restore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters