-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (51 loc) · 1.4 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
54
55
56
57
'use strict';
/**
* @module webmake-ejs
* @requires ejs
* @requires deferred
* @requires webmake
* @author Tyler Waters
**/
var ejs = require('ejs');
var deferred = require('deferred');
/**
* @typedef {object} webmake-extension-return
* @description the object that webmake is expecting
* @property {string} code code rendered in module block.
* @property {string} [sourceMap] sourcemap to provide.
*/
/**
* @type Array<string>
* @description Extensions this plugin applies to.
*/
exports.extension = ['ejs'];
/**
* Compile function called from webmake.
* @param {string} src source of the referenced file.
* @param {Object} info information about context (unused).
* @param {string} info.filename full path of the referenced file.
* @param {string} info.localFilename package name.
* @param {boolean} info.sourceMap whether or not to include source map.
* @param {string} info.generatedFilename generated package name.
* @returns {webmake-extension-return} value returned to webmake.
*/
exports.compile = function (src, info) {
return {
'code': compileEjs(src)
};
};
function compileEjs (src) {
var def = deferred();
try {
var template = ejs.compile(src, {'client': true});
def.resolve([
'module.exports = function (opts) {',
' return (' + template.toString() + ')(opts || {});',
'};'
].join('\n'));
}
catch (e) {
def.reject(e);
}
return def.promise;
}