-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (49 loc) · 1.62 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
'use strict';
var ejs = require('ejs');
var path = require('path');
//提取<script>
var scriptExpr = /<!--[\S\s]*?-->|<script([\s\S]*?)>([\s\S]*?)<\/script>/gi;
var attrExpr = /\s*([\w-]+)[\s\S]*?=[\s\S]*?"\s*([\s\S]*?)\s*"/g;
//<%- include('user/show', {user: user}) %> <% include user/show %>
var includeExpr = /<%-?\s[\s\S]*?include\s*(?:\(\s*('|")(.*?)\1|([\w\/\\-_\.]+))/g;
var ejsPrefixExpr = /\.ejs$/;
module.exports = function (content, file, conf) {
conf.filename = file.getId();
conf.client = true;
return content.replace(scriptExpr, function (matchStr) {
var template = arguments[2];
if (!template) {
return matchStr;
}
var attrs = {};
var keyVal = '';
while ((keyVal = attrExpr.exec(arguments[1]))) {
attrs[keyVal[1]] = keyVal[2];
}
if (attrs['type'] !== 'text/ejs') {
return matchStr;
}
//判断是否需要去解析 ejs 模板
try {
if (!conf.raw) {
//判断 include
var includeArgs = '';
while ((includeArgs = includeExpr.exec(template))) {
var ejsUri = includeArgs[2] || includeArgs[3];
file.cache.addDeps(path.resolve(file.dirname, ejsPrefixExpr.test(ejsUri) ? ejsUri : (ejsUri + '.ejs')));
}
var data = {};
if(attrs['data-json']) {
var realpath = path.resolve(file.dirname, attrs['data-json']);
data = require(realpath);
file.cache.addDeps(realpath);
}
return ejs.render(template, data, conf);
}
} catch (e) {
fis.log.warn('ejs parse %s with an error: %s', file.filename, e);
return matchStr;
}
return template;
});
};