-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
213 lines (203 loc) · 7.36 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @date 13-1-16
* @describe: 管理所有模版的模块
* todo:解决模版本身的配置问题
* 可通过add方法添加模版支持,其中compile部分为预编译阶段,update为更新模版的渲染模块到项目的lib中
* @author: KnightWu
*/
var fs = require('fs');
var path = require('path');
var uglify = require('uglify-js');
var fetchData = require('./lib/fetchData');
var templatePlugin = require('./lib/templatePlugin');
var currentTemplatePlugin = null;
var helperContext = '((typeof ___kkit___ !== "undefined" ) ? ___kkit___.tph : tpHelper)';
/**
* 加载所有插件
* @param pluginsPath
*/
var loadPlugins = function (pluginsPath) {
var plugins = fs.readdirSync(pluginsPath);
plugins.forEach(function (pluginName) {
loadPlugin(path.resolve(pluginsPath, './' + pluginName));
});
};
/**
* 加载单个插件
* @param pluginPath 插件路径
*/
var loadPlugin = function (pluginPath) {
var pluginName = path.basename(pluginPath, '.js');
var pluginDir = path.dirname(pluginPath);
var plugin = require(path.resolve(pluginDir, './' + pluginName));
plugin.suffix = pluginName;
templatePlugin.add(plugin);
};
loadPlugins(path.resolve(__dirname, './lib/plugins')); //默认加载 todo:以后移动到项目统一加载的位置
/**
* 获取uglify函数对象中的所有语句 todo:应该采用更加简单的正则处理字符串的方式,使用uglify函数越大,效率越低
* @param func
* @return {string}
*/
var getCodeInFunction = function (func) {
var funcObj = func;
if (!(func instanceof uglify.AST_Node)) {
funcObj = uglify.parse(func);
}
var codeArr = funcObj.body[0].body;
var code = '';
for (var idx = 0; idx < codeArr.length; idx++) {
code += codeArr[idx].print_to_string() + ';';
}
return code;
};
/**
* 获取数据处理对象的字符串
* @param realPath
* @return {*}
*/
var extendDataProgressToData = function (realPath, isEs6) {
var templateDir = path.dirname(realPath);
var dataProgress = null;
try {
dataProgress = fs.readFileSync(templateDir + '/data.js', 'utf-8');
} catch (e) {
}
var fragment = '{}';
if (isEs6) {
fragment = dataProgress.replace(/^[\s]*module\.exports[\s]*=[\s]*/, '').replace(/;[\s]*$/, '');
} else {
try {
fragment = uglify.parse(dataProgress).body[0].body.right.print_to_string();
} catch (err) {
}
}
fragment = (fragment === '{}') ? '' : '_data = ' + (fetchData('helperName').val || helperContext) + '.mixin(_data,' + fragment + ');';
return fragment;
};
/**
* 读出文件,根据后缀调用不同方法生成模版
* @param filePath 模板文件路径
* @param tp 后缀
* @param isEs6 模板是否支持es6
* @return {null}
*/
var compile = function (filePath, tp, isEs6) {
var sourceCode = fs.readFileSync(filePath, 'utf-8');
if (sourceCode.trim() === '') {
throw new Error('The template [' + filePath + '] is empty');
}
var dataProgress = extendDataProgressToData(filePath, isEs6);
currentTemplatePlugin = tp;
return tp.compile(filePath, dataProgress, fetchData('helperName').val || helperContext, isEs6);
};
/**
* 根据 './lib/helper/tpHelper.html' 模版,更新模版辅助模块的枢纽模块代码
* @param tps 所有使用到的模版模块
* @param to 写到这个目录
*/
var updateTpHelper = function (tps, to) {
var tpHelperCode = compile(path.resolve(__dirname, './lib/helper/tpHelperTpl.js'), require('ktemplate'));
var helperPath = path.resolve(__dirname, path.dirname(to) || './lib/helper/', './tpHelper.js');
fs.writeFileSync(
helperPath,
tpHelperCode(tps)
);
};
module.exports = {
loadPlugins: loadPlugins,
/**
* 增加一种解析模板插件支持
* @param pluginConfig
*/
loadPlugin: loadPlugin,
/**
* update all plugins that will be used in the project lib for template render.
* @param to the file update to {to} dir
*/
updateHelper: function (to) {
console.log('Update plugins begin...');
var allTemplateHelper = [];
var tpPath = function (type, modules) {
var modulePath = '';
modules.forEach(function (mobj) {
if (mobj.id.indexOf(type) !== -1) {
modulePath = path.dirname(mobj.id);
}
});
return modulePath;
};
var allPlugins = templatePlugin.all();
var relPath = path.resolve(__dirname, to || './lib/helper/tpHelper/');
var helperName = path.basename(relPath);
if (!fs.existsSync(relPath)) {
fs.mkdirSync(relPath);
}
for (var type in allPlugins) {
if (allPlugins.hasOwnProperty(type) && allPlugins[type].update !== null) {
var plugin = allPlugins[type];
if (typeof plugin === 'undefined' || typeof plugin.update === 'undefined') {
continue;
}
var code = plugin.update(tpPath.bind(undefined, plugin.tpName));
fs.writeFileSync(path.resolve(relPath, './' + type + '.js'), code);
console.log(' Update ' + type + '.js success.');
allTemplateHelper.push({id: type, relPath: './' + helperName});
}
}
fetchData('allTemplate').store = allTemplateHelper;
fetchData('helperName').store = helperName;
updateTpHelper(allTemplateHelper, to);
},
/**
* 当前模版生成 sourceMap
* todo:异步执行时可能会有问题
*/
generateSourceMap: function () {
if (currentTemplatePlugin.sourceMap !== null) {
currentTemplatePlugin.sourceMap.apply(currentTemplatePlugin, arguments);
}
},
/**
* 解析模版语句
* todo:支持str的compile
* @param templatePath 模版路径
* @param plugin 指定插件名称
* @return {Function} 返回解析后的函数
*/
compile: function (templatePath, plugin) {
var suffixReg = /.*\.(.*$)/g;
var suffix = plugin || suffixReg.exec(templatePath)[1];
var tp = templatePlugin.all()[suffix];
if (typeof tp === 'undefined') {
throw "Don't support the template plugin type: [" + suffix + ']';
}
var renderFunctionStr = compile(templatePath, tp);
fetchData('usedTemplates').mixin(JSON.parse('{"' + suffix + '": true}'));
return new Function('_data', getCodeInFunction(renderFunctionStr));
},
/**
* 解析模版语句, 自动匹配模版
* @param templateDir 模版文件夹路径
* @param fileName 模版文件名称(不包括后缀)
* @param isEs6 是否适配es6模式 仅支持ktemplate
* @return {null} 返回解析后的对象
*/
compileAdaptive: function (templateDir, fileName, isEs6) {
var render = null;
var allPlugins = templatePlugin.all();
for (var suffix in allPlugins) {
var realPath = templateDir + '/' + fileName + '.' + suffix;
if (allPlugins.hasOwnProperty(suffix) && fs.existsSync(realPath)) {
render = compile(realPath, allPlugins[suffix], isEs6);
}
}
return render;
},
getTp: function (suffix) {
return templatePlugin.all()[suffix].tp;
},
getSupportedSuffix: function () {
return Object.keys(templatePlugin.all());
}
};