forked from node-cube/cube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.js
More file actions
159 lines (151 loc) · 4.1 KB
/
processor.js
File metadata and controls
159 lines (151 loc) · 4.1 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
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
'use strict';
const async = require('async');
const path = require('path');
const debug = require('debug')('cube:processor');
const xfs = require('xfs');
const wraper = require('./lib/wraper');
exports.process = function (cube, data, callback) {
let config = cube.config;
let qpath = data.queryPath;
let rpath = data.realPath;
let flagModuleWrap = data.wrap;
let flagCompress = data.compress;
let cachePath = qpath + ':' + flagModuleWrap + ':' + flagCompress + ':' + config.remote;
function checkCache(done) {
var tmp = cube.CACHE[cachePath];
xfs.lstat(path.join(config.root, rpath), function (err, stats) {
if (err) {
return done({
code: 500,
message: 'read file stats error:' + err.message
});
}
var mtime = new Date(stats.mtime).getTime();
if (tmp) { // if cached, check cache
if (tmp.modifyTime === mtime) { // target the cache, just return
debug('hint cache', rpath);
return done({code: 'CACHED'}, tmp);
}
}
data.modifyTime = mtime;
done(null, data);
});
}
function prepareCode(data, done) {
try {
data.source = data.code = xfs.readFileSync(path.join(config.root, data.realPath)).toString();
} catch (e) {
return done({
code: 500,
file: qpath,
message: 'read file content error:' + e.message
});
}
done(null, data);
}
function processResult(err, result) {
if (err) {
callback(err, data);
return;
}
var wraperMethod;
switch(data.type) {
case 'script':
try {
result = wraper.processScript(cube, result);
} catch (e) {
return callback(e, result);
}
if (flagModuleWrap) {
wraperMethod = 'wrapScript';
}
// utils.setRequires(result.queryPath, result.requires);
end(null, result);
break;
case 'style':
if (flagModuleWrap) {
wraperMethod = 'wrapStyle';
}
wraper.processStyle(cube, result, end);
break;
case 'template':
if (flagModuleWrap) {
try {
result = wraper.processScript(cube, result);
} catch (e) {
return callback(e, result);
}
wraperMethod = 'wrapScript';
}
end(null, result);
break;
default:
callback('unknow type', result);
}
function end(err, result) {
if (err) {
return callback(err);
}
/** build 的时候,启用lazyWrap, 方便确认哪些require需要被转名字 */
if (config.lazyWrap) {
result.genCode = function (cb) {
wraper[wraperMethod](cube, this, function (err, result) {
if (!err) {
//cache(result);
}
cb(err, result);
});
};
callback(null, result);
} else if (flagModuleWrap) {
result.mime = cube.getMIMEType('script');
wraper[wraperMethod](cube, result, function (err, result) {
if (!err) {
cache(result);
}
callback(err, result);
});
} else {
cache(result);
callback(null, result);
}
}
function cache(data) {
// cache result
if (cube.config.devCache) {
debug('cache processed file: %s, %s', result.realPath, result.mtime);
delete data.ast;
delete data.processors;
cube.CACHE[cachePath] = data;
}
}
}
let actions = [
checkCache,
prepareCode
];
async.waterfall(actions, function (err, data) {
if (err) {
if (err.code === 'CACHED') {
callback(err, data);
} else {
callback(err, data);
}
return;
}
var processActions = [
function (done) {
debug('start process');
done(null, data);
}
];
data.processors.forEach(function (p) {
var name = p.constructor.name;
processActions.push(function (d, done) {
debug('step into processor:' + name);
p.process(d, done);
});
});
async.waterfall(processActions, processResult);
});
};