-
Notifications
You must be signed in to change notification settings - Fork 10
/
clean_traceur_compile.js
65 lines (51 loc) · 1.68 KB
/
clean_traceur_compile.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
// traceur needs a version of System. It's possible that it's already
// been required and System overwritten/lost.
// This make sure traceur is entirely reloaded.
var traceurPath = require.resolve("traceur");
var path = require("path");
var assign = require("lodash/assign");
var search = path.sep + "node_modules" + path.sep + "traceur" + path.sep;
var index = traceurPath.indexOf(search);
var tPath = traceurPath.substr(0,index+search.length);
for(var name in require.cache) {
if( name.indexOf(tPath) === 0 ) {
delete require.cache[name];
}
}
var globalNames = ["System"];
var oldGlobals = {};
globalNames.forEach(function(name){
oldGlobals[name] = global[name];
});
var traceur = require('traceur');
var NodeCompiler = traceur.NodeCompiler;
var commonJSOptions = traceur.commonJSOptions;
var globals = {};
globalNames.forEach(function(name){
globals[name] = global[name];
});
globalNames.forEach(function(name){
global[name] = oldGlobals[name];
});
module.exports = function(source, compileOptions, options){
var saved = {};
var theCompilerOptions = assign({}, compileOptions);
globalNames.forEach(function(name){
saved[name] = global[name];
global[name] = globals[name];
});
if(theCompilerOptions.sourceMaps) {
theCompilerOptions.sourceMaps = "memory";
}
if(options.traceurOptions) {
theCompilerOptions = assign(options.traceurOptions, theCompilerOptions);
}
var compiler = new NodeCompiler(commonJSOptions(theCompilerOptions));
var result = {};
result.code = compiler.compile(source, theCompilerOptions.filename, theCompilerOptions.filename);
result.map = compiler.getSourceMap();
globalNames.forEach(function(name){
global[name] = saved[name];
});
return result;
};