70 lines
1.6 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');
2019-06-08 18:50:07 +02:00
const koaViews = require('koa-views');
2016-08-11 12:42:00 +02:00
/**
* Public assets hook
*/
module.exports = strapi => {
return {
/**
* Initialize the hook
*/
2019-06-08 18:50:07 +02:00
initialize() {
const { views } = strapi.config.middleware.settings;
if (_.isPlainObject(views) && !_.isEmpty(views)) {
const opts = _.clone(views);
2016-08-11 12:42:00 +02:00
2019-07-21 22:37:29 +02:00
if (Object.prototype.hasOwnProperty.call(opts, 'default')) {
2016-08-11 12:42:00 +02:00
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(
2019-06-08 18:50:07 +02:00
koaViews(
2017-07-24 19:58:03 +02:00
path.resolve(strapi.config.appPath, strapi.config.paths.views),
opts
)
);
2016-08-11 12:42:00 +02:00
}
2019-06-08 18:50:07 +02:00
},
2016-08-11 12:42:00 +02:00
};
};