2014-09-26 20:39:27 -04:00
|
|
|
var transpiler = require('./index.js');
|
2014-09-25 20:55:46 -04:00
|
|
|
|
2014-09-25 17:04:46 -04:00
|
|
|
module.exports = {
|
|
|
|
'preprocessor:traceur': ['factory', createJs2DartPreprocessor]
|
|
|
|
};
|
2014-09-25 20:55:46 -04:00
|
|
|
|
2014-09-28 16:55:01 -04:00
|
|
|
function createJs2DartPreprocessor(logger, basePath, config, emitter) {
|
2014-09-25 17:04:46 -04:00
|
|
|
var log = logger.create('traceur');
|
2014-09-28 16:55:01 -04:00
|
|
|
// Reload the transpiler sources so we don't need to
|
|
|
|
// restart karma when we made changes to traceur.
|
|
|
|
// As there is no event that is called before any preprocessor runs,
|
|
|
|
// we listen for the end event in karma to reload the
|
|
|
|
// transpiler sources.
|
|
|
|
emitter.on('run_complete', function(filesPromise) {
|
|
|
|
transpiler.reloadSources();
|
|
|
|
});
|
2014-09-25 20:55:46 -04:00
|
|
|
return function(content, file, done) {
|
2014-09-25 17:04:46 -04:00
|
|
|
try {
|
|
|
|
var moduleName = config.resolveModuleName(file.originalPath);
|
|
|
|
if (config.transformPath) {
|
|
|
|
file.path = config.transformPath(file.originalPath);
|
|
|
|
}
|
2014-10-30 13:52:32 -04:00
|
|
|
|
|
|
|
var result = transpiler.compile(config.options, {
|
2014-09-25 17:04:46 -04:00
|
|
|
inputPath: file.originalPath,
|
2014-10-30 13:52:32 -04:00
|
|
|
outputPath: file.path,
|
2014-09-25 17:04:46 -04:00
|
|
|
moduleName: moduleName
|
2015-03-17 19:43:30 -04:00
|
|
|
}, content, true);
|
2014-10-30 13:52:32 -04:00
|
|
|
|
|
|
|
var transpiledContent = result.js;
|
|
|
|
var sourceMap = result.sourceMap;
|
|
|
|
|
|
|
|
if (sourceMap) {
|
|
|
|
transpiledContent += '\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,';
|
|
|
|
transpiledContent += new Buffer(JSON.stringify(sourceMap)).toString('base64') + '\n';
|
|
|
|
file.sourceMap = sourceMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
done(null, transpiledContent);
|
2014-09-25 17:04:46 -04:00
|
|
|
} catch (errors) {
|
|
|
|
var errorString;
|
|
|
|
if (errors.forEach) {
|
|
|
|
errors.forEach(function(error) {
|
|
|
|
log.error(error);
|
|
|
|
});
|
|
|
|
errorString = errors.join('\n');
|
|
|
|
} else {
|
|
|
|
log.error(errors);
|
|
|
|
errorString = errors;
|
|
|
|
}
|
|
|
|
done(new Error('TRACEUR COMPILE ERRORS\n' + errorString));
|
2014-09-25 20:55:46 -04:00
|
|
|
}
|
|
|
|
};
|
2014-09-25 17:04:46 -04:00
|
|
|
}
|
2014-09-25 20:55:46 -04:00
|
|
|
|
2014-09-28 16:55:01 -04:00
|
|
|
createJs2DartPreprocessor.$inject = ['logger', 'config.basePath', 'config.traceurPreprocessor', 'emitter'];
|