2014-09-25 17:04:46 -04:00
|
|
|
// 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');
|
2014-09-26 20:39:27 -04:00
|
|
|
var SELF_SOURCE_REGEX = /transpiler\/src/;
|
2014-09-25 17:04:46 -04:00
|
|
|
var SELF_COMPILE_OPTIONS = {
|
|
|
|
modules: 'register',
|
|
|
|
moduleName: true,
|
|
|
|
script: false // parse as a module
|
|
|
|
};
|
|
|
|
|
|
|
|
var needsReload = true;
|
2014-12-10 18:19:03 -05:00
|
|
|
var oldSystemGet = System.get;
|
2014-09-25 17:04:46 -04:00
|
|
|
|
2014-09-28 16:55:01 -04:00
|
|
|
exports.reloadSources = function() {
|
2014-09-25 17:04:46 -04:00
|
|
|
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
|
|
|
|
});
|
2014-09-26 20:39:27 -04:00
|
|
|
var CompilerCls = System.get('transpiler/src/compiler').Compiler;
|
2014-10-30 13:52:32 -04:00
|
|
|
|
|
|
|
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;
|
2014-09-25 17:04:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
2014-12-10 18:19:03 -05:00
|
|
|
|
|
|
|
// Traceur modules are register with the ".js" extension but we don't want
|
|
|
|
// to add it to all the import statements.
|
|
|
|
System.get = function get(normalizedName) {
|
|
|
|
var m = oldSystemGet.call(this, normalizedName);
|
|
|
|
if (!m && normalizedName.indexOf('traceur') == 0) {
|
|
|
|
m = oldSystemGet.call(this, normalizedName + '.js');
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
};
|
2014-09-25 17:04:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadModule(filepath, transpile) {
|
|
|
|
var data = fs.readFileSync(filepath, 'utf8');
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
throw new Error('Failed to import ' + filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transpile) {
|
2014-10-27 12:32:55 -04:00
|
|
|
var moduleName = path.normalize(filepath)
|
2014-09-26 20:39:27 -04:00
|
|
|
.replace(__dirname, 'transpiler')
|
2014-10-27 12:32:55 -04:00
|
|
|
.replace(/\\/g, '/')
|
2014-09-25 17:04:46 -04:00
|
|
|
.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;
|
2014-12-10 18:19:03 -05:00
|
|
|
}
|