2015-04-06 16:30:35 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var fse = require('fs-extra');
|
|
|
|
var path = require('path');
|
|
|
|
var traceur = require('../../transpiler');
|
|
|
|
var walkSync = require('walk-sync');
|
|
|
|
var Writer = require('broccoli-writer');
|
|
|
|
var xtend = require('xtend');
|
|
|
|
|
|
|
|
class TraceurFilter extends Writer {
|
2015-04-09 18:56:54 -04:00
|
|
|
constructor(private inputTree, private destExtension: string = '.js',
|
|
|
|
private options = {}, private hackSourceMapExtension: boolean = false) {}
|
2015-04-08 11:28:12 -04:00
|
|
|
static RUNTIME_PATH = traceur.RUNTIME_PATH;
|
2015-04-06 16:30:35 -04:00
|
|
|
write(readTree, destDir) {
|
|
|
|
return readTree(this.inputTree)
|
2015-04-08 11:28:12 -04:00
|
|
|
.then(srcDir => {
|
|
|
|
walkSync(srcDir)
|
|
|
|
.filter(filepath =>
|
|
|
|
{
|
|
|
|
var extension = path.extname(filepath).toLowerCase();
|
2015-04-09 18:56:54 -04:00
|
|
|
return extension === '.js' || extension === '.es6' || extension === '.cjs';
|
2015-04-08 11:28:12 -04:00
|
|
|
})
|
|
|
|
.map(filepath => {
|
|
|
|
var options = xtend({filename: filepath}, this.options);
|
|
|
|
|
|
|
|
var fsOpts = {encoding: 'utf-8'};
|
|
|
|
var sourcecode = fs.readFileSync(path.join(srcDir, filepath), fsOpts);
|
|
|
|
|
|
|
|
var result = traceur.compile(options, filepath, sourcecode);
|
|
|
|
|
|
|
|
// TODO: we should fix the sourceMappingURL written by Traceur instead of overriding
|
|
|
|
// (but we might switch to typescript first)
|
2015-04-09 18:56:54 -04:00
|
|
|
var url = path.basename(filepath).replace(/\.es6$/, '') +
|
|
|
|
(this.destExtension === '.js' ? '.js.map' : '.map');
|
|
|
|
if (this.hackSourceMapExtension) {
|
|
|
|
url = path.basename(filepath).replace(/\.\w+$/, '') + '.map';
|
|
|
|
}
|
|
|
|
result.js = result.js + `\n//# sourceMappingURL=./${url}`;
|
2015-04-08 11:28:12 -04:00
|
|
|
|
|
|
|
var destFilepath = filepath.replace(/\.\w+$/, this.destExtension);
|
|
|
|
var destFile = path.join(destDir, destFilepath);
|
|
|
|
fse.mkdirsSync(path.dirname(destFile));
|
2015-04-09 18:56:54 -04:00
|
|
|
var destMap = path.join(destDir, destFilepath + '.map');
|
2015-04-08 11:28:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(destFile, result.js, fsOpts);
|
|
|
|
|
|
|
|
result.sourceMap.file = destFilepath;
|
|
|
|
fs.writeFileSync(destMap, JSON.stringify(result.sourceMap), fsOpts);
|
|
|
|
});
|
|
|
|
});
|
2015-04-06 16:30:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TraceurFilter;
|