-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (48 loc) · 1.79 KB
/
index.js
File metadata and controls
60 lines (48 loc) · 1.79 KB
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
54
55
56
57
58
59
60
var ejs = require('ejs'),
path = require('path');
var createJstEjsPreprocessor = function(args, config, logger, helper) {
config = config || {};
var log = logger.create('preprocessor.jst-ejs');
var defaultOptions = {
logicalMountPoint: "src"
};
var options = helper.merge(defaultOptions, args.options || {}, config.options || {});
var transformPath = function(filepath) {
return filepath.replace(/\.jst.ejs$/, '.js');
};
// Removes base and sprockets untill sprockets mountPath with options.logicalPathStrip
var transformLogicalPath = function(filepath) {
var logicalPath = filepath.replace(/\.jst.ejs$/, '');
// Poor mans solutions here, its somewhat more complex
return logicalPath.substring(logicalPath.indexOf(options.logicalMountPoint));
};
// Internal (private) namespace storage
var namespace = 'this.JST';
var makeJST = function (data, file) {
return "(function () { " +
namespace + " || (" + namespace + " = {}); " +
namespace + "[" + JSON.stringify(file.logicalPath) + "] = " +
data.replace(/$(.)/mg, '$1 ').trimLeft().trimRight() +
" }).call(this);";
};
return function(content, file, done) {
var result = "";
log.debug('Processing "%s".', file.originalPath);
file.path = transformPath(file.originalPath);
file.logicalPath = transformLogicalPath(file.originalPath);
try {
result = helper._.template(content).source;
result = makeJST(result, file);
} catch (e) {
log.error('%s\n at %s', e.message, file.originalPath);
return;
}
// We are done!
done(result);
};
};
createJstEjsPreprocessor.$inject = ['args', 'config.JstEjsPreprocessor', 'logger', 'helper'];
// PUBLISH DI MODULE
module.exports = {
'preprocessor:jst-ejs': ['factory', createJstEjsPreprocessor]
};