-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
175 lines (147 loc) · 4.96 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
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var babel = require('babel-core');
var micromatch = require('micromatch');
function fileLastModifiedHash(path) {
var mtime = fs.lstatSync(path).mtime.getTime();
return crypto
.createHash('md5')
.update(mtime + '-' + path)
.digest('hex');
}
module.exports = function(options) {
options = options || {};
var srcPath = options.srcPath;
var cachePath = options.cachePath || 'memory';
var isMemoryCache = cachePath === 'memory';
var exclude = options.exclude || [];
var debug = options.debug || false;
var webConsoleErrors = options.consoleErrors || false;
// filename to last known hash map
var hashMap = {};
// hash to transpiled file contents map
var cacheMap = {};
if (!isMemoryCache) {
try {
fs.mkdirSync(cachePath);
} catch (e) {}
}
var babelOptions = options.babelOptions || { stage: 0 };
babelOptions.highlightCode = false;
function log() {
if (debug) {
console.log.apply(undefined, arguments);
}
}
function handleError(res, error) {
if (webConsoleErrors) {
var errOutput = String(error).replace(/\'/g, '\\\'').replace(/\"/g, '\\\"');
res.send(
'/* Babel parsing error from babel-middleware */' +
'\n /* See error console output for details. */' +
'\n var output = ' + JSON.stringify(error) +
'\n console.error("' + errOutput + '", output.codeFrame)'
);
} else {
res.status(500).send(error);
}
res.end();
}
function pathForHash(hash) {
return path.resolve(cachePath + '/' + hash + '.js');
}
return function(req, res, next) {
var src = path.resolve(srcPath + '/' + req.path); // XXX Need the correct path
var hash = fileLastModifiedHash(src);
var lastKnownHash = hashMap[src];
var hashPath;
if (exclude.length) {
if (micromatch.any(req.path.replace(/^\/+|\/+$/g, ''), exclude)) {
log('Excluded: %s (%s)', req.path, exclude);
res.append('X-Babel-Cache', false);
res.sendFile(src, {}, function(err) {
if (err) {
handleError(res, err);
}
});
return;
}
}
log('Preparing: %s (%s)', src, hash);
res.append('X-Babel-Cache', true);
res.append('X-Babel-Cache-Hash', hash);
if (!isMemoryCache) {
hashPath = pathForHash(hash);
try {
fs.statSync(hashPath);
hashMap[src] = lastKnownHash = hash;
} catch(e) {}
}
if (lastKnownHash && lastKnownHash === hash) {
// file unchanged, exit early
var cacheMiss = false;
if (!isMemoryCache) {
try {
fs.lstatSync(hashPath);
} catch(e) {
cacheMiss = true;
}
// Ensure Cache directory exists
if (cacheMiss) {
try {
fs.lstatSync(cachePath);
} catch (e) {
fs.mkdirSync(cachePath);
}
}
}
if (!cacheMiss) {
res.append('X-Babel-Cache-Hit', true);
if (isMemoryCache) {
log('Serving (cached): %s', src);
res.write(cacheMap[hash]);
res.end();
} else {
log('Serving (cached): %s', hashPath);
res.sendFile(hashPath, {}, function(err) {
if (err) {
handleError(res, err);
}
});
}
return;
}
}
res.append('X-Babel-Cache-Hit', false);
if (isMemoryCache && lastKnownHash && lastKnownHash in cacheMap) {
delete cacheMap[lastKnownHash];
} else if (!isMemoryCache && lastKnownHash) {
try {
fs.unlinkSync(pathForHash(lastKnownHash));
} catch(e) {}
}
var result;
try {
result = babel.transformFileSync(src, babelOptions);
} catch(e) {
handleError(res, e);
return;
}
var code = result.code;
hashMap[src] = hash;
if (isMemoryCache) {
cacheMap[hash] = code;
} else {
fs.writeFile(hashPath, code, function(err) {
if (err) {
// console.error('Error saving ' + hashPath + ': ' + err);
delete hashMap[src];
}
});
}
log('Serving (uncached): %s', src);
res.write(code);
res.end();
};
};