angular-cn/tools/transpiler/gulp-traceur.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-09-25 17:30:10 -04:00
'use strict';
var through = require('through2');
var compiler = require('./index');
2014-09-25 17:30:10 -04:00
module.exports = gulpTraceur;
gulpTraceur.RUNTIME_PATH = compiler.RUNTIME_PATH;
gulpTraceur.sourcesChanged = compiler.sourcesChanged;
2014-09-25 17:30:10 -04:00
function gulpTraceur(options, resolveModuleName) {
2014-09-25 17:30:10 -04:00
options = options || {};
return through.obj(function (file, enc, done) {
if (file.isNull()) {
done();
return;
}
if (file.isStream()) {
throw new Error('gulp-traceur: Streaming not supported');
}
try {
var originalFilePath = file.history[0];
var moduleName = resolveModuleName ? resolveModuleName(file.relative) : null;
var compiled = compiler.compile(options, {
inputPath: file.relative,
outputPath: file.relative,
moduleName: moduleName
}, file.contents.toString());
2014-09-25 17:30:10 -04:00
file.contents = new Buffer(compiled);
this.push(file);
done();
} catch (errors) {
if (errors.join) {
throw new Error('gulp-traceur: '+errors.join('\n'));
} else {
console.error('Error when transpiling:\n ' + originalFilePath);
2014-09-25 17:30:10 -04:00
throw errors;
}
}
});
};