forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (82 loc) · 2.35 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
// Entry point for Node.
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var traceur = require('traceur');
exports.RUNTIME_PATH = traceur.RUNTIME_PATH;
var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.js');
var SELF_SOURCE_REGEX = /transpiler\/src/;
var SELF_COMPILE_OPTIONS = {
modules: 'register',
moduleName: true,
script: false // parse as a module
};
var needsReload = true;
exports.reloadSources = function() {
needsReload = true;
};
exports.compile = function compile(options, paths, source) {
if (needsReload) {
reloadCompiler();
needsReload = false;
}
var inputPath, outputPath, moduleName;
if (typeof paths === 'string') {
inputPath = outputPath = paths;
} else {
inputPath = paths.inputPath;
outputPath = paths.inputPath;
moduleName = paths.moduleName;
}
outputPath = outputPath || inputPath;
moduleName = moduleName || inputPath;
moduleName = moduleName.replace(/\.\w*$/, '');
var localOptions = extend(options, {
moduleName: moduleName
});
var CompilerCls = System.get('transpiler/src/compiler').Compiler;
var compiler = new CompilerCls(localOptions);
var result = {
js: compiler.compile(source, inputPath, outputPath),
sourceMap: null
};
var sourceMapString = compiler.getSourceMap();
if (sourceMapString) {
result.sourceMap = JSON.parse(sourceMapString);
}
return result;
};
// Transpile and evaluate the code in `src`.
// Use existing traceur to compile our sources.
function reloadCompiler() {
loadModule(TRACEUR_PATH, false);
glob.sync(__dirname + '/src/**/*.js').forEach(function(fileName) {
loadModule(fileName, true);
});
}
function loadModule(filepath, transpile) {
var data = fs.readFileSync(filepath, 'utf8');
if (!data) {
throw new Error('Failed to import ' + filepath);
}
if (transpile) {
var moduleName = path.normalize(filepath)
.replace(__dirname, 'transpiler')
.replace(/\\/g, '/')
.replace(/\.\w*$/, '');
data = (new traceur.NodeCompiler(
extend(SELF_COMPILE_OPTIONS, { moduleName: moduleName } )
)).compile(data, filepath, filepath);
}
('global', eval)(data);
}
function extend(source, props) {
var res = {};
for (var prop in source) {
res[prop] = source[prop];
}
for (var prop in props) {
res[prop] = props[prop];
}
return res;
}