2015-04-10 19:45:24 -04:00
|
|
|
/// <reference path="../../typings/fs-extra/fs-extra.d.ts" />
|
|
|
|
/// <reference path="../../typings/node/node.d.ts" />
|
2015-04-24 14:00:15 -04:00
|
|
|
|
2015-04-10 19:45:24 -04:00
|
|
|
import fs = require('fs');
|
|
|
|
import fse = require('fs-extra');
|
|
|
|
import path = require('path');
|
2015-05-04 11:19:25 -04:00
|
|
|
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from '../diffing-broccoli-plugin';
|
2015-04-06 16:30:35 -04:00
|
|
|
|
2015-04-26 04:04:28 -04:00
|
|
|
let traceur = require('../../../../tools/transpiler');
|
2015-04-24 14:00:15 -04:00
|
|
|
let xtend = require('xtend');
|
|
|
|
|
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
class DiffingTraceurCompiler implements DiffingBroccoliPlugin {
|
|
|
|
constructor(public inputPath: string, public cachePath: string, public options) {}
|
2015-04-24 14:00:15 -04:00
|
|
|
|
2015-05-06 19:24:10 -04:00
|
|
|
static includeExtensions = ['.js', '.es6', '.cjs'];
|
2015-04-24 14:00:15 -04:00
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
rebuild(treeDiff: DiffResult) {
|
2015-05-31 20:24:21 -04:00
|
|
|
treeDiff.addedPaths.concat(treeDiff.changedPaths)
|
|
|
|
.forEach((changedFilePath) => {
|
|
|
|
var traceurOpts = xtend({filename: changedFilePath}, this.options.traceurOptions);
|
|
|
|
|
|
|
|
var fsOpts = {encoding: 'utf-8'};
|
|
|
|
var absoluteInputFilePath = path.join(this.inputPath, changedFilePath);
|
|
|
|
var sourcecode = fs.readFileSync(absoluteInputFilePath, fsOpts);
|
|
|
|
|
|
|
|
var result = traceur.compile(traceurOpts, changedFilePath, sourcecode);
|
|
|
|
|
|
|
|
// TODO: we should fix the sourceMappingURL written by Traceur instead of overriding
|
|
|
|
// (but we might switch to typescript first)
|
|
|
|
var mapFilepath =
|
|
|
|
changedFilePath.replace(/\.\w+$/, '') + this.options.destSourceMapExtension;
|
|
|
|
result.js = result.js + '\n//# sourceMappingURL=./' + path.basename(mapFilepath);
|
|
|
|
|
|
|
|
var destFilepath = changedFilePath.replace(/\.\w+$/, this.options.destExtension);
|
|
|
|
var destFile = path.join(this.cachePath, destFilepath);
|
|
|
|
fse.mkdirsSync(path.dirname(destFile));
|
|
|
|
fs.writeFileSync(destFile, result.js, fsOpts);
|
|
|
|
|
|
|
|
var destMap = path.join(this.cachePath, mapFilepath);
|
|
|
|
result.sourceMap.file = destFilepath;
|
|
|
|
fs.writeFileSync(destMap, JSON.stringify(result.sourceMap), fsOpts);
|
|
|
|
});
|
2015-04-24 14:00:15 -04:00
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
treeDiff.removedPaths.forEach((removedFilePath) => {
|
2015-05-06 19:24:10 -04:00
|
|
|
var destFilepath = removedFilePath.replace(/\.\w+$/, this.options.destExtension);
|
|
|
|
var absoluteOuputFilePath = path.join(this.cachePath, destFilepath);
|
|
|
|
fs.unlinkSync(absoluteOuputFilePath);
|
2015-04-24 14:00:15 -04:00
|
|
|
});
|
2015-04-06 16:30:35 -04:00
|
|
|
}
|
2015-05-04 11:19:25 -04:00
|
|
|
}
|
2015-04-06 16:30:35 -04:00
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
let transpileWithTraceur = wrapDiffingPlugin(DiffingTraceurCompiler);
|
|
|
|
let TRACEUR_RUNTIME_PATH = traceur.RUNTIME_PATH;
|
2015-04-24 14:00:15 -04:00
|
|
|
|
2015-05-04 11:19:25 -04:00
|
|
|
export {transpileWithTraceur as default, TRACEUR_RUNTIME_PATH};
|