-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
70 lines (62 loc) · 1.62 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
'use strict';
var extend = require('extend-object');
var loaderUtils = require('loader-utils');
var os = require('os');
var traceur = require('traceur');
var defaults = {
modules: 'commonjs',
runtime: false,
sourceMaps: true
};
module.exports = function(source) {
var filename = loaderUtils.getRemainingRequest(this);
var content = source;
var map;
var options = {};
var result;
this.cacheable && this.cacheable();
// Process query and setup options/defaults/forced for Traceur
extend(options, defaults, loaderUtils.parseQuery(this.query));
Object.keys(options).forEach(function(key) {
switch(options[key]) {
case 'true':
options[key] = true;
break;
case 'false':
options[key] = false;
break;
case 'undefined':
options[key] = undefined;
break;
case 'null':
options[key] = null;
break;
}
});
// Handle Traceur runtime
if(filename === traceur.RUNTIME_PATH) {
return content;
}
if(options.runtime) {
content = 'require("' + traceur.RUNTIME_PATH + '");' + content;
}
// Parse code through Traceur
try {
delete options.runtime;
var compiler = new traceur.Compiler(options);
result = compiler.compile(content, filename);
// Process source map (if available) and return result
if(options.sourceMaps) {
map = JSON.parse(compiler.getSourceMap());
map.sourcesContent = [source];
this.callback(null, result, map);
}
else {
return result;
}
}
catch(errors) {
throw new Error(errors.join(os.EOL));
}
};
module.exports.runtime = traceur.RUNTIME_PATH;