83 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-08-11 12:42:00 +02:00
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const path = require('path');
// Public node modules.
const _ = require('lodash');
const consolidate = require('consolidate');
2016-08-11 12:42:00 +02:00
/**
* Public assets hook
*/
module.exports = strapi => {
return {
/**
* Default options
*/
defaults: {
views: {
enabled: false
}
2016-08-11 12:42:00 +02:00
},
/**
* Initialize the hook
*/
2017-07-24 19:58:03 +02:00
initialize: function(cb) {
if (
2017-07-26 18:53:48 +02:00
_.isPlainObject(strapi.config.middleware.settings.views) &&
!_.isEmpty(strapi.config.middleware.settings.views)
2017-07-24 19:58:03 +02:00
) {
2017-07-26 18:53:48 +02:00
const opts = _.clone(strapi.config.middleware.settings.views);
2016-08-11 12:42:00 +02:00
if (opts.hasOwnProperty('default')) {
opts.extension = opts.default;
delete opts.default;
}
// Map every template engine in config.
_.forEach(opts.map, engine => {
2016-08-11 12:42:00 +02:00
if (!consolidate.requires[engine]) {
// Try to require them using `consolidate` or throw an error.
try {
2017-07-24 19:58:03 +02:00
consolidate.requires[engine] = require(path.resolve(
strapi.config.appPath,
'node_modules',
engine
));
2016-08-11 12:42:00 +02:00
} catch (err) {
2017-07-24 19:58:03 +02:00
strapi.log.error(
'`' + engine + '` template engine not installed.'
);
strapi.log.error(
'Execute `$ npm install ' + engine + ' --save` to install it.'
);
2016-08-11 12:42:00 +02:00
process.exit(1);
}
}
// Initialize the engine with `consolidate`.
consolidate[engine];
});
2017-07-24 19:58:03 +02:00
strapi.app.use(
strapi.koaMiddlewares.views(
path.resolve(strapi.config.appPath, strapi.config.paths.views),
opts
)
);
2016-08-11 12:42:00 +02:00
}
cb();
}
};
};