forked from aui/tmodjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmod.js
955 lines (664 loc) · 24.6 KB
/
tmod.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/*!
* TmodJS - AOT Template Compiler
* https://github.com/aui/tmodjs
* Released under the MIT, BSD, and GPL Licenses
*/
'use strict';
var template = require('./lib/template-AOTcompile.js');
var uglifyjs = require("./lib/uglify.js");
var fs = require('fs');
var path = require('path');
var events = require('events');
var crypto = require('crypto');
var vm = require("vm");
// 跨平台 path 接口,统一 windows 与 linux 的路径分隔符,
// 避免不同平台模板编译后其 id 不一致
(function () {
if (!/\\/.test(path.resolve())) {
return path;
}
var oldPath = path;
var newPath = Object.create(oldPath);
var proxy = function (name) {
return function () {
var value = oldPath[name].apply(oldPath, arguments);
if (typeof value === 'string') {
value = value.split(oldPath.sep).join('/');
}
return value;
}
};
for (var name in newPath) {
if (typeof oldPath[name] === 'function') {
newPath[name] = proxy(name);
}
};
path = newPath;
})();
var RUNTIME = 'template';
var EXTNAME_RE = /\.(html|htm|tpl)$/i;
var FILTER_RE = /[^\w\.\-$]/;
var DIRNAME_RE = /[^\/]*/;
module.exports = {
__proto__: events.EventEmitter.prototype,
// 默认配置
// 用户配置将保存到模板根目录 package.json 文件中
defaults: {
// 编译输出目录设置
output: './build',
// 模板使用的编码。(注意:非 utf-8 编码的模板缺乏测试)
charset: 'utf-8',
// 模板合并规则
// 注意:type 参数的值为 templatejs 才会生效
combo: ['*'],
// 定义模板采用哪种语法,可选:
// simple: 默认语法,易于读写。可参看语法文档
// native: 功能丰富,灵活多变。语法类似微型模板引擎 tmpl
syntax: 'simple',
// 自定义辅助方法路径
helpers: null,
// 是否输出为压缩的格式
minify: true,
// 是否内嵌异步加载插件(beta)
// 可以支持 template.async(path, function (render) {}) 方式异步载入模板
// 注意:type 参数是 templatejs 的时候才生效
async: false,
// 是否嵌入模板引擎,否则编译为不依赖引擎的纯 js 代码
// 通常来说,模板不多的情况下,编译为原生的 js 打包后体积更小,因为不必嵌入引擎
// 当模板很多的时候,内置模板引擎,模板使用字符串存储的方案会更能节省空间
engine: false,
// 输出的模块类型(不区分大小写),可选:
// templatejs: 模板目录将会打包后输出,可使用 script 标签直接引入,也支持 NodeJS/RequireJS/SeaJS。
// cmd: 这是一种兼容 RequireJS/SeaJS 的模块(类似 atc v1版本编译结果)
// amd: 支持 RequireJS 等流行加载器
// commonjs: 编译为 NodeJS 模块
type: 'templatejs'
},
// 获取用户配置
getUserConfig: function (options, dir) {
dir = this.path || dir;
var file = dir + '/package.json';
var defaults = this.defaults;
var json = null;
var name = null;
var config = {};
// 读取目录中 package.json
if (fs.existsSync(file)) {
var fileContent = fs.readFileSync(file, 'utf-8');
if (fileContent) {
json = JSON.parse(fileContent);
}
}
if (!json) {
json = {
"name": 'template',
"version": '1.0.0',
"dependencies": {
"tmodjs": "~0.0.1"
},
"tmodjs-config": {}
}
}
// 默认配置 优先级:0
for (name in defaults) {
config[name] = defaults[name];
}
// 项目配置 优先级:1
for (name in json["tmodjs-config"]) {
config[name] = json["tmodjs-config"][name];
}
// 用户配置 优先级:2
for (name in options) {
config[name] = options[name];
}
json["tmodjs-config"] = config;
this['package.json'] = json;
// 忽略大小写
config['type'] = config['type'].toLowerCase();
config['syntax'] = config['syntax'].toLowerCase();
return config;
},
/** 保存用户配置 */
saveUserConfig: function () {
var file = this.path + '/package.json';
var configName = 'tmodjs-config';
var json = this['package.json'];
var options = json[configName];
var userConfigList = Object.keys(this.defaults);
//if (options.output.indexOf('.') === 0) {
// json.main = options.output + '/' + (options.runtime || RUNTIME) + '.js';
//}
// 只保存指定的字段
json[configName] = JSON.parse(
JSON.stringify(options, userConfigList)
);
var text = JSON.stringify(json, null, 4);
fs.writeFileSync(file, text, 'utf-8');
},
// 绑定文件监听事件
_onwatch: function (dir, callback) {
var that = this;
var watchList = {};
var timer = {};
var walk = function (dir) {
fs.readdirSync(dir).forEach(function (item) {
var fullname = dir + '/' + item;
if (fs.statSync(fullname).isDirectory()){
watch(fullname);
walk(fullname);
}
});
};
// 排除“.”、“_”开头或者非英文命名的目录
var filter = function (name) {
return !FILTER_RE.test(name) && name !== that.output;
};
var watch = function (parent) {
var target = path.basename(parent);
if (!filter(target)){
return;
}
if (watchList[parent]) {
watchList[parent].close();
}
watchList[parent] = fs.watch(parent, function (event, filename) {
var fullname = parent + '/' + filename;
var type;
var fstype;
if (!filter(filename)) {
return;
}
// 检查文件、目录是否存在
if (!fs.existsSync(fullname)) {
// 如果目录被删除则关闭监视器
if (watchList[fullname]) {
fstype = 'directory';
watchList[fullname].close();
delete watchList[fullname];
} else {
fstype = 'file';
}
type = 'delete';
} else {
// 文件
if (fs.statSync(fullname).isFile()) {
fstype = 'file';
type = event == 'rename' ? 'create' : 'updated'
// 文件夹
} else if (event === 'rename') {
fstype = 'directory';
type = 'create'
watch(fullname);
walk(fullname);
}
}
var eventData = {
type: type,
target: filename,
parent: parent,
fstype: fstype
};
if (/windows/i.test(require('os').type())) {
// window 下 nodejs fs.watch 方法尚未稳定
clearTimeout(timer[fullname]);
timer[fullname] = setTimeout(function() {
callback.call(that, eventData);
}, 16);
} else {
callback.call(that, eventData);
}
});
};
watch(dir);
walk(dir);
},
// 筛选模板文件
_filter: function (name) {
return !FILTER_RE.test(name) && EXTNAME_RE.test(name);
},
// 模板文件写入
_fsWrite: function (file, data) {
this._fsMkdir(path.dirname(file));
fs.writeFileSync(file, data, this.options['charset']);
},
// 模板文件读取
_fsRead: function (file) {
return fs.readFileSync(file, this.options['charset']);
},
// 创建目录,包括子文件夹
_fsMkdir: function (dir) {
var currPath = dir;
var toMakeUpPath = [];
while (!fs.existsSync(currPath)) {
toMakeUpPath.unshift(currPath);
currPath = path.dirname(currPath);
}
toMakeUpPath.forEach(function (pathItem) {
fs.mkdirSync(pathItem);
});
},
// 删除文件夹,包括子文件夹
_rmdir: function (dir) {
var walk = function (dir) {
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
return;
}
var files = fs.readdirSync(dir);
if (!files.length) {
fs.rmdirSync(dir);
return;
} else {
files.forEach(function (file) {
var fullName = path.join(dir, file);
if (fs.statSync(fullName).isDirectory()) {
walk(fullName);
} else {
fs.unlinkSync(fullName);
}
});
}
fs.rmdirSync(dir);
};
walk(dir);
},
// 删除模板文件
_fsUnlink: function (file) {
return fs.existsSync(file) && fs.unlinkSync(file);
},
// 获取字符串 md5 值
_md5: function (text) {
return crypto.createHash('md5').update(text).digest('hex');
},
// 检查模板是否更改
_isChange: function (html, js) {
var newMd5 = this._md5(html + JSON.stringify(this['package.json']));
var oldMd5 = js.match(/<MD5:(\w*)>/)[1];
return newMd5 !== oldMd5;
},
// 调试语法错误
_debug: function (error, callback) {
var debugFile = error.debugFile;
var code = error.temp;
code = "/*! <DEBUG:" + error.id + '> */\n' + code;
this._fsWrite(debugFile, code);
// 启动子进程进行调试,从根本上避免影响当前进程
var exec = require('child_process').exec;
exec('node ' + debugFile, {timeout: 0}, function (error, stdout, stderr) {
var message = error ? error.message : '';
message = message
.replace(/^Command\sfailed\:|\s*SyntaxError[\w\W]*$/g, '')
.trim();
callback(message);
});
//this._fsUnlink(debugFile);
},
// 在控制台显示日志(支持UBB)
_log: function (message) {
var styles = {
// styles
'bold' : ['\x1B[1m', '\x1B[22m'],
'italic' : ['\x1B[3m', '\x1B[23m'],
'underline' : ['\x1B[4m', '\x1B[24m'],
'inverse' : ['\x1B[7m', '\x1B[27m'],
// colors
'white' : ['\x1B[37m', '\x1B[39m'],
'grey' : ['\x1B[90m', '\x1B[39m'],
'black' : ['\x1B[30m', '\x1B[39m'],
'blue' : ['\x1B[34m', '\x1B[39m'],
'cyan' : ['\x1B[36m', '\x1B[39m'],
'green' : ['\x1B[32m', '\x1B[39m'],
'magenta' : ['\x1B[35m', '\x1B[39m'],
'red' : ['\x1B[31m', '\x1B[39m'],
'yellow' : ['\x1B[33m', '\x1B[39m']
};
styles['b'] = styles['bold'];
styles['i'] = styles['italic'];
styles['u'] = styles['underline'];
message = message.replace(/\[([^\]]*?)\]/igm, function ($1, $2) {
return $2.indexOf('/') === 0
? styles[$2.slice(1)][1]
: styles[$2][0];
});
this.log(message);
},
log: function (message) {
process.stdout.write(message);
},
// 打包模板
_combo: function () {
var that = this;
var templates = [];
var ignores = [];
var isDebug = this.options.debug;
var isWrappings = this.options.type !== 'templatejs';
var runtime = this.options.engine ? '/lib/template-full.js' : '/lib/template-runtime.js';
var template = fs.readFileSync(__dirname + runtime, 'utf-8');
var combo = '';
var walk = function (dir) {
var dirList = fs.readdirSync(dir);
dirList.forEach(function (item) {
if (fs.statSync(dir + '/' + item).isDirectory()) {
walk(dir + '/' + item);
} else if (that._filter(item)) {
var id = (dir + '/' + item)
.replace(EXTNAME_RE, '')
.replace(that.path + '/', '');
templates.push(id);
if (!that.combo.test(id)) {
ignores.push(id);
return;
}
var target = that.output + '/' + id + '.js';
if (fs.existsSync(target)) {
var code = that._fsRead(target);
// 一个猥琐的实现:
// 文件末尾设置一个空注释,然后让 UglifyJS 不压缩它,避免很多文件挤成一行
code = code.replace(/^\/\*[\w\W]*?\*\//, '/**/');
combo += code;
}
}
});
};
if (!isWrappings) {
walk(this.path);
}
var build = Date.now();
var debug = isDebug ? '<DEBUG>' : '';
var data = {
version: this.version,
build: build,
templates: combo,
debug: debug,
plugins: '',
syntax: '',
engine: '',
helpers: this.helpers
};
// 嵌入异步加载插件
if (this.options.async) {
data.plugins = fs.readFileSync(__dirname + '/lib/template-async.js', 'utf-8');
}
// 嵌入引擎
if (this.options.engine) {
data.engine = fs.readFileSync(__dirname + '/lib/template.js', 'utf-8');
data.syntax = fs.readFileSync(__dirname + '/lib/template-syntax.js', 'utf-8');
}
template = template.replace(/['"]<\:(.*?)\:>['"]/g, function ($1, $2) {
return data[$2] || '';
});
var target = path.join(this.output, (this.options.runtime || RUNTIME) + '.js');
this._fsWrite(target, template);
isDebug || !this.options.minify
? uglifyjs.beautify(target)
: uglifyjs.minify(target);
this.emit('combo', {
output: target,
isDebug: isDebug,
templates: templates,
ignores: ignores,
build: build
});
},
/**
* 监听模板的修改进行即时编译
*/
watch: function () {
// 监控模板目录
this.on('watch', function (data) {
var type = data.type;
var fstype = data.fstype;
var target = data.target;
var parent = data.parent;
var fullname = parent + '/' + target;
if (target && fstype === 'file' && this._filter(target)) {
if (type === 'delete') {
this.emit('delete', {
source: data.target
});
fullname = fullname.replace(EXTNAME_RE, '');
this._fsUnlink(fullname.replace(this.path, this.output) + '.js');
this._combo();
} else if (/updated|create/.test(type)) {
this.emit('change', {
source: data.target
});
if (this._compile(fullname)) {
this._combo();
};
}
}
});
},
// 编译单个模板
_compile: function (file) {
var that = this;
// 模板字符串
var source = this._fsRead(file);
// 目标路径
var target = file
.replace(EXTNAME_RE, '.js')
.replace(this.path, this.output);
var mod = '';
var modObject = {};
var error = true;
var errorInfo = null;
var isDebug = this.options.debug;
var isWrappings = this.options.wrappings;
var isEngine = this.options.engine;
// 读取上一次编译的结果
if (fs.existsSync(target)) {
mod = this._fsRead(target);
}
// 检查模板是否有改动
var isChange = !mod
|| /<DEBUG>/.test(mod)
|| isDebug
|| this._isChange(source, mod);
var id = file
.replace(this.path + '/', './');
var extname = id.match(EXTNAME_RE)[1];
id = id.replace(EXTNAME_RE, '');
// 模板加载事件
this.emit('load', {
id: id,
file: file,
extname: extname,
isChange: isChange,
source: source,
target: target
});
try {
if (isChange) {
modObject = template.AOTcompile(id, source, {
runtime: RUNTIME,
engine: this.options.engine,
type: this.options.type,
debug: isDebug
});
mod = modObject.code;
}
error = false;
} catch (e) {
errorInfo = e;
}
if (!error && isChange) {
var md5 = this._md5(source + JSON.stringify(this['package.json']));
mod = '/*! <TmodJS> <MD5:' + md5 + '>'
+ (isDebug ? ' <DEBUG>' : '')
+ '*/\n' + mod;
this._fsWrite(target, mod);
uglifyjs[isDebug || !this.options.minify ? 'beautify' : 'minify'](target);
}
var compileInfo = {
id: id,
file: file,
extname: extname,
isChange: isChange,
error: error,
source: source,
target: target,
code: mod,
requires: modObject.requires || []
};
if (error) {
errorInfo.debugFile = this.path + '/.debug.js';
this._debug(errorInfo, function (message) {
var e = {
name: errorInfo.name,
type: 'compileError',
message: message,
debugFile: errorInfo.debugFile,
temp: errorInfo.temp
};
for (var name in compileInfo) {
e[name] = compileInfo[name];
};
// 模板编译错误事件
this.emit('compileError', e);
this.emit('error', e);
}.bind(this));
} else {
// 模板编译成功事件
this.emit('compile', compileInfo);
}
if (error) {
return false;
} else {
return compileInfo;
}
},
/**
* 编译模板
* @param {String} 模板文件路径,无此参数则编译目录所有模板
* @param {Boolean} 是否递归编译 include 依赖
*/
compile: function (file, recursion) {
var that = this;
var error = false;
if (file) {
var extname = path.extname(file);
var walk = function (list) {
list.forEach(function (file) {
if (error) {
return;
}
var info = that._compile(file);
error = !info;
if (!error && recursion !== false && info.requires.length) {
list = info.requires.map(function (id) {
var target = path.resolve(that.path, id + extname);
return target;
});
walk(list);
};
});
};
walk(typeof file === 'string' ? [file] : file);
!error && this._combo();
} else {
var walk = function (dir) {
var dirList = fs.readdirSync(dir);
dirList.forEach(function (item) {
if (error) {
return;
}
if (fs.statSync(dir + '/' + item).isDirectory()) {
walk(dir + '/' + item);
} else if (that._filter(item)) {
error = !that._compile(dir + '/' + item);
}
});
};
walk(this.path);
!error && this._combo();
}
},
init: function (input, options) {
events.EventEmitter.call(this);
options = this.options = this.getUserConfig(options, input);
// 模板目录
this.path = path.resolve(input);
// 输出目录
this.output = path.resolve(path.join(this.path, options.output));
// 辅助方法
this.helpers = '';
// 加载辅助方法
if (options['helpers']) {
var helpersPath = path.join(this.path, options['helpers']);
if (fs.existsSync(helpersPath)) {
this.helpers = fs.readFileSync(helpersPath, 'utf-8');
vm.runInNewContext(this.helpers, {
template: template
});
}
}
// 加载模板语法设置
if (options['syntax'] && options['syntax'] !== 'native') {
var syntaxPath = options['syntax'] === 'simple'
? __dirname + '/lib/template-syntax.js'
: path.join(this.path, options['syntax']);
if (fs.existsSync(syntaxPath)) {
vm.runInNewContext(fs.readFileSync(syntaxPath, 'utf-8'), {
template: template
});
}
}
// 模板合并规则
if (options.combo.length) {
var reg = [];
options.combo.forEach(function (combo) {
combo = combo
.replace(/([\$\.])/g, '\\$1')
.replace(/\*/g, '(.*?)');
reg.push('^' + combo + '$');
});
this.combo = new RegExp(reg.join('|'));
}
// 初始化 watch 事件
this.on('newListener', function (event, listener) {
if (event === 'watch') {
this._log('\n[inverse]Waiting..[/inverse]\n\n');
this._onwatch(this.path, function (data) {
this.emit('watch', data);
});
this._onwatch = function () {};
}
});
// 监听模板修改事件
this.on('change', function (data) {
var time = (new Date).toLocaleTimeString();
this._log('[grey]' + time + '[/grey] ');
this._log('[grey]Template has been updated[/grey]\n');
});
// 监听模板加载事件
this.on('load', function (data) {
this._log(data.id + '[grey].' + data.extname + '[/grey]');
});
// 监听模板编译事件
this.on('compile', function (data) {
if (data.isChange) {
this._log(' [green]√[/green]\n');
} else {
this._log(' [grey]√[/grey]\n');
}
});
// 监听编译错误事件
this.on('compileError', function (data) {
this._log(' [inverse][red]Syntax Error[/red][/inverse]\n');
this._log('\n[red]Template ID: ' + data.id + '[/red]\n');
this._log('[red]' + data.message + '[/red]\n');
});
// 监听模板合并事件
this.on('combo', function (data) {
this._log('\n');
var iLength = data.ignores.length;
var tLengtn = data.templates.length;
if (data.ignores.length) {
this._log('[grey]Ignore(' + iLength + '/' + tLengtn + '): '
+ data.ignores.join(', ') + '[/grey]\n');
}
this._log('[grey]>>> [/grey][green]' + data.output + '[/green]');
this._log(this.options.debug ? ' [red]<DEBUG>[/red]\n' : '\n');
});
}
};