Added card-asset config with sensible default

- This comment removes the block on themes controlling card assets via config
- It also changes the default behaviour from "false" config (doing nothing) to excluding bookmark and gallery card assets
- This is essentially the same thing, as only bookmark and gallery card assets exist at the moment, but it's being done because it makes this feature future-proof for all theme developers.
- As we add new cards, all themes will automatically get the assets to make them work
- As theme developers want to, they can create their own custom assets and disble assets for any cards they support by adding them to the exclude list
- They can also remove any custom code they currently have to support bookmark and gallery cards, and set card_assets: true in package.json to use the defaults instead
This commit is contained in:
Hannah Wolfe 2021-10-19 17:33:20 +01:00
parent e4c1e0d938
commit e25f1df0ae
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55
5 changed files with 32 additions and 16 deletions

View File

@ -3,16 +3,17 @@ const _ = require('lodash');
const path = require('path');
const fs = require('fs').promises;
const defaultConfig = false;
class CardAssetService {
constructor(options = {}) {
// @TODO: use our config paths concept
this.src = options.src || path.join(__dirname, '../../src/cards');
this.dest = options.dest || path.join(__dirname, '../../public');
this.config = 'config' in options ? options.config : defaultConfig;
this.minifier = new Minifier({src: this.src, dest: this.dest});
if ('config' in options) {
this.config = options.config;
}
this.files = [];
}

View File

@ -1,4 +1,6 @@
{
"posts_per_page": 5,
"card_assets": false
"card_assets": {
"exclude": ["bookmark", "gallery"]
}
}

View File

@ -9,9 +9,5 @@ module.exports.create = function configLoader(packageJson) {
config = _.assign(config, _.pick(packageJson.config, allowedKeys));
}
// @TOD0: remove this guard when we're ready
// Temporary override to prevent themes from controlling this until we're ready
config.card_assets = defaultConfig.card_assets;
return config;
};

View File

@ -9,6 +9,7 @@ const cardAssetService = require('../../../../core/frontend/services/card-assets
const CardAssetService = require('../../../../core/frontend/services/card-assets/service');
const themeEngine = require('../../../../core/frontend/services/theme-engine');
const themeDefaults = require('../../../../core/frontend/services/theme-engine/config/defaults.json');
describe('Card Asset Init', function () {
it('calls loader with config', function () {
@ -107,13 +108,23 @@ describe('Card Asset Service', function () {
});
describe('Generate the correct glob strings', function () {
// @TODO: change the default
it('DEFAULT CASE: do nothing [temp]', function () {
it('CARD ASSET SERVICE DEFAULT CASE: do nothing', function () {
const cardAssets = new CardAssetService();
cardAssets.generateGlobs().should.eql({});
});
it('GHOST DEFAULT CASE: exclude bookmark and gallery', function () {
const cardAssets = new CardAssetService({
config: themeDefaults.card_assets
});
cardAssets.generateGlobs().should.eql({
'cards.min.css': 'css/!(bookmark|gallery).css',
'cards.min.js': 'js/!(bookmark|gallery).js'
});
});
it('CASE: card_assets = true, all cards assets should be included', function () {
const cardAssets = new CardAssetService({
config: true

View File

@ -13,7 +13,9 @@ describe('Themes', function () {
config.should.eql({
posts_per_page: 5,
card_assets: false
card_assets: {
exclude: ['bookmark', 'gallery']
}
});
});
@ -22,16 +24,18 @@ describe('Themes', function () {
config.should.eql({
posts_per_page: 5,
card_assets: false
card_assets: {
exclude: ['bookmark', 'gallery']
}
});
});
it('handles allows package.json to overrideg default', function () {
const config = themeConfig.create({name: 'casper', config: {posts_per_page: 3}});
it('handles allows package.json to override default', function () {
const config = themeConfig.create({name: 'casper', config: {posts_per_page: 3, card_assets: true}});
config.should.eql({
posts_per_page: 3,
card_assets: false
card_assets: true
});
});
@ -40,7 +44,9 @@ describe('Themes', function () {
config.should.eql({
posts_per_page: 5,
card_assets: false
card_assets: {
exclude: ['bookmark', 'gallery']
}
});
});
});