-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpile.js
202 lines (176 loc) · 7.53 KB
/
transpile.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
'use strict';
var fs = require('fs');
var uaParser = require('./lib/ua');
var features = {} ;
function makeTest(test){
return function(ua) {
var uaID = ua.family.toLowerCase() ;
return test.subtests.every(function(subtest) {
for (var n=ua.major;n>=0;n--) {
var uaVer = uaID+(n?n:"") ;
if (uaVer in subtest.res)
return subtest.res[uaVer]==true ;
}
})?test:false;
}
}
var kangax = require('./build/compat.json') ;
Object.keys(kangax).forEach(function(revision){
kangax[revision].tests.forEach(function(test) {
var name = test.name.replace(/\W+/g,"_") ;
var testFunc = makeTest(test) ;
if (features[name]) {
console.warn("transpile-middleware: Warning - duplicate feature '"+name+"' in "+revision+" renamed to "+revision+"_"+name) ;
} else {
features[name] = testFunc;
}
features[revision+"_"+name] = testFunc;
}) ;
}) ;
//Map babel transforms to kangax names
var kangaxToBabel = {
arrow_functions:['babel-plugin-transform-es2015-arrow-functions'],
'const':['babel-plugin-transform-es2015-block-scoping'],
'let':['babel-plugin-transform-es2015-block-scoping'],
object_literal_extensions:['babel-plugin-transform-es2015-computed-properties','babel-plugin-transform-es2015-literals','babel-plugin-transform-es2015-shorthand-properties'],
template_literals:['babel-plugin-transform-es2015-template-literals'],
destructuring_declarations:['babel-plugin-transform-es2015-destructuring'],
destructuring_assignment:['babel-plugin-transform-es2015-destructuring'],
destructuring_parameters:['babel-plugin-transform-es2015-destructuring'],
es6_arrow_functions:['babel-plugin-transform-es2015-arrow-functions'],
es6_const:['babel-plugin-transform-es2015-block-scoping'],
es6_let:['babel-plugin-transform-es2015-block-scoping'],
es6_object_literal_extensions:['babel-plugin-transform-es2015-computed-properties','babel-plugin-transform-es2015-literals','babel-plugin-transform-es2015-shorthand-properties'],
es6_template_literals:['babel-plugin-transform-es2015-template-literals'],
es6_destructuring_declarations:['babel-plugin-transform-es2015-destructuring'],
es6_destructuring_assignment:['babel-plugin-transform-es2015-destructuring'],
es6_destructuring_parameters:['babel-plugin-transform-es2015-destructuring']
} ;
var nodentPlugins = {
async_functions:true,
async_return:true,
async_throw:true,
await_anywhere:true
} ;
var aliases = {
es6_template_strings:'template_literals',
template_strings:'template_literals'
};
function _try(fn,error) {
return function(){
try {
return fn.apply(this,arguments) ;
} catch (ex) {
(error || console.error)(ex) ;
return undefined ;
}
}
}
function createHandler(opts) {
if (!Array.isArray(opts.features)) {
return function(_0,_1,next) { next(); } ;
}
var enableCache = ('enableCache' in opts)?opts.enableCache:true ;
var match = opts.match;
var sourcemap = opts.sourcemap || false;
if (!match) match = /\.js$/;
var uaParser = require('./lib/ua');
var transformed = {};
var xform = function transformReqHandler(req, res, next) {
if (!req.url.match(match)) return next();
var ua = uaParser(req.headers['user-agent']);
var key = [req.url, "*", ua.ua.family, ua.ua.major].join('<>');
if (enableCache === true && key in transformed) {
res.setHeader('Content-Type','text/javascript')
res.write(transformed[key]);
res.end();
return ;
}
try {
var transpilers = [] ;
var babelPlugins = {} ;
var useNodent = null ;
opts.features.forEach(feature => {
var test ;
if (aliases[feature])
feature = aliases[feature] ;
if (test = nodentPlugins[feature]) {
useNodent = { promises: true } ;
} else if (features[feature]) {
if (kangaxToBabel[feature] && !features[feature](ua.ua)) {
kangaxToBabel[feature].forEach(function(plugin){
babelPlugins[plugin] = _try(require,ex=>console.error("Feature "+feature+": "+ex))(plugin) ;
}) ;
}
} else console.error("Unknown feature "+feature) ;
}) ;
var transformKeys = Object.keys(babelPlugins);
if (useNodent) {
transformKeys.push('nodent') ;
useNodent.sourcemap = sourcemap ;
if (features.async_functions(ua.ua))
useNodent.engine = true ;
var nodent = {
compiler: _try(require,ex=>console.error("Feature "+feature+": "+ex))('nodent')(),
method: 'compile',
args: (req,code) => [code, req.url, useNodent],
outputProperty: 'code' } ;
if (!nodent.compiler.version || nodent.compiler.version<"2.6")
console.log("Nodent version should be >=2.6") ;
else
transpilers.push(nodent) ;
}
transformKeys = req.url+"<>!"+transformKeys.sort().join('<>') ;
if (transformKeys in transformed) {
res.setHeader('Content-Type','text/javascript')
res.write(transformed[transformKeys]);
res.end();
return ;
}
babelPlugins = Object.keys(babelPlugins).map(key=>babelPlugins[key]).filter(plugin=>!!plugin) ;
if (useNodent && useNodent.engine)
babelPlugins.push(require('babel-plugin-syntax-async-functions')) ;
if (babelPlugins.length) {
var babel = require('babel-core');
transpilers.push({
compiler: babel, method: 'transform',
args: (req,code) => [code, { plugins: babelPlugins, compact:false }],
outputProperty: 'code'
}) ;
}
var fileContents = fs.readFileSync(opts.srcDir + req.url);
var result = transpilers.reduce(
(output, transpiler) => {
var args = transpiler.args(req,output);
if (args === false) return output;
var compiler = transpiler.compiler;
return compiler[transpiler.method].apply(compiler, args)[transpiler.outputProperty];
},
fileContents.toString()
);
if (enableCache === true)
transformed[key] = transformed[transformKeys] = result;
res.setHeader('Content-Type','text/javascript')
res.write(result);
res.end();
} catch (ex) {
res.status(500).send("Error occurred whilst running transforms: "+ex.message+"\n"+ex.stack);
}
};
xform.clearCache = function(url) {
if (url) {
url += "<>" ;
Object.keys(transformed).forEach(function(k){
if (k.slice(0,url.length)===url)
delete transformed[k] ;
}) ;
} else {
transformed = {} ;
}
} ;
return xform ;
}
module.exports = {
createHandler:createHandler,
features:Object.keys(kangaxToBabel).concat(Object.keys(nodentPlugins)).concat(Object.keys(aliases))
};