-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
53 lines (47 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
let swig = require('swig');
const filters = require('swig/lib/filters');
const Resource = require('./lib/resource.js');
const tagNames = [ 'body', 'head', 'html', 'pagelet', 'require', 'css', 'script', 'uri', 'title', 'datalet', 'ATF' ];
module.exports = exports = swig;
exports.tagNames = tagNames;
exports.Resource = Resource;
exports.setServiceWorkerRegistrationJS = Resource.setServiceWorkerRegistrationJS;
// exports.setCombo = Resource.setCombo;
exports.filters = exports.filters || filters;
exports.configure = function(options) {
swig = options.swig || swig;
options.swig = undefined;
tagNames.forEach(function extendTags(tag) {
const t = require('./lib/tags/' + tag);
swig.setTag(tag, t.parse, t.compile, t.ends, t.blockLevel || false);
});
const map = options.cacheMap ? Resource.loadOptions(options.map) : options.map;
swig.setExtension('Resource', Resource);
swig.setExtension('_map', map);
Resource.setRoot(options.root || process.cwd());
if (typeof options.comboURI === 'function') {
Resource.prototype.comboURI = options.comboURI;
}
if (options.logger) {
Resource.setLogger(options.logger);
}
};
exports.middleware = function createSwigMiddleware(options) {
this.configure(options);
return function swigMiddleware(req, res, next) {
const pagelets = req.get('X-Pagelets');
res.locals._query = req.query;
res.locals._body = req.body;
if (pagelets) {
res.set('Content-Type', 'application/json');
// res.set('Cache-Control', 'no-cache, no-store');
// res.set('Pragma', 'no-cache');
// res.set('Expires', 0);
res.locals._pagelets = pagelets;
}
next();
};
};
// for unittest
exports.cleanOptions = Resource.cleanOptions;